Implemented Bulk for Documents
This commit is contained in:
parent
01e1875f36
commit
f9124c1583
|
|
@ -42,6 +42,32 @@ class DocumentRepository {
|
||||||
return documentResponse.data;
|
return documentResponse.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<List<DocumentEntity>> 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 + '/documents/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 DocumentListResponse documentResponse =
|
||||||
|
serializers.deserializeWith(DocumentListResponse.serializer, response);
|
||||||
|
|
||||||
|
return documentResponse.data.toList();
|
||||||
|
}
|
||||||
|
|
||||||
Future<DocumentEntity> saveData(
|
Future<DocumentEntity> saveData(
|
||||||
Credentials credentials, DocumentEntity document,
|
Credentials credentials, DocumentEntity document,
|
||||||
[EntityAction action]) async {
|
[EntityAction action]) async {
|
||||||
|
|
|
||||||
|
|
@ -136,60 +136,60 @@ class SaveDocumentFailure implements StopSaving {
|
||||||
}
|
}
|
||||||
|
|
||||||
class ArchiveDocumentRequest implements StartSaving {
|
class ArchiveDocumentRequest implements StartSaving {
|
||||||
ArchiveDocumentRequest(this.completer, this.documentId);
|
ArchiveDocumentRequest(this.completer, this.documentIds);
|
||||||
|
|
||||||
final Completer completer;
|
final Completer completer;
|
||||||
final String documentId;
|
final List<String> documentIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ArchiveDocumentSuccess implements StopSaving, PersistData {
|
class ArchiveDocumentSuccess implements StopSaving, PersistData {
|
||||||
ArchiveDocumentSuccess(this.document);
|
ArchiveDocumentSuccess(this.documents);
|
||||||
|
|
||||||
final DocumentEntity document;
|
final List<DocumentEntity> documents;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ArchiveDocumentFailure implements StopSaving {
|
class ArchiveDocumentFailure implements StopSaving {
|
||||||
ArchiveDocumentFailure(this.document);
|
ArchiveDocumentFailure(this.documents);
|
||||||
|
|
||||||
final DocumentEntity document;
|
final List<DocumentEntity> documents;
|
||||||
}
|
}
|
||||||
|
|
||||||
class DeleteDocumentRequest implements StartSaving {
|
class DeleteDocumentRequest implements StartSaving {
|
||||||
DeleteDocumentRequest(this.completer, this.documentId);
|
DeleteDocumentRequest(this.completer, this.documentIds);
|
||||||
|
|
||||||
final Completer completer;
|
final Completer completer;
|
||||||
final String documentId;
|
final List<String> documentIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
class DeleteDocumentSuccess implements StopSaving, PersistData {
|
class DeleteDocumentSuccess implements StopSaving, PersistData {
|
||||||
DeleteDocumentSuccess(this.document);
|
DeleteDocumentSuccess(this.documents);
|
||||||
|
|
||||||
final DocumentEntity document;
|
final List<DocumentEntity> documents;
|
||||||
}
|
}
|
||||||
|
|
||||||
class DeleteDocumentFailure implements StopSaving {
|
class DeleteDocumentFailure implements StopSaving {
|
||||||
DeleteDocumentFailure(this.document);
|
DeleteDocumentFailure(this.documents);
|
||||||
|
|
||||||
final DocumentEntity document;
|
final List<DocumentEntity> documents;
|
||||||
}
|
}
|
||||||
|
|
||||||
class RestoreDocumentRequest implements StartSaving {
|
class RestoreDocumentRequest implements StartSaving {
|
||||||
RestoreDocumentRequest(this.completer, this.documentId);
|
RestoreDocumentRequest(this.completer, this.documentIds);
|
||||||
|
|
||||||
final Completer completer;
|
final Completer completer;
|
||||||
final String documentId;
|
final List<String> documentIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
class RestoreDocumentSuccess implements StopSaving, PersistData {
|
class RestoreDocumentSuccess implements StopSaving, PersistData {
|
||||||
RestoreDocumentSuccess(this.document);
|
RestoreDocumentSuccess(this.documents);
|
||||||
|
|
||||||
final DocumentEntity document;
|
final List<DocumentEntity> documents;
|
||||||
}
|
}
|
||||||
|
|
||||||
class RestoreDocumentFailure implements StopSaving {
|
class RestoreDocumentFailure implements StopSaving {
|
||||||
RestoreDocumentFailure(this.document);
|
RestoreDocumentFailure(this.documents);
|
||||||
|
|
||||||
final DocumentEntity document;
|
final List<DocumentEntity> documents;
|
||||||
}
|
}
|
||||||
|
|
||||||
class FilterDocuments {
|
class FilterDocuments {
|
||||||
|
|
@ -244,6 +244,7 @@ void handleDocumentAction(
|
||||||
final store = StoreProvider.of<AppState>(context);
|
final store = StoreProvider.of<AppState>(context);
|
||||||
final localization = AppLocalization.of(context);
|
final localization = AppLocalization.of(context);
|
||||||
final document = documents.first;
|
final document = documents.first;
|
||||||
|
final documentIds = documents.map((document) => document.id).toList();
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case EntityAction.edit:
|
case EntityAction.edit:
|
||||||
|
|
@ -252,17 +253,17 @@ void handleDocumentAction(
|
||||||
case EntityAction.restore:
|
case EntityAction.restore:
|
||||||
store.dispatch(RestoreDocumentRequest(
|
store.dispatch(RestoreDocumentRequest(
|
||||||
snackBarCompleter(context, localization.restoredDocument),
|
snackBarCompleter(context, localization.restoredDocument),
|
||||||
document.id));
|
documentIds));
|
||||||
break;
|
break;
|
||||||
case EntityAction.archive:
|
case EntityAction.archive:
|
||||||
store.dispatch(ArchiveDocumentRequest(
|
store.dispatch(ArchiveDocumentRequest(
|
||||||
snackBarCompleter(context, localization.archivedDocument),
|
snackBarCompleter(context, localization.archivedDocument),
|
||||||
document.id));
|
documentIds));
|
||||||
break;
|
break;
|
||||||
case EntityAction.delete:
|
case EntityAction.delete:
|
||||||
store.dispatch(DeleteDocumentRequest(
|
store.dispatch(DeleteDocumentRequest(
|
||||||
snackBarCompleter(context, localization.deletedDocument),
|
snackBarCompleter(context, localization.deletedDocument),
|
||||||
document.id));
|
documentIds));
|
||||||
break;
|
break;
|
||||||
case EntityAction.toggleMultiselect:
|
case EntityAction.toggleMultiselect:
|
||||||
if (!store.state.documentListState.isInMultiselect()) {
|
if (!store.state.documentListState.isInMultiselect()) {
|
||||||
|
|
|
||||||
|
|
@ -86,17 +86,21 @@ Middleware<AppState> _viewDocumentList() {
|
||||||
Middleware<AppState> _archiveDocument(DocumentRepository repository) {
|
Middleware<AppState> _archiveDocument(DocumentRepository repository) {
|
||||||
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
||||||
final action = dynamicAction as ArchiveDocumentRequest;
|
final action = dynamicAction as ArchiveDocumentRequest;
|
||||||
final origDocument = store.state.documentState.map[action.documentId];
|
|
||||||
repository
|
repository
|
||||||
.saveData(store.state.credentials, origDocument, EntityAction.archive)
|
.bulkAction(
|
||||||
.then((DocumentEntity document) {
|
store.state.credentials, action.documentIds, EntityAction.archive)
|
||||||
store.dispatch(ArchiveDocumentSuccess(document));
|
.then((List<DocumentEntity> documents) {
|
||||||
|
store.dispatch(ArchiveDocumentSuccess(documents));
|
||||||
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(ArchiveDocumentFailure(origDocument));
|
final documents = action.documentIds
|
||||||
|
.map((id) => store.state.documentState.map[id])
|
||||||
|
.toList();
|
||||||
|
store.dispatch(ArchiveDocumentFailure(documents));
|
||||||
if (action.completer != null) {
|
if (action.completer != null) {
|
||||||
action.completer.completeError(error);
|
action.completer.completeError(error);
|
||||||
}
|
}
|
||||||
|
|
@ -109,17 +113,21 @@ Middleware<AppState> _archiveDocument(DocumentRepository repository) {
|
||||||
Middleware<AppState> _deleteDocument(DocumentRepository repository) {
|
Middleware<AppState> _deleteDocument(DocumentRepository repository) {
|
||||||
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
||||||
final action = dynamicAction as DeleteDocumentRequest;
|
final action = dynamicAction as DeleteDocumentRequest;
|
||||||
final origDocument = store.state.documentState.map[action.documentId];
|
|
||||||
repository
|
repository
|
||||||
.saveData(store.state.credentials, origDocument, EntityAction.delete)
|
.bulkAction(
|
||||||
.then((DocumentEntity document) {
|
store.state.credentials, action.documentIds, EntityAction.delete)
|
||||||
store.dispatch(DeleteDocumentSuccess(document));
|
.then((List<DocumentEntity> documents) {
|
||||||
|
store.dispatch(DeleteDocumentSuccess(documents));
|
||||||
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(DeleteDocumentFailure(origDocument));
|
final documents = action.documentIds
|
||||||
|
.map((id) => store.state.documentState.map[id])
|
||||||
|
.toList();
|
||||||
|
store.dispatch(DeleteDocumentFailure(documents));
|
||||||
if (action.completer != null) {
|
if (action.completer != null) {
|
||||||
action.completer.completeError(error);
|
action.completer.completeError(error);
|
||||||
}
|
}
|
||||||
|
|
@ -132,17 +140,21 @@ Middleware<AppState> _deleteDocument(DocumentRepository repository) {
|
||||||
Middleware<AppState> _restoreDocument(DocumentRepository repository) {
|
Middleware<AppState> _restoreDocument(DocumentRepository repository) {
|
||||||
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
|
||||||
final action = dynamicAction as RestoreDocumentRequest;
|
final action = dynamicAction as RestoreDocumentRequest;
|
||||||
final origDocument = store.state.documentState.map[action.documentId];
|
|
||||||
repository
|
repository
|
||||||
.saveData(store.state.credentials, origDocument, EntityAction.restore)
|
.bulkAction(
|
||||||
.then((DocumentEntity document) {
|
store.state.credentials, action.documentIds, EntityAction.restore)
|
||||||
store.dispatch(RestoreDocumentSuccess(document));
|
.then((List<DocumentEntity> documents) {
|
||||||
|
store.dispatch(RestoreDocumentSuccess(documents));
|
||||||
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(RestoreDocumentFailure(origDocument));
|
final documents = action.documentIds
|
||||||
|
.map((id) => store.state.documentState.map[id])
|
||||||
|
.toList();
|
||||||
|
store.dispatch(RestoreDocumentFailure(documents));
|
||||||
if (action.completer != null) {
|
if (action.completer != null) {
|
||||||
action.completer.completeError(error);
|
action.completer.completeError(error);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -151,63 +151,106 @@ final documentsReducer = combineReducers<DocumentState>([
|
||||||
|
|
||||||
DocumentState _archiveDocumentRequest(
|
DocumentState _archiveDocumentRequest(
|
||||||
DocumentState documentState, ArchiveDocumentRequest action) {
|
DocumentState documentState, ArchiveDocumentRequest action) {
|
||||||
final document = documentState.map[action.documentId]
|
final documents =
|
||||||
.rebuild((b) => b..archivedAt = DateTime.now().millisecondsSinceEpoch);
|
action.documentIds.map((id) => documentState.map[id]).toList();
|
||||||
|
|
||||||
return documentState.rebuild((b) => b..map[action.documentId] = document);
|
for (int i = 0; i < documents.length; i++) {
|
||||||
|
documents[i] = documents[i]
|
||||||
|
.rebuild((b) => b..archivedAt = DateTime.now().millisecondsSinceEpoch);
|
||||||
|
}
|
||||||
|
return documentState.rebuild((b) {
|
||||||
|
for (final document in documents) {
|
||||||
|
b.map[document.id] = document;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
DocumentState _archiveDocumentSuccess(
|
DocumentState _archiveDocumentSuccess(
|
||||||
DocumentState documentState, ArchiveDocumentSuccess action) {
|
DocumentState documentState, ArchiveDocumentSuccess action) {
|
||||||
return documentState
|
return documentState.rebuild((b) {
|
||||||
.rebuild((b) => b..map[action.document.id] = action.document);
|
for (final document in action.documents) {
|
||||||
|
b.map[document.id] = document;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
DocumentState _archiveDocumentFailure(
|
DocumentState _archiveDocumentFailure(
|
||||||
DocumentState documentState, ArchiveDocumentFailure action) {
|
DocumentState documentState, ArchiveDocumentFailure action) {
|
||||||
return documentState
|
return documentState.rebuild((b) {
|
||||||
.rebuild((b) => b..map[action.document.id] = action.document);
|
for (final document in action.documents) {
|
||||||
|
b.map[document.id] = document;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
DocumentState _deleteDocumentRequest(
|
DocumentState _deleteDocumentRequest(
|
||||||
DocumentState documentState, DeleteDocumentRequest action) {
|
DocumentState documentState, DeleteDocumentRequest action) {
|
||||||
final document = documentState.map[action.documentId].rebuild((b) => b
|
final documents =
|
||||||
..archivedAt = DateTime.now().millisecondsSinceEpoch
|
action.documentIds.map((id) => documentState.map[id]).toList();
|
||||||
..isDeleted = true);
|
|
||||||
|
|
||||||
return documentState.rebuild((b) => b..map[action.documentId] = document);
|
for (int i = 0; i < documents.length; i++) {
|
||||||
|
documents[i] = documents[i].rebuild((b) => b
|
||||||
|
..archivedAt = DateTime.now().millisecondsSinceEpoch
|
||||||
|
..isDeleted = true);
|
||||||
|
}
|
||||||
|
return documentState.rebuild((b) {
|
||||||
|
for (final document in documents) {
|
||||||
|
b.map[document.id] = document;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
DocumentState _deleteDocumentSuccess(
|
DocumentState _deleteDocumentSuccess(
|
||||||
DocumentState documentState, DeleteDocumentSuccess action) {
|
DocumentState documentState, DeleteDocumentSuccess action) {
|
||||||
return documentState.rebuild((b) =>
|
return documentState.rebuild((b) {
|
||||||
b..map.remove(action.document.id)..list.remove(action.document.id));
|
for (final document in action.documents) {
|
||||||
|
b.map[document.id] = document;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
DocumentState _deleteDocumentFailure(
|
DocumentState _deleteDocumentFailure(
|
||||||
DocumentState documentState, DeleteDocumentFailure action) {
|
DocumentState documentState, DeleteDocumentFailure action) {
|
||||||
return documentState
|
return documentState.rebuild((b) {
|
||||||
.rebuild((b) => b..map[action.document.id] = action.document);
|
for (final document in action.documents) {
|
||||||
|
b.map[document.id] = document;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
DocumentState _restoreDocumentRequest(
|
DocumentState _restoreDocumentRequest(
|
||||||
DocumentState documentState, RestoreDocumentRequest action) {
|
DocumentState documentState, RestoreDocumentRequest action) {
|
||||||
final document = documentState.map[action.documentId].rebuild((b) => b
|
final documents =
|
||||||
..archivedAt = null
|
action.documentIds.map((id) => documentState.map[id]).toList();
|
||||||
..isDeleted = false);
|
|
||||||
return documentState.rebuild((b) => b..map[action.documentId] = document);
|
for (int i = 0; i < documents.length; i++) {
|
||||||
|
documents[i] = documents[i].rebuild((b) => b
|
||||||
|
..archivedAt = null
|
||||||
|
..isDeleted = false);
|
||||||
|
}
|
||||||
|
return documentState.rebuild((b) {
|
||||||
|
for (final document in documents) {
|
||||||
|
b.map[document.id] = document;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
DocumentState _restoreDocumentSuccess(
|
DocumentState _restoreDocumentSuccess(
|
||||||
DocumentState documentState, RestoreDocumentSuccess action) {
|
DocumentState documentState, RestoreDocumentSuccess action) {
|
||||||
return documentState
|
return documentState.rebuild((b) {
|
||||||
.rebuild((b) => b..map[action.document.id] = action.document);
|
for (final document in action.documents) {
|
||||||
|
b.map[document.id] = document;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
DocumentState _restoreDocumentFailure(
|
DocumentState _restoreDocumentFailure(
|
||||||
DocumentState documentState, RestoreDocumentFailure action) {
|
DocumentState documentState, RestoreDocumentFailure action) {
|
||||||
return documentState
|
return documentState.rebuild((b) {
|
||||||
.rebuild((b) => b..map[action.document.id] = action.document);
|
for (final document in action.documents) {
|
||||||
|
b.map[document.id] = document;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
DocumentState _addDocument(
|
DocumentState _addDocument(
|
||||||
|
|
|
||||||
|
|
@ -172,7 +172,7 @@ class ExpenseViewVM {
|
||||||
store.dispatch(DeleteDocumentRequest(
|
store.dispatch(DeleteDocumentRequest(
|
||||||
snackBarCompleter(
|
snackBarCompleter(
|
||||||
context, AppLocalization.of(context).deletedDocument),
|
context, AppLocalization.of(context).deletedDocument),
|
||||||
document.id));
|
[document.id]));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -213,7 +213,7 @@ class InvoiceViewVM extends EntityViewVM {
|
||||||
store.dispatch(DeleteDocumentRequest(
|
store.dispatch(DeleteDocumentRequest(
|
||||||
snackBarCompleter(
|
snackBarCompleter(
|
||||||
context, AppLocalization.of(context).deletedDocument),
|
context, AppLocalization.of(context).deletedDocument),
|
||||||
document.id));
|
[document.id]));
|
||||||
},
|
},
|
||||||
onViewExpense: (BuildContext context, DocumentEntity document) {
|
onViewExpense: (BuildContext context, DocumentEntity document) {
|
||||||
store.dispatch(
|
store.dispatch(
|
||||||
|
|
|
||||||
|
|
@ -158,7 +158,7 @@ class QuoteViewVM extends EntityViewVM {
|
||||||
store.dispatch(DeleteDocumentRequest(
|
store.dispatch(DeleteDocumentRequest(
|
||||||
snackBarCompleter(
|
snackBarCompleter(
|
||||||
context, AppLocalization.of(context).deletedDocument),
|
context, AppLocalization.of(context).deletedDocument),
|
||||||
document.id));
|
[document.id]));
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue