Skip to content

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

feat: make notes logic stateful

thecodrr86e0625a739f
diff --git a/apps/web/src/components/dialogs/confirm.js b/apps/web/src/components/dialogs/confirm.js
index 41c65322e..6a2310217 100644
--- a/apps/web/src/components/dialogs/confirm.js
+++ b/apps/web/src/components/dialogs/confirm.js
@@ -5,7 +5,7 @@ import Dialog, { showDialog } from "./dialog";
function Confirm(props) {
return (
<Dialog
- open={true}
+ isOpen={true}
title={props.title}
icon={props.icon}
content={
diff --git a/apps/web/src/components/dialogs/logindialog.js b/apps/web/src/components/dialogs/logindialog.js
index 477ca2c1d..1bd932355 100644
--- a/apps/web/src/components/dialogs/logindialog.js
+++ b/apps/web/src/components/dialogs/logindialog.js
@@ -6,7 +6,7 @@ import Dialog, { showDialog } from "./dialog";
const LoginDialog = props => (
<Dialog
- open={true}
+ isOpen={true}
title={"Login"}
icon={Icon.LogIn}
onCloseClick={props.onClose}
diff --git a/apps/web/src/components/dialogs/move-note-dialog.js b/apps/web/src/components/dialogs/move-note-dialog.js
index 72adba520..c1484216e 100644
--- a/apps/web/src/components/dialogs/move-note-dialog.js
+++ b/apps/web/src/components/dialogs/move-note-dialog.js
@@ -22,7 +22,7 @@ export default class MoveDialog extends React.Component {
const props = this.props;
return (
<Dialog
- open={true}
+ isOpen={true}
title={"Move Note"}
icon={Icon.Move}
content={
diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.js
index b4db57e47..a1e16d177 100644
--- a/apps/web/src/components/note/index.js
+++ b/apps/web/src/components/note/index.js
@@ -7,9 +7,10 @@ import { showSnack } from "../snackbar";
import ListItem from "../list-item";
import { confirm } from "../dialogs/confirm";
import { showMoveNoteDialog } from "../dialogs/movenotedialog";
+import { store } from "../../stores/note-store";
const dropdownRefs = [];
-const menuItems = note => [
+const menuItems = (note, index, groupIndex) => [
{
title: note.notebook ? "Move" : "Add to",
onClick: async () => {
@@ -20,25 +21,11 @@ const menuItems = note => [
},
{
title: note.pinned ? "Unpin" : "Pin",
- onClick: async () =>
- db.notes
- .note(note.id)
- .pin()
- .then(() => {
- showSnack("Note pinned!", Icon.Check);
- ev.emit("refreshNotes");
- })
+ onClick: () => store.getState().pin(note, index)
},
{
title: note.favorite ? "Unfavorite" : "Favorite",
- onClick: async () =>
- db.notes
- .note(note.id)
- .favorite()
- .then(() => {
- showSnack("Note favorited!", Icon.Check);
- ev.emit("refreshNotes");
- })
+ onClick: () => store.getState().favorite(note, index)
},
{ title: "Edit" },
{ title: note.locked ? "Remove lock" : "Lock" }, //TODO
@@ -51,9 +38,10 @@ const menuItems = note => [
Icon.Trash2,
"Delete",
"Are you sure you want to delete this note?"
- ).then(res => {
+ ).then(async res => {
if (res) {
- ev.emit("onClearNote", note.id);
+ await store.getState().delete(note.id, { index, groupIndex });
+ /* ev.emit("onClearNote", note.id);
db.notes
.delete(note.id)
.then(
@@ -63,7 +51,7 @@ const menuItems = note => [
ev.emit("refreshNotes");
}
)
- .catch(console.log);
+ .catch(console.log); */
}
});
}
@@ -74,7 +62,7 @@ function sendOpenNoteEvent(note) {
ev.emit("onOpenNote", note);
}
-const Note = ({ item, index }) => {
+const Note = ({ item, index, groupIndex }) => {
const note = item;
return note ? (
<ListItem
@@ -91,7 +79,7 @@ const Note = ({ item, index }) => {
}
pinned={note.pinned}
menuData={note}
- menuItems={menuItems(note)}
+ menuItems={menuItems(note, index, groupIndex)}
dropdownRefs={dropdownRefs}
/>
) : null;
diff --git a/apps/web/src/stores/note-store.js b/apps/web/src/stores/note-store.js
new file mode 100644
index 000000000..ac6a10897
--- /dev/null
+++ b/apps/web/src/stores/note-store.js
@@ -0,0 +1,45 @@
+import { db } from "../common/index";
+import createStore from "../common/store";
+
+function noteStore(set) {
+ return {
+ init: function() {
+ set(state => {
+ //TODO save group type
+ state.notes = db.notes.group(undefined, true);
+ });
+ },
+ notes: {
+ items: [],
+ groupCounts: [],
+ groups: []
+ },
+ delete: async function(id, info) {
+ await db.notes.delete(id);
+ set(state => {
+ state.notes.items.splice(info.index, 1);
+ state.notes.groupCounts[info.groupIndex]--;
+ if (state.notes.groupCounts[info.groupIndex] <= 0) {
+ state.notes.groups.splice(info.groupIndex, 1);
+ state.notes.groupCounts.splice(info.groupIndex, 1);
+ }
+ });
+ },
+ pin: async function(note, index) {
+ await db.notes.note(note).pin();
+ set(state => {
+ state.notes = db.notes.group(undefined, true);
+ });
+ },
+ favorite: async function(note, index) {
+ await db.notes.note(note).favorite();
+ set(state => {
+ state.notes.items[index].favorite = !note.favorite;
+ });
+ }
+ };
+}
+
+const [useStore, store] = createStore(noteStore);
+
+export { useStore, store };
diff --git a/apps/web/src/views/Home.js b/apps/web/src/views/Home.js
index 269ca6f1a..0be476bcf 100644
--- a/apps/web/src/views/Home.js
+++ b/apps/web/src/views/Home.js
@@ -1,11 +1,12 @@
import React, { useEffect, useState } from "react";
import { Flex, Text, Box } from "rebass";
import * as Icon from "react-feather";
-import { db, ev, sendNewNoteEvent } from "../common";
+import { db, sendNewNoteEvent } from "../common";
import { GroupedVirtuoso as GroupList } from "react-virtuoso";
import Button from "../components/button";
import Search from "../components/search";
import Note from "../components/note";
+import { useStore, store } from "../stores/note-store";
function SearchBox(props) {
return (
@@ -24,25 +25,11 @@ function SearchBox(props) {
}
function Home() {
- const [notes, setNotes] = useState({
- items: [],
- groupCounts: [],
- groups: []
- });
- useEffect(() => {
- function onRefreshNotes() {
- let groups = db.notes.group(undefined, true);
- setNotes(groups);
- }
- onRefreshNotes();
- ev.addListener("refreshNotes", onRefreshNotes);
- return () => {
- ev.removeListener("refreshNotes", onRefreshNotes);
- };
- }, []);
+ useEffect(() => store.getState().init(), []);
+ const notes = useStore(store => store.notes);
return (
<Flex flexDirection="column" flex="1 1 auto">
- <SearchBox setNotes={setNotes} />
+ <SearchBox />
<GroupList
style={{
width: "100%",
@@ -63,7 +50,11 @@ function Home() {
)
}
item={(index, groupIndex) => (
- <Note index={index} item={notes.items[index]} />
+ <Note
+ index={index}
+ groupIndex={groupIndex}
+ item={notes.items[index]}
+ />
)}
/>
<Button
diff --git a/apps/web/yarn.lock b/apps/web/yarn.lock
index 6d9a6e4fa..9fd8ab4cb 100644
--- a/apps/web/yarn.lock
+++ b/apps/web/yarn.lock
@@ -7025,7 +7025,7 @@ normalize-url@^3.0.0, normalize-url@^3.0.1:
"notes-core@https://github.com/thecodrr/notes-core.git":
version "1.1.0"
- resolved "https://github.com/thecodrr/notes-core.git#feadc3ac93504309afa35e42c4e225fadacb3ee7"
+ resolved "https://github.com/thecodrr/notes-core.git#47caff445840c830a29818f3a666f40e1e361684"
dependencies:
fast-sort "^2.0.1"
fuzzysearch "^1.0.3"