feat: add toast notification for add/move note to notebook

This commit is contained in:
thecodrr
2020-09-14 11:59:44 +05:00
parent 07d7658c80
commit 940e2295c8
3 changed files with 40 additions and 7 deletions

View File

@@ -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 };

View File

@@ -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();

View File

@@ -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]);
},
},
{