diff --git a/lib/data/repositories/vendor_repository.dart b/lib/data/repositories/vendor_repository.dart index 59c6645f5..8189282f9 100644 --- a/lib/data/repositories/vendor_repository.dart +++ b/lib/data/repositories/vendor_repository.dart @@ -42,6 +42,32 @@ class VendorRepository { return vendorResponse.data; } + Future> bulkAction( + Credentials credentials, List ids, EntityAction action) async { + dynamic response; + + switch (action) { + case EntityAction.restore: + case EntityAction.archive: + case EntityAction.delete: + var url = credentials.url + '/vendors/bulk?include=activities'; + if (action != null) { + url += '&action=' + action.toString(); + } + response = await webClient.post(url, credentials.token, + data: json.encode([ids])); + break; + default: + // Might have other actions in the future + break; + } + + final VendorListResponse vendorResponse = + serializers.deserializeWith(VendorListResponse.serializer, response); + + return vendorResponse.data.toList(); + } + Future saveData(Credentials credentials, VendorEntity vendor, [EntityAction action]) async { final data = serializers.serializeWith(VendorEntity.serializer, vendor); diff --git a/lib/redux/vendor/vendor_actions.dart b/lib/redux/vendor/vendor_actions.dart index f716494aa..8fde20db2 100644 --- a/lib/redux/vendor/vendor_actions.dart +++ b/lib/redux/vendor/vendor_actions.dart @@ -148,60 +148,60 @@ class SaveVendorFailure implements StopSaving { } class ArchiveVendorRequest implements StartSaving { - ArchiveVendorRequest(this.completer, this.vendorId); + ArchiveVendorRequest(this.completer, this.vendorIds); final Completer completer; - final String vendorId; + final List vendorIds; } class ArchiveVendorSuccess implements StopSaving, PersistData { - ArchiveVendorSuccess(this.vendor); + ArchiveVendorSuccess(this.vendors); - final VendorEntity vendor; + final List vendors; } class ArchiveVendorFailure implements StopSaving { - ArchiveVendorFailure(this.vendor); + ArchiveVendorFailure(this.vendors); - final VendorEntity vendor; + final List vendors; } class DeleteVendorRequest implements StartSaving { - DeleteVendorRequest(this.completer, this.vendorId); + DeleteVendorRequest(this.completer, this.vendorIds); final Completer completer; - final String vendorId; + final List vendorIds; } class DeleteVendorSuccess implements StopSaving, PersistData { - DeleteVendorSuccess(this.vendor); + DeleteVendorSuccess(this.vendors); - final VendorEntity vendor; + final List vendors; } class DeleteVendorFailure implements StopSaving { - DeleteVendorFailure(this.vendor); + DeleteVendorFailure(this.vendors); - final VendorEntity vendor; + final List vendors; } class RestoreVendorRequest implements StartSaving { - RestoreVendorRequest(this.completer, this.vendorId); + RestoreVendorRequest(this.completer, this.vendorIds); final Completer completer; - final String vendorId; + final List vendorIds; } class RestoreVendorSuccess implements StopSaving, PersistData { - RestoreVendorSuccess(this.vendor); + RestoreVendorSuccess(this.vendors); - final VendorEntity vendor; + final List vendors; } class RestoreVendorFailure implements StopSaving { - RestoreVendorFailure(this.vendor); + RestoreVendorFailure(this.vendors); - final VendorEntity vendor; + final List vendors; } class EditVendorContact implements PersistUI { @@ -283,6 +283,7 @@ void handleVendorAction( final CompanyEntity company = state.selectedCompany; final localization = AppLocalization.of(context); final vendor = vendors.first as VendorEntity; + final vendorIds = vendors.map((vendor) => vendor.id).toList(); switch (action) { case EntityAction.edit: @@ -295,15 +296,15 @@ void handleVendorAction( break; case EntityAction.restore: store.dispatch(RestoreVendorRequest( - snackBarCompleter(context, localization.restoredVendor), vendor.id)); + snackBarCompleter(context, localization.restoredVendor), vendorIds)); break; case EntityAction.archive: store.dispatch(ArchiveVendorRequest( - snackBarCompleter(context, localization.archivedVendor), vendor.id)); + snackBarCompleter(context, localization.archivedVendor), vendorIds)); break; case EntityAction.delete: store.dispatch(DeleteVendorRequest( - snackBarCompleter(context, localization.deletedVendor), vendor.id)); + snackBarCompleter(context, localization.deletedVendor), vendorIds)); break; case EntityAction.toggleMultiselect: if (!store.state.vendorListState.isInMultiselect()) { diff --git a/lib/redux/vendor/vendor_middleware.dart b/lib/redux/vendor/vendor_middleware.dart index 36e3904d4..719cdd227 100644 --- a/lib/redux/vendor/vendor_middleware.dart +++ b/lib/redux/vendor/vendor_middleware.dart @@ -112,17 +112,21 @@ Middleware _viewVendorList() { Middleware _archiveVendor(VendorRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as ArchiveVendorRequest; - final origVendor = store.state.vendorState.map[action.vendorId]; + repository - .saveData(store.state.credentials, origVendor, EntityAction.archive) - .then((VendorEntity vendor) { - store.dispatch(ArchiveVendorSuccess(vendor)); + .bulkAction( + store.state.credentials, action.vendorIds, EntityAction.archive) + .then((List vendors) { + store.dispatch(ArchiveVendorSuccess(vendors)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); - store.dispatch(ArchiveVendorFailure(origVendor)); + final vendors = action.vendorIds + .map((id) => store.state.vendorState.map[id]) + .toList(); + store.dispatch(ArchiveVendorFailure(vendors)); if (action.completer != null) { action.completer.completeError(error); } @@ -135,17 +139,21 @@ Middleware _archiveVendor(VendorRepository repository) { Middleware _deleteVendor(VendorRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as DeleteVendorRequest; - final origVendor = store.state.vendorState.map[action.vendorId]; + repository - .saveData(store.state.credentials, origVendor, EntityAction.delete) - .then((VendorEntity vendor) { - store.dispatch(DeleteVendorSuccess(vendor)); + .bulkAction( + store.state.credentials, action.vendorIds, EntityAction.delete) + .then((List vendors) { + store.dispatch(DeleteVendorSuccess(vendors)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); - store.dispatch(DeleteVendorFailure(origVendor)); + final vendors = action.vendorIds + .map((id) => store.state.vendorState.map[id]) + .toList(); + store.dispatch(DeleteVendorFailure(vendors)); if (action.completer != null) { action.completer.completeError(error); } @@ -158,17 +166,21 @@ Middleware _deleteVendor(VendorRepository repository) { Middleware _restoreVendor(VendorRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as RestoreVendorRequest; - final origVendor = store.state.vendorState.map[action.vendorId]; + repository - .saveData(store.state.credentials, origVendor, EntityAction.restore) - .then((VendorEntity vendor) { - store.dispatch(RestoreVendorSuccess(vendor)); + .bulkAction( + store.state.credentials, action.vendorIds, EntityAction.restore) + .then((List vendors) { + store.dispatch(RestoreVendorSuccess(vendors)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); - store.dispatch(RestoreVendorFailure(origVendor)); + final vendors = action.vendorIds + .map((id) => store.state.vendorState.map[id]) + .toList(); + store.dispatch(RestoreVendorFailure(vendors)); if (action.completer != null) { action.completer.completeError(error); } diff --git a/lib/redux/vendor/vendor_reducer.dart b/lib/redux/vendor/vendor_reducer.dart index d5d637551..4c1653a1e 100644 --- a/lib/redux/vendor/vendor_reducer.dart +++ b/lib/redux/vendor/vendor_reducer.dart @@ -192,57 +192,103 @@ final vendorsReducer = combineReducers([ VendorState _archiveVendorRequest( VendorState vendorState, ArchiveVendorRequest action) { - final vendor = vendorState.map[action.vendorId] - .rebuild((b) => b..archivedAt = DateTime.now().millisecondsSinceEpoch); + final vendors = action.vendorIds.map((id) => vendorState.map[id]).toList(); - return vendorState.rebuild((b) => b..map[action.vendorId] = vendor); + for (int i = 0; i < vendors.length; i++) { + vendors[i] = vendors[i] + .rebuild((b) => b..archivedAt = DateTime.now().millisecondsSinceEpoch); + } + return vendorState.rebuild((b) { + for (final vendor in vendors) { + b.map[vendor.id] = vendor; + } + }); } VendorState _archiveVendorSuccess( VendorState vendorState, ArchiveVendorSuccess action) { - return vendorState.rebuild((b) => b..map[action.vendor.id] = action.vendor); + return vendorState.rebuild((b) { + for (final vendor in action.vendors) { + b.map[vendor.id] = vendor; + } + }); } VendorState _archiveVendorFailure( VendorState vendorState, ArchiveVendorFailure action) { - return vendorState.rebuild((b) => b..map[action.vendor.id] = action.vendor); + return vendorState.rebuild((b) { + for (final vendor in action.vendors) { + b.map[vendor.id] = vendor; + } + }); } VendorState _deleteVendorRequest( VendorState vendorState, DeleteVendorRequest action) { - final vendor = vendorState.map[action.vendorId].rebuild((b) => b - ..archivedAt = DateTime.now().millisecondsSinceEpoch - ..isDeleted = true); + final vendors = action.vendorIds.map((id) => vendorState.map[id]).toList(); - return vendorState.rebuild((b) => b..map[action.vendorId] = vendor); + for (int i = 0; i < vendors.length; i++) { + vendors[i] = vendors[i].rebuild((b) => b + ..archivedAt = DateTime.now().millisecondsSinceEpoch + ..isDeleted = true); + } + return vendorState.rebuild((b) { + for (final vendor in vendors) { + b.map[vendor.id] = vendor; + } + }); } VendorState _deleteVendorSuccess( VendorState vendorState, DeleteVendorSuccess action) { - return vendorState.rebuild((b) => b..map[action.vendor.id] = action.vendor); + return vendorState.rebuild((b) { + for (final vendor in action.vendors) { + b.map[vendor.id] = vendor; + } + }); } VendorState _deleteVendorFailure( VendorState vendorState, DeleteVendorFailure action) { - return vendorState.rebuild((b) => b..map[action.vendor.id] = action.vendor); + return vendorState.rebuild((b) { + for (final vendor in action.vendors) { + b.map[vendor.id] = vendor; + } + }); } VendorState _restoreVendorRequest( VendorState vendorState, RestoreVendorRequest action) { - final vendor = vendorState.map[action.vendorId].rebuild((b) => b - ..archivedAt = null - ..isDeleted = false); - return vendorState.rebuild((b) => b..map[action.vendorId] = vendor); + final vendors = action.vendorIds.map((id) => vendorState.map[id]).toList(); + + for (int i = 0; i < vendors.length; i++) { + vendors[i] = vendors[i].rebuild((b) => b + ..archivedAt = null + ..isDeleted = false); + } + return vendorState.rebuild((b) { + for (final vendor in vendors) { + b.map[vendor.id] = vendor; + } + }); } VendorState _restoreVendorSuccess( VendorState vendorState, RestoreVendorSuccess action) { - return vendorState.rebuild((b) => b..map[action.vendor.id] = action.vendor); + return vendorState.rebuild((b) { + for (final vendor in action.vendors) { + b.map[vendor.id] = vendor; + } + }); } VendorState _restoreVendorFailure( VendorState vendorState, RestoreVendorFailure action) { - return vendorState.rebuild((b) => b..map[action.vendor.id] = action.vendor); + return vendorState.rebuild((b) { + for (final vendor in action.vendors) { + b.map[vendor.id] = vendor; + } + }); } VendorState _addVendor(VendorState vendorState, AddVendorSuccess action) {