Add confirmation window when documents have been deleted

This commit is contained in:
Laurent Nguyen 2024-05-15 15:59:53 +02:00
parent ec913eb84c
commit 1b63553058
3 changed files with 23 additions and 12 deletions

View File

@ -33,6 +33,7 @@ export const deleteDocument = async (collection: CollectionBase, documentId: Doc
* @returns array of ids that were successfully deleted
*/
export const deleteDocuments = async (collection: CollectionBase, documentIds: DocumentId[]): Promise<DocumentId[]> => {
const nbDocuments = documentIds.length;
const clearMessage = logConsoleProgress(`Deleting ${documentIds.length} ${getEntityName(true)}`);
try {
const v2Container = await client().database(collection.databaseId).container(collection.id());
@ -69,8 +70,8 @@ export const deleteDocuments = async (collection: CollectionBase, documentIds: D
const allResult = await Promise.all(promiseArray);
const flatAllResult = Array.prototype.concat.apply([], allResult);
logConsoleInfo(`Successfully deleted ${getEntityName(true)}: ${flatAllResult.length} out of ${documentIds.length}`);
// TODO: handle case result.length != documentIds.length
logConsoleInfo(`Successfully deleted ${getEntityName(true)}: ${flatAllResult.length} out of ${nbDocuments}`);
// TODO: handle case result.length != nbDocuments
return flatAllResult;
} catch (error) {
handleError(error, "DeleteDocuments", `Error while deleting ${documentIds.length} ${getEntityName(true)}`);

View File

@ -70,6 +70,7 @@ jest.mock("Explorer/Controls/Dialog", () => ({
useDialog: {
getState: jest.fn(() => ({
showOkCancelModalDialog: (title: string, subText: string, okLabel: string, onOk: () => void) => onOk(),
showOkModalDialog: () => {},
})),
},
}));

View File

@ -860,7 +860,7 @@ export const DocumentsTabComponent: React.FunctionComponent<IDocumentsTabCompone
},
startKey,
);
return undefined;
throw error;
},
)
.finally(() => setIsExecuting(false));
@ -873,7 +873,8 @@ export const DocumentsTabComponent: React.FunctionComponent<IDocumentsTabCompone
onExecutionErrorChange(false);
setIsExecuting(true);
_deleteDocuments(toDeleteDocumentIds)
.then((deletedIds: DocumentId[]) => {
.then(
(deletedIds: DocumentId[]) => {
const deletedRids = new Set(deletedIds.map((documentId) => documentId.rid));
const newDocumentIds = [...documentIds.filter((documentId) => !deletedRids.has(documentId.rid))];
setDocumentIds(newDocumentIds);
@ -882,7 +883,15 @@ export const DocumentsTabComponent: React.FunctionComponent<IDocumentsTabCompone
setClickedRow(undefined);
setSelectedRows(new Set());
setEditorState(ViewModels.DocumentExplorerState.noDocumentSelected);
})
useDialog
.getState()
.showOkModalDialog("Delete documents", `${deletedIds.length} document(s) successfully deleted.`);
},
(error: Error) =>
useDialog
.getState()
.showOkModalDialog("Delete documents", `Document(s) deleted failed (${JSON.stringify(error)})`),
)
.finally(() => setIsExecuting(false));
},
[onExecutionErrorChange, _deleteDocuments, documentIds],