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; 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, Future<VendorEntity> saveData(Credentials credentials, VendorEntity vendor,
[EntityAction action]) async { [EntityAction action]) async {
final data = serializers.serializeWith(VendorEntity.serializer, vendor); final data = serializers.serializeWith(VendorEntity.serializer, vendor);

View File

@ -148,60 +148,60 @@ class SaveVendorFailure implements StopSaving {
} }
class ArchiveVendorRequest implements StartSaving { class ArchiveVendorRequest implements StartSaving {
ArchiveVendorRequest(this.completer, this.vendorId); ArchiveVendorRequest(this.completer, this.vendorIds);
final Completer completer; final Completer completer;
final String vendorId; final List<String> vendorIds;
} }
class ArchiveVendorSuccess implements StopSaving, PersistData { class ArchiveVendorSuccess implements StopSaving, PersistData {
ArchiveVendorSuccess(this.vendor); ArchiveVendorSuccess(this.vendors);
final VendorEntity vendor; final List<VendorEntity> vendors;
} }
class ArchiveVendorFailure implements StopSaving { class ArchiveVendorFailure implements StopSaving {
ArchiveVendorFailure(this.vendor); ArchiveVendorFailure(this.vendors);
final VendorEntity vendor; final List<VendorEntity> vendors;
} }
class DeleteVendorRequest implements StartSaving { class DeleteVendorRequest implements StartSaving {
DeleteVendorRequest(this.completer, this.vendorId); DeleteVendorRequest(this.completer, this.vendorIds);
final Completer completer; final Completer completer;
final String vendorId; final List<String> vendorIds;
} }
class DeleteVendorSuccess implements StopSaving, PersistData { class DeleteVendorSuccess implements StopSaving, PersistData {
DeleteVendorSuccess(this.vendor); DeleteVendorSuccess(this.vendors);
final VendorEntity vendor; final List<VendorEntity> vendors;
} }
class DeleteVendorFailure implements StopSaving { class DeleteVendorFailure implements StopSaving {
DeleteVendorFailure(this.vendor); DeleteVendorFailure(this.vendors);
final VendorEntity vendor; final List<VendorEntity> vendors;
} }
class RestoreVendorRequest implements StartSaving { class RestoreVendorRequest implements StartSaving {
RestoreVendorRequest(this.completer, this.vendorId); RestoreVendorRequest(this.completer, this.vendorIds);
final Completer completer; final Completer completer;
final String vendorId; final List<String> vendorIds;
} }
class RestoreVendorSuccess implements StopSaving, PersistData { class RestoreVendorSuccess implements StopSaving, PersistData {
RestoreVendorSuccess(this.vendor); RestoreVendorSuccess(this.vendors);
final VendorEntity vendor; final List<VendorEntity> vendors;
} }
class RestoreVendorFailure implements StopSaving { class RestoreVendorFailure implements StopSaving {
RestoreVendorFailure(this.vendor); RestoreVendorFailure(this.vendors);
final VendorEntity vendor; final List<VendorEntity> vendors;
} }
class EditVendorContact implements PersistUI { class EditVendorContact implements PersistUI {
@ -283,6 +283,7 @@ void handleVendorAction(
final CompanyEntity company = state.selectedCompany; final CompanyEntity company = state.selectedCompany;
final localization = AppLocalization.of(context); final localization = AppLocalization.of(context);
final vendor = vendors.first as VendorEntity; final vendor = vendors.first as VendorEntity;
final vendorIds = vendors.map((vendor) => vendor.id).toList();
switch (action) { switch (action) {
case EntityAction.edit: case EntityAction.edit:
@ -295,15 +296,15 @@ void handleVendorAction(
break; break;
case EntityAction.restore: case EntityAction.restore:
store.dispatch(RestoreVendorRequest( store.dispatch(RestoreVendorRequest(
snackBarCompleter(context, localization.restoredVendor), vendor.id)); snackBarCompleter(context, localization.restoredVendor), vendorIds));
break; break;
case EntityAction.archive: case EntityAction.archive:
store.dispatch(ArchiveVendorRequest( store.dispatch(ArchiveVendorRequest(
snackBarCompleter(context, localization.archivedVendor), vendor.id)); snackBarCompleter(context, localization.archivedVendor), vendorIds));
break; break;
case EntityAction.delete: case EntityAction.delete:
store.dispatch(DeleteVendorRequest( store.dispatch(DeleteVendorRequest(
snackBarCompleter(context, localization.deletedVendor), vendor.id)); snackBarCompleter(context, localization.deletedVendor), vendorIds));
break; break;
case EntityAction.toggleMultiselect: case EntityAction.toggleMultiselect:
if (!store.state.vendorListState.isInMultiselect()) { if (!store.state.vendorListState.isInMultiselect()) {

View File

@ -112,17 +112,21 @@ Middleware<AppState> _viewVendorList() {
Middleware<AppState> _archiveVendor(VendorRepository repository) { Middleware<AppState> _archiveVendor(VendorRepository repository) {
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) { return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
final action = dynamicAction as ArchiveVendorRequest; final action = dynamicAction as ArchiveVendorRequest;
final origVendor = store.state.vendorState.map[action.vendorId];
repository repository
.saveData(store.state.credentials, origVendor, EntityAction.archive) .bulkAction(
.then((VendorEntity vendor) { store.state.credentials, action.vendorIds, EntityAction.archive)
store.dispatch(ArchiveVendorSuccess(vendor)); .then((List<VendorEntity> vendors) {
store.dispatch(ArchiveVendorSuccess(vendors));
if (action.completer != null) { if (action.completer != null) {
action.completer.complete(null); action.completer.complete(null);
} }
}).catchError((Object error) { }).catchError((Object error) {
print(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) { if (action.completer != null) {
action.completer.completeError(error); action.completer.completeError(error);
} }
@ -135,17 +139,21 @@ Middleware<AppState> _archiveVendor(VendorRepository repository) {
Middleware<AppState> _deleteVendor(VendorRepository repository) { Middleware<AppState> _deleteVendor(VendorRepository repository) {
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) { return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
final action = dynamicAction as DeleteVendorRequest; final action = dynamicAction as DeleteVendorRequest;
final origVendor = store.state.vendorState.map[action.vendorId];
repository repository
.saveData(store.state.credentials, origVendor, EntityAction.delete) .bulkAction(
.then((VendorEntity vendor) { store.state.credentials, action.vendorIds, EntityAction.delete)
store.dispatch(DeleteVendorSuccess(vendor)); .then((List<VendorEntity> vendors) {
store.dispatch(DeleteVendorSuccess(vendors));
if (action.completer != null) { if (action.completer != null) {
action.completer.complete(null); action.completer.complete(null);
} }
}).catchError((Object error) { }).catchError((Object error) {
print(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) { if (action.completer != null) {
action.completer.completeError(error); action.completer.completeError(error);
} }
@ -158,17 +166,21 @@ Middleware<AppState> _deleteVendor(VendorRepository repository) {
Middleware<AppState> _restoreVendor(VendorRepository repository) { Middleware<AppState> _restoreVendor(VendorRepository repository) {
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) { return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
final action = dynamicAction as RestoreVendorRequest; final action = dynamicAction as RestoreVendorRequest;
final origVendor = store.state.vendorState.map[action.vendorId];
repository repository
.saveData(store.state.credentials, origVendor, EntityAction.restore) .bulkAction(
.then((VendorEntity vendor) { store.state.credentials, action.vendorIds, EntityAction.restore)
store.dispatch(RestoreVendorSuccess(vendor)); .then((List<VendorEntity> vendors) {
store.dispatch(RestoreVendorSuccess(vendors));
if (action.completer != null) { if (action.completer != null) {
action.completer.complete(null); action.completer.complete(null);
} }
}).catchError((Object error) { }).catchError((Object error) {
print(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) { if (action.completer != null) {
action.completer.completeError(error); action.completer.completeError(error);
} }

View File

@ -192,57 +192,103 @@ final vendorsReducer = combineReducers<VendorState>([
VendorState _archiveVendorRequest( VendorState _archiveVendorRequest(
VendorState vendorState, ArchiveVendorRequest action) { VendorState vendorState, ArchiveVendorRequest action) {
final vendor = vendorState.map[action.vendorId] final vendors = action.vendorIds.map((id) => vendorState.map[id]).toList();
.rebuild((b) => b..archivedAt = DateTime.now().millisecondsSinceEpoch);
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 _archiveVendorSuccess(
VendorState vendorState, ArchiveVendorSuccess action) { 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 _archiveVendorFailure(
VendorState vendorState, ArchiveVendorFailure action) { 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 _deleteVendorRequest(
VendorState vendorState, DeleteVendorRequest action) { VendorState vendorState, DeleteVendorRequest action) {
final vendor = vendorState.map[action.vendorId].rebuild((b) => b final vendors = action.vendorIds.map((id) => vendorState.map[id]).toList();
..archivedAt = DateTime.now().millisecondsSinceEpoch
..isDeleted = true);
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 _deleteVendorSuccess(
VendorState vendorState, DeleteVendorSuccess action) { 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 _deleteVendorFailure(
VendorState vendorState, DeleteVendorFailure action) { 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 _restoreVendorRequest(
VendorState vendorState, RestoreVendorRequest action) { VendorState vendorState, RestoreVendorRequest action) {
final vendor = vendorState.map[action.vendorId].rebuild((b) => b final vendors = action.vendorIds.map((id) => vendorState.map[id]).toList();
..archivedAt = null
..isDeleted = false); for (int i = 0; i < vendors.length; i++) {
return vendorState.rebuild((b) => b..map[action.vendorId] = vendor); 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 _restoreVendorSuccess(
VendorState vendorState, RestoreVendorSuccess action) { 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 _restoreVendorFailure(
VendorState vendorState, RestoreVendorFailure action) { 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) { VendorState _addVendor(VendorState vendorState, AddVendorSuccess action) {