Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

refactor: cleanup all toast related code

thecodrrd01107525242
diff --git a/apps/web/src/common/toasts.js b/apps/web/src/common/toasts.js
index 1e395fe3a..dd0cea632 100644
--- a/apps/web/src/common/toasts.js
+++ b/apps/web/src/common/toasts.js
@@ -30,30 +30,6 @@ function showNotesMovedToast(note, noteIds, notebook) {
var toast = showToast("success", messageText, actions);
}
-function showNoteDeleted(note) {
- const undoAction = () => {
- let deletedNotes = db.trash.all;
- deletedNotes.forEach(async (element) => {
- if (element.itemId === note) {
- await db.trash.restore(element.id);
- notestore.refresh();
- }
- });
- };
-
- let actions = [{ text: "Undo", onClick: undoAction }];
-
- showToast("success", "Note Deleted", actions);
-}
-
-function showNoteColored(colors, label) {
- if (colors.includes(label)) {
- showToast("success", "Untagged as " + label);
- } else {
- showToast("success", "Tagged as " + label);
- }
-}
-
async function showUnpinnedToast(itemId, itemType) {
const noun = itemType === "note" ? "Note" : "Notebook";
const messageText = `${noun} unpinned successfully!`;
@@ -66,24 +42,17 @@ async function showUnpinnedToast(itemId, itemType) {
var toast = showToast("success", messageText, actions);
}
-function showNoteBookDeleted(notebook) {
- const undoAction = () => {
- let deletedNotes = db.trash.all;
- deletedNotes.forEach(async (element) => {
- if (element.itemId === notebook) {
- await db.trash.restore(element.id);
- nbstore.refresh();
- }
- });
+function showItemDeletedToast(item) {
+ const noun = item.type === "note" ? "Note" : "Notebook";
+ const messageText = `${noun} deleted successfully!`;
+ const undoAction = async () => {
+ toast.hide();
+ let trashItem = db.trash.all.find((i) => i.itemId === item.id);
+ await db.trash.restore(trashItem.id);
+ nbstore.refresh();
};
-
let actions = [{ text: "Undo", onClick: undoAction }];
-
- showToast("success", "Notebook Deleted", actions);
-}
-
-function showTrashItemRestored() {
- showToast("success", "Item restored");
+ var toast = showToast("success", messageText, actions);
}
async function showPermanentDeleteToast(item, index) {
@@ -105,10 +74,7 @@ async function showPermanentDeleteToast(item, index) {
export {
showNotesMovedToast,
- showNoteDeleted,
- showNoteColored,
showUnpinnedToast,
- showNoteBookDeleted,
- showTrashItemRestored,
+ showItemDeletedToast,
showPermanentDeleteToast,
};
diff --git a/apps/web/src/components/menu/colors.js b/apps/web/src/components/menu/colors.js
index 22013ff9b..bf3729848 100644
--- a/apps/web/src/components/menu/colors.js
+++ b/apps/web/src/components/menu/colors.js
@@ -4,7 +4,6 @@ import * as Icon from "../icons/index";
import { objectMap } from "../../utils/object";
import { useStore } from "../../stores/note-store";
import { Flex } from "rebass";
-import { showNoteColored } from "../../common/toasts";
function Colors(props) {
const { id, colors } = props.data;
@@ -15,10 +14,7 @@ function Colors(props) {
{objectMap(COLORS, (label, code) => (
<Flex
sx={{ position: "relative" }}
- onClick={() => {
- setColor(id, label);
- showNoteColored(colors, label);
- }}
+ onClick={() => setColor(id, label)}
key={label}
>
<Icon.Circle
diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.js
index 609c2c98f..bd4c4a6a8 100644
--- a/apps/web/src/components/note/index.js
+++ b/apps/web/src/components/note/index.js
@@ -13,7 +13,7 @@ import { useTheme } from "emotion-theming";
import Colors from "../menu/colors";
import { showExportDialog } from "../dialogs/exportdialog";
import { setHashParam } from "../../utils/useHashParam";
-import { showNoteDeleted } from "../../common/toasts";
+import { showItemDeletedToast } from "../../common/toasts";
import { showUnpinnedToast } from "../../common/toasts";
import { showToast } from "../../utils/toast";
@@ -75,7 +75,7 @@ function menuItems(note, context) {
.notebook(context.notebook.id)
.topics.topic(context.value)
.delete(note.id);
- await store.setContext(context);
+ store.setContext(context);
}
});
},
@@ -99,7 +99,7 @@ function menuItems(note, context) {
"Are you sure you want to delete this note?"
).then(async (res) => {
if (res) {
- await store.delete(note.id).then(showNoteDeleted(note.id));
+ await store.delete(note.id).then(() => showItemDeletedToast(note));
}
});
},
diff --git a/apps/web/src/components/notebook/index.js b/apps/web/src/components/notebook/index.js
index 9fe6f5043..5f933d897 100644
--- a/apps/web/src/components/notebook/index.js
+++ b/apps/web/src/components/notebook/index.js
@@ -5,8 +5,7 @@ import { store } from "../../stores/notebook-store";
import * as Icon from "../icons";
import { showEditNoteDialog } from "../dialogs/addnotebookdialog";
import { confirm } from "../dialogs/confirm";
-import { showUnpinnedToast } from "../../common/toasts";
-import { showNoteBookDeleted } from "../../common/toasts";
+import { showItemDeletedToast, showUnpinnedToast } from "../../common/toasts";
function menuItems(notebook, index) {
return [
@@ -33,7 +32,7 @@ function menuItems(notebook, index) {
if (res) {
await store
.delete(notebook.id, index)
- .then(showNoteBookDeleted(notebook.id));
+ .then(() => showItemDeletedToast(notebook));
}
});
},
diff --git a/apps/web/src/views/trash.js b/apps/web/src/views/trash.js
index ef8edb698..ad9daf2ef 100644
--- a/apps/web/src/views/trash.js
+++ b/apps/web/src/views/trash.js
@@ -9,10 +9,7 @@ import { useStore, store } from "../stores/trash-store";
import { toTitleCase } from "../utils/string";
import TrashPlaceholder from "../components/placeholders/trash-placeholder";
import { showToast } from "../utils/toast";
-import {
- showTrashItemRestored,
- showPermanentDeleteToast,
-} from "../common/toasts";
+import { showPermanentDeleteToast } from "../common/toasts";
function menuItems(item, index) {
return [
@@ -20,7 +17,9 @@ function menuItems(item, index) {
title: "Restore",
onClick: () => {
store.restore(item.id, index);
- showTrashItemRestored();
+ showToast(
+ `${item.type === "note" ? "Note" : "Notebook"} restored successfully!`
+ );
},
},
{