From 2159f6720e7aea5100cb07fb19937279c58510fb Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Tue, 12 Sep 2023 17:07:26 +0500 Subject: [PATCH] mobile: fix delete notebook in bulk --- .../app/components/selection-header/index.js | 19 +----- apps/mobile/app/utils/functions.js | 63 +++++++++---------- 2 files changed, 32 insertions(+), 50 deletions(-) diff --git a/apps/mobile/app/components/selection-header/index.js b/apps/mobile/app/components/selection-header/index.js index 1b6264d76..197869e0f 100644 --- a/apps/mobile/app/components/selection-header/index.js +++ b/apps/mobile/app/components/selection-header/index.js @@ -317,23 +317,8 @@ export const SelectionHeader = React.memo(() => { customStyle={{ marginLeft: 10 }} - onPress={async () => { - presentDialog({ - title: `Delete ${ - selectedItemsList.length > 1 ? "items" : "item" - }`, - paragraph: `Are you sure you want to delete ${ - selectedItemsList.length > 1 ? "these items?" : "this item?" - }`, - positiveText: "Delete", - negativeText: "Cancel", - positivePress: () => { - deleteItems(); - }, - positiveType: "errorShade" - }); - - return; + onPress={() => { + deleteItems(); }} tooltipText="Move to trash" tooltipPosition={1} diff --git a/apps/mobile/app/utils/functions.js b/apps/mobile/app/utils/functions.js index 32a2f0e34..9bc68848d 100644 --- a/apps/mobile/app/utils/functions.js +++ b/apps/mobile/app/utils/functions.js @@ -107,46 +107,43 @@ export const deleteItems = async (item, context) => { if (topics?.length > 0) { const result = await confirmDeleteAllNotes(topics, "topic", context); - if (result.delete) { - for (const topic of topics) { - if (result.deleteNotes) { + if (!result.delete) return; + for (const topic of topics) { + if (result.deleteNotes) { + const notes = db.notebooks + .notebook(topic.notebookId) + .topics.topic(topic.id).all; + await db.notes.delete(...notes.map((note) => note.id)); + } + await db.notebooks.notebook(topic.notebookId).topics.delete(topic.id); + } + useMenuStore.getState().setMenuPins(); + ToastEvent.show({ + heading: `${topics.length > 1 ? "Topics" : "Topic"} deleted`, + type: "success" + }); + } + + if (notebooks?.length > 0) { + const result = await confirmDeleteAllNotes(notebooks, "notebook", context); + if (!result.delete) return; + let ids = notebooks.map((i) => i.id); + if (result.deleteNotes) { + for (let id of ids) { + const notebook = db.notebooks.notebook(id); + const topics = notebook.topics.all; + for (let topic of topics) { const notes = db.notebooks .notebook(topic.notebookId) .topics.topic(topic.id).all; await db.notes.delete(...notes.map((note) => note.id)); } - await db.notebooks.notebook(topic.notebookId).topics.delete(topic.id); + const notes = db.relations.from(notebook.data, "note"); + await db.notes.delete(...notes.map((note) => note.id)); } - useMenuStore.getState().setMenuPins(); - ToastEvent.show({ - heading: `${topics.length > 1 ? "Topics" : "Topic"} deleted`, - type: "success" - }); - } - } - - if (notebooks?.length > 0) { - const result = await confirmDeleteAllNotes(notebooks, "notebook", context); - - if (result.delete) { - let ids = notebooks.map((i) => i.id); - if (result.deleteNotes) { - for (let id of ids) { - const notebook = db.notebooks.notebook(id); - const topics = notebook.topics.all; - for (let topic of topics) { - const notes = db.notebooks - .notebook(topic.notebookId) - .topics.topic(topic.id).all; - await db.notes.delete(...notes.map((note) => note.id)); - } - const notes = db.relations.from(notebook.data, "note"); - await db.notes.delete(...notes.map((note) => note.id)); - } - } - await db.notebooks.delete(...ids); - useMenuStore.getState().setMenuPins(); } + await db.notebooks.delete(...ids); + useMenuStore.getState().setMenuPins(); } Navigation.queueRoutesForUpdate();