From e500b7c3f8cbf1caaa3a69a7c84d2ef297f78ecf Mon Sep 17 00:00:00 2001 From: thecodrr Date: Mon, 13 Sep 2021 10:24:16 +0500 Subject: [PATCH] fix: nb cannot be unpinned if 3 nbs are pinned --- apps/web/src/components/note/index.js | 10 +++++++--- apps/web/src/components/notebook/index.js | 11 ++++++++--- apps/web/src/stores/app-store.js | 6 ++++-- apps/web/src/stores/note-store.js | 5 +---- apps/web/src/stores/notebook-store.js | 9 ++++----- apps/web/src/stores/user-store.js | 2 -- 6 files changed, 24 insertions(+), 19 deletions(-) diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.js index ab5ce4965..f5d88ff95 100644 --- a/apps/web/src/components/note/index.js +++ b/apps/web/src/components/note/index.js @@ -155,9 +155,13 @@ export default React.memo(Note, function (prevProps, nextProps) { ); }); -const pin = async (note) => { - await store.pin(note.id); - if (note.pinned) await showUnpinnedToast(note.id, "note"); +const pin = (note) => { + return store + .pin(note.id) + .then(async () => { + if (note.pinned) await showUnpinnedToast(note.id, "note"); + }) + .catch((error) => showToast("error", error.message)); }; const menuItems = [ diff --git a/apps/web/src/components/notebook/index.js b/apps/web/src/components/notebook/index.js index 422a4aca8..3a099bc1e 100644 --- a/apps/web/src/components/notebook/index.js +++ b/apps/web/src/components/notebook/index.js @@ -9,6 +9,7 @@ import * as Icon from "../icons"; import { hashNavigate, navigate } from "../../navigation"; import { getTotalNotes } from "../../common"; import IconTag from "../icon-tag"; +import { showToast } from "../../utils/toast"; function Notebook(props) { const { item, index } = props; @@ -83,9 +84,13 @@ export default React.memo(Notebook, (prev, next) => { ); }); -const pin = async (notebook) => { - await store.pin(notebook); - if (notebook.pinned) showUnpinnedToast(notebook.id, "notebook"); +const pin = (notebook) => { + return store + .pin(notebook.id) + .then(() => { + if (notebook.pinned) showUnpinnedToast(notebook.id, "notebook"); + }) + .catch((error) => showToast("error", error.message)); }; const menuItems = [ diff --git a/apps/web/src/stores/app-store.js b/apps/web/src/stores/app-store.js index 09fa6006e..6a5525eb1 100644 --- a/apps/web/src/stores/app-store.js +++ b/apps/web/src/stores/app-store.js @@ -141,8 +141,10 @@ class AppStore extends BaseStore { this.set((state) => (state.isSyncing = true)); return db .sync(full, force) - .then(async () => { - showToast("Sync completed."); + .then(async (result) => { + if (!result) return; + + showToast("success", "Sync completed."); await this.updateLastSynced(); return await this.refresh(); }) diff --git a/apps/web/src/stores/note-store.js b/apps/web/src/stores/note-store.js index 7f7352a8d..7f5cdfda7 100644 --- a/apps/web/src/stores/note-store.js +++ b/apps/web/src/stores/note-store.js @@ -7,7 +7,6 @@ import Vault from "../common/vault"; import BaseStore from "."; import { EV, EVENTS } from "notes-core/common"; import Config from "../utils/config"; -import { showToast } from "../utils/toast"; import { qclone } from "qclone"; import { hashNavigate } from "../navigation"; import { groupArray } from "notes-core/utils/grouping"; @@ -81,11 +80,9 @@ class NoteStore extends BaseStore { }; pin = async (id) => { - // TODO (hack) we probably shouldn't do this here. const note = db.notes.note(id); if (!note.data.pinned && db.notes.pinned.length >= 3) { - await showToast("error", "You cannot pin more than 3 notes."); - return; + throw new Error("You cannot pin more than 3 notes."); } if (!this._syncEditor(note.id, "pinned", !note.data.pinned)) { await note.pin(); diff --git a/apps/web/src/stores/notebook-store.js b/apps/web/src/stores/notebook-store.js index d1cb8cba5..af42c7f6b 100644 --- a/apps/web/src/stores/notebook-store.js +++ b/apps/web/src/stores/notebook-store.js @@ -3,7 +3,6 @@ import createStore from "../common/store"; import { store as appStore } from "./app-store"; import { store as noteStore } from "./note-store"; import BaseStore from "./index"; -import { showToast } from "../utils/toast"; import { groupArray } from "notes-core/utils/grouping"; import Config from "../utils/config"; @@ -36,11 +35,11 @@ class NotebookStore extends BaseStore { }; pin = async (notebookId) => { - // TODO (hack) We probably shouldn't do this here. - if (db.notebooks.pinned.length >= 3) { - return await showToast("error", "You cannot pin more than 3 notebooks."); + const notebook = db.notebooks.notebook(notebookId); + if (!notebook._notebook.pinned && db.notebooks.pinned.length >= 3) { + throw new Error("You cannot pin more than 3 notebooks."); } - await db.notebooks.notebook(notebookId).pin(); + await notebook.pin(); this.refresh(); }; diff --git a/apps/web/src/stores/user-store.js b/apps/web/src/stores/user-store.js index 45e25679d..67c61d9e0 100644 --- a/apps/web/src/stores/user-store.js +++ b/apps/web/src/stores/user-store.js @@ -10,7 +10,6 @@ import { showSessionExpiredDialog, } from "../common/dialog-controller"; import { Text } from "rebass"; -import { showToast } from "../utils/toast"; import { showAccountLoggedOutNotice } from "../common/dialog-controller"; import Config from "../utils/config"; import { onPageVisibilityChanged } from "../utils/page-visibility"; @@ -64,7 +63,6 @@ class UserStore extends BaseStore { this.set((state) => (state.user.subscription = subscription)); }); EV.subscribe(EVENTS.userEmailConfirmed, async () => { - showToast("success", "Email confirmed successfully!"); window.location.reload(); });