Skip to content

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

feat: implement global state for favorites

thecodrrf21573241fbb
diff --git a/apps/web/src/components/listview/index.js b/apps/web/src/components/listview/index.js
index ac7aa7a99..d9d327aa1 100644
--- a/apps/web/src/components/listview/index.js
+++ b/apps/web/src/components/listview/index.js
@@ -5,7 +5,7 @@ import ListItem from "../list-item";
import TimeAgo from "timeago-react";
import ListContainer from "../list-container";
-function ListView({ items, menu, button, onClick }) {
+function ListView({ items, menu, button, onClick, noType }) {
return (
items && (
<ListContainer
@@ -17,16 +17,18 @@ function ListView({ items, menu, button, onClick }) {
title={item.title}
body={item.headline}
index={index}
- onClick={onClick.bind(this, item)} //TODO
+ onClick={onClick && onClick.bind(this, item)}
info={
<Flex justifyContent="center" alignItems="center">
<TimeAgo datetime={item.dateDeleted || item.dateCreated} />
<Text as="span" mx={1}>
•
</Text>
- <Text color="primary">
- {item.type[0].toUpperCase() + item.type.substring(1)}
- </Text>
+ {!noType && (
+ <Text color="primary">
+ {item.type[0].toUpperCase() + item.type.substring(1)}
+ </Text>
+ )}
</Flex>
}
menuData={item}
diff --git a/apps/web/src/components/notebook/index.js b/apps/web/src/components/notebook/index.js
index 8cc6052f8..511d09df7 100644
--- a/apps/web/src/components/notebook/index.js
+++ b/apps/web/src/components/notebook/index.js
@@ -10,10 +10,6 @@ const menuItems = (notebook, index) => [
title: notebook.pinned ? "Unpin" : "Pin",
onClick: () => store.getState().pin(notebook, index)
},
- {
- title: notebook.favorite ? "Unfavorite" : "Favorite",
- onClick: () => store.getState().favorite(notebook, index)
- },
{ title: "Edit" },
{ title: "Share" },
{
diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.js
index d39f95f20..912d11e45 100644
--- a/apps/web/src/stores/editor-store.js
+++ b/apps/web/src/stores/editor-store.js
@@ -1,5 +1,5 @@
import createStore from "../common/store";
-import { store as notestore } from "./note-store";
+import { store as notestore, LIST_TYPES } from "./note-store";
import { db } from "../common";
const SESSION_STATES = {
@@ -47,7 +47,7 @@ function editorStore(set, get) {
};
});
},
- saveSession: function() {
+ saveSession: function(oldSession) {
const { session } = get();
const { title, id, content, pinned, favorite } = session;
let note = {
@@ -60,17 +60,23 @@ function editorStore(set, get) {
db.notes.add(note).then(id => {
set(state => {
state.session.id = id;
- notestore.getState().init();
+ notestore.getState().refresh();
+
+ // we update favorites only if favorites has changed
+ if (!oldSession || oldSession.favorite !== session.favorite) {
+ notestore.getState().refreshList(LIST_TYPES.fav);
+ }
});
});
},
setSession: function(session) {
+ const oldSession = get().session;
set(state => {
state.session.state = SESSION_STATES.stale;
session(state);
clearTimeout(state.session.timeout);
state.session.timeout = setTimeout(() => {
- get().saveSession();
+ get().saveSession(oldSession);
}, 500);
});
},
diff --git a/apps/web/src/stores/note-store.js b/apps/web/src/stores/note-store.js
index 5c65e1858..938c8f8b2 100644
--- a/apps/web/src/stores/note-store.js
+++ b/apps/web/src/stores/note-store.js
@@ -1,18 +1,29 @@
import { db } from "../common/index";
import createStore from "../common/store";
+import { store as trashStore } from "./trash-store";
+
+const LIST_TYPES = {
+ fav: "favorites"
+};
function noteStore(set) {
return {
- init: function() {
+ notes: {
+ items: [],
+ groupCounts: [],
+ groups: []
+ },
+ favorites: [],
+ refresh: function() {
set(state => {
//TODO save group type
state.notes = db.notes.group(undefined, true);
});
},
- notes: {
- items: [],
- groupCounts: [],
- groups: []
+ refreshList: function(listType) {
+ set(state => {
+ state[listType] = db.notes[listType];
+ });
},
delete: async function(id, info) {
await db.notes.delete(id);
@@ -23,6 +34,7 @@ function noteStore(set) {
state.notes.groups.splice(info.groupIndex, 1);
state.notes.groupCounts.splice(info.groupIndex, 1);
}
+ trashStore.getState().refresh();
});
},
pin: async function(note) {
@@ -34,7 +46,12 @@ function noteStore(set) {
favorite: async function(note, index) {
await db.notes.note(note).favorite();
set(state => {
+ if (index < 0) {
+ index = state.notes.findIndex(n => n.id === note.id);
+ if (index < 0) return;
+ }
state.notes.items[index].favorite = !note.favorite;
+ state.initList("favorites");
});
}
};
@@ -42,4 +59,4 @@ function noteStore(set) {
const [useStore, store] = createStore(noteStore);
-export { useStore, store };
+export { useStore, store, LIST_TYPES };
diff --git a/apps/web/src/stores/notebook-store.js b/apps/web/src/stores/notebook-store.js
index 141be3154..fbeeac22c 100644
--- a/apps/web/src/stores/notebook-store.js
+++ b/apps/web/src/stores/notebook-store.js
@@ -1,9 +1,10 @@
import { db } from "../common/index";
import createStore from "../common/store";
+import { store as trashStore } from "./trash-store";
function notebookStore(set) {
return {
- init: function() {
+ refresh: function() {
set(state => {
state.notebooks = db.notebooks.all;
});
@@ -19,16 +20,13 @@ function notebookStore(set) {
await db.notebooks.delete(id);
set(state => {
state.notebooks.splice(index, 1);
+ trashStore.getState().refresh();
});
},
update: function() {},
pin: async function(notebook, index) {
await db.notebooks.notebook(notebook).pin();
set(state => (state.notebooks[index].pinned = !notebook.pinned));
- },
- favorite: async function(notebook, index) {
- await db.notebooks.notebook(notebook).favorite();
- set(state => (state.notebooks[index].favorite = !notebook.favorite));
}
};
}
diff --git a/apps/web/src/views/Favorites.js b/apps/web/src/views/Favorites.js
index 8a7af2481..0aa9e5e50 100644
--- a/apps/web/src/views/Favorites.js
+++ b/apps/web/src/views/Favorites.js
@@ -1,63 +1,24 @@
-import React from "react";
-import { db, ev } from "../common";
-import * as Icon from "react-feather";
+import React, { useEffect } from "react";
+import { ev } from "../common";
import ListView from "../components/listview";
-import { showSnack } from "../components/snackbar";
-import { confirm } from "../components/dialogs/confirm";
+import { useStore, store, LIST_TYPES } from "../stores/note-store";
const dropdownRefs = [];
const menuItems = item => [
{
title: "Unfavorite",
- onClick: async () => {
- confirm(
- Icon.Star,
- "Unfavorite",
- "Are you sure you want to remove this item from favorites?"
- ).then(res => {
- if (res) {
- (item.type === "notes"
- ? db.notes.note(item.id)
- : db.notebooks.notebook(item.id)
- )
- .favorite()
- .then(() => {
- let itemType = item.type[0] + item.type.substring(1);
- showSnack(itemType + " Unfavorited!", Icon.Check);
- ev.emit(`refreshFavorites`);
- });
- }
- });
- }
- },
- {
- title: "Delete",
- color: "red",
- onClick: async () => {
- confirm(
- Icon.Trash2,
- "Delete",
- "Are you sure you want to delete this note? It will be moved to trash and permanently deleted after 7 days."
- ).then(res => {
- if (res) {
- let itemType = item.type[0] + item.type.substring(1);
- (itemType === "notes" ? db.notes : db.notebooks)
- .delete(item.id)
- .then(() => {
- showSnack(itemType + " Deleted!", Icon.Trash);
- ev.emit(`refreshFavorites`);
- });
- }
- });
- }
+ onClick: () => store.getState().favorite(item)
}
];
function Favorites() {
+ useEffect(() => store.getState().refreshList(LIST_TYPES.fav), []);
+ const items = useStore(store => store.favorites);
return (
<ListView
type="Favorites"
- getItems={db.notes.favorites}
+ items={items}
menu={{ menuItems, dropdownRefs }}
+ noType
button={undefined}
onClick={item => {
if (item.type === "note") {
diff --git a/apps/web/src/views/Home.js b/apps/web/src/views/Home.js
index 9da278941..7cd85ffd9 100644
--- a/apps/web/src/views/Home.js
+++ b/apps/web/src/views/Home.js
@@ -26,7 +26,7 @@ function SearchBox(props) {
}
function Home() {
- useEffect(() => store.getState().init(), []);
+ useEffect(() => store.getState().refresh(), []);
const notes = useStore(store => store.notes);
const newSession = useEditorStore(store => store.newSession);
return (
diff --git a/apps/web/src/views/Notebooks.js b/apps/web/src/views/Notebooks.js
index eb8165043..dbcf23269 100644
--- a/apps/web/src/views/Notebooks.js
+++ b/apps/web/src/views/Notebooks.js
@@ -8,7 +8,7 @@ import { useStore, store } from "../stores/notebook-store";
const Notebooks = props => {
const [open, setOpen] = useState(false);
- useEffect(() => store.getState().init(), []);
+ useEffect(() => store.getState().refresh(), []);
const notebooks = useStore(state => state.notebooks);
const add = useStore(state => state.add);