Implemented Bulk for Vendors

This commit is contained in:
Gianfranco Gasbarri 2019-11-03 19:26:15 +00:00
parent 7fa242ced1
commit b4e423b956
4 changed files with 138 additions and 53 deletions

View File

@ -42,6 +42,32 @@ class VendorRepository {
return vendorResponse.data;
}
Future<List<VendorEntity>> bulkAction(
Credentials credentials, List<String> 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<VendorEntity> saveData(Credentials credentials, VendorEntity vendor,
[EntityAction action]) async {
final data = serializers.serializeWith(VendorEntity.serializer, vendor);

View File

@ -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<String> vendorIds;
}
class ArchiveVendorSuccess implements StopSaving, PersistData {
ArchiveVendorSuccess(this.vendor);
ArchiveVendorSuccess(this.vendors);
final VendorEntity vendor;
final List<VendorEntity> vendors;
}
class ArchiveVendorFailure implements StopSaving {
ArchiveVendorFailure(this.vendor);
ArchiveVendorFailure(this.vendors);
final VendorEntity vendor;
final List<VendorEntity> vendors;
}
class DeleteVendorRequest implements StartSaving {
DeleteVendorRequest(this.completer, this.vendorId);
DeleteVendorRequest(this.completer, this.vendorIds);
final Completer completer;
final String vendorId;
final List<String> vendorIds;
}
class DeleteVendorSuccess implements StopSaving, PersistData {
DeleteVendorSuccess(this.vendor);
DeleteVendorSuccess(this.vendors);
final VendorEntity vendor;
final List<VendorEntity> vendors;
}
class DeleteVendorFailure implements StopSaving {
DeleteVendorFailure(this.vendor);
DeleteVendorFailure(this.vendors);
final VendorEntity vendor;
final List<VendorEntity> vendors;
}
class RestoreVendorRequest implements StartSaving {
RestoreVendorRequest(this.completer, this.vendorId);
RestoreVendorRequest(this.completer, this.vendorIds);
final Completer completer;
final String vendorId;
final List<String> vendorIds;
}
class RestoreVendorSuccess implements StopSaving, PersistData {
RestoreVendorSuccess(this.vendor);
RestoreVendorSuccess(this.vendors);
final VendorEntity vendor;
final List<VendorEntity> vendors;
}
class RestoreVendorFailure implements StopSaving {
RestoreVendorFailure(this.vendor);
RestoreVendorFailure(this.vendors);
final VendorEntity vendor;
final List<VendorEntity> 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()) {

View File

@ -112,17 +112,21 @@ Middleware<AppState> _viewVendorList() {
Middleware<AppState> _archiveVendor(VendorRepository repository) {
return (Store<AppState> 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<VendorEntity> 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<AppState> _archiveVendor(VendorRepository repository) {
Middleware<AppState> _deleteVendor(VendorRepository repository) {
return (Store<AppState> 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<VendorEntity> 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<AppState> _deleteVendor(VendorRepository repository) {
Middleware<AppState> _restoreVendor(VendorRepository repository) {
return (Store<AppState> 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<VendorEntity> 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);
}

View File

@ -192,57 +192,103 @@ final vendorsReducer = combineReducers<VendorState>([
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) {