From 940e2295c84cbb4aa760ecd7893c7bdd0ae24100 Mon Sep 17 00:00:00 2001 From: thecodrr Date: Mon, 14 Sep 2020 11:59:44 +0500 Subject: [PATCH] feat: add toast notification for add/move note to notebook --- apps/web/src/common/toasts.js | 29 +++++++++++++++++++ .../components/dialogs/move-note-dialog.js | 14 ++++++--- apps/web/src/components/note/index.js | 4 +-- 3 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 apps/web/src/common/toasts.js diff --git a/apps/web/src/common/toasts.js b/apps/web/src/common/toasts.js new file mode 100644 index 000000000..53a2731d5 --- /dev/null +++ b/apps/web/src/common/toasts.js @@ -0,0 +1,29 @@ +import { db } from "."; +import { showToast } from "../utils/toast"; + +function showNotesMovedToast(note, noteIds, notebook) { + let actions; + + const newNotebook = db.notebooks.notebook(notebook.id); + let verb = + noteIds.length > 1 ? "added" : note.notebook?.id ? "moved" : "added"; + let messageText = noteIds.length > 1 ? `${noteIds.length} notes` : "Note"; + messageText += ` ${verb} to "${newNotebook.title}"`; + + if (noteIds.length === 1) { + const undoAction = async () => { + if (note.notebook?.id) { + db.notes.move(note.notebook, note.id); + const notebook = db.notebooks.notebook(note.notebook.id); + showToast("success", `Note moved back to "${notebook.title}"`); + } else { + await newNotebook.topics.topic(notebook.topic).delete(note.id); + showToast("success", `Note removed from "${newNotebook.title}"`); + } + }; + actions = [{ text: "Undo", onClick: undoAction }]; + } + showToast("success", messageText, actions); +} + +export { showNotesMovedToast }; diff --git a/apps/web/src/components/dialogs/move-note-dialog.js b/apps/web/src/components/dialogs/move-note-dialog.js index faedcec24..d45ac5a72 100644 --- a/apps/web/src/components/dialogs/move-note-dialog.js +++ b/apps/web/src/components/dialogs/move-note-dialog.js @@ -5,6 +5,8 @@ import * as Icon from "../icons"; import { db } from "../../common"; import Dialog, { showDialog } from "./dialog"; import { toTitleCase } from "../../utils/string"; +import { showNotesMovedToast } from "../../common/toasts"; +import { showToast } from "../../utils/toast"; class MoveDialog extends React.Component { history = []; @@ -40,12 +42,16 @@ class MoveDialog extends React.Component { text: "Move", onClick: async () => { try { - await db.notes.move( - { id: this.selectedNotebook.id, topic: this.selectedTopic }, - ...props.noteIds - ); + const notebook = { + id: this.selectedNotebook.id, + topic: this.selectedTopic, + }; + const note = db.notes.note(props.noteIds[0]).data; + await db.notes.move(notebook, ...props.noteIds); + showNotesMovedToast(note, props.noteIds, notebook); props.onMove(); } catch (e) { + showToast("error", e.message); console.error(e); } finally { props.onClose(); diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.js index 46441a401..1aaa18ce1 100644 --- a/apps/web/src/components/note/index.js +++ b/apps/web/src/components/note/index.js @@ -20,9 +20,7 @@ function menuItems(note, context) { { title: note.notebook?.id ? "Move" : "Add to", onClick: async () => { - if (await showMoveNoteDialog([note.id])) { - console.log("Note moved successfully!"); - } + await showMoveNoteDialog([note.id]); }, }, {