Skip to content

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

feat: move trash logic to global state

thecodrr0deb7954514d
diff --git a/apps/web/src/components/listview/index.js b/apps/web/src/components/listview/index.js
index 4fafbf46d..8ad122985 100644
--- a/apps/web/src/components/listview/index.js
+++ b/apps/web/src/components/listview/index.js
@@ -5,19 +5,7 @@ import ListItem from "../list-item";
import TimeAgo from "timeago-react";
import ListContainer from "../list-container";
-function ListView({ type, getItems, menu, button, onClick }) {
- const [items, setItems] = useState([]);
-
- useEffect(() => {
- function onRefreshItems() {
- setItems(getItems);
- }
- onRefreshItems();
- ev.addListener(`refresh${type}`, onRefreshItems);
- return () => {
- ev.removeListener(`refresh${type}`, onRefreshItems);
- };
- }, [getItems, type]);
+function ListView({ items, menu, button, onClick }) {
return (
items && (
<ListContainer
diff --git a/apps/web/src/stores/trash-store.js b/apps/web/src/stores/trash-store.js
new file mode 100644
index 000000000..dbc47a8ea
--- /dev/null
+++ b/apps/web/src/stores/trash-store.js
@@ -0,0 +1,38 @@
+import { db } from "../common/index";
+import createStore from "../common/store";
+
+function trashStore(set) {
+ return {
+ init: function() {
+ set(state => {
+ state.trash = db.trash.all;
+ });
+ },
+ trash: [],
+ delete: function(id, index) {
+ return db.trash.delete(id).then(() => {
+ set(state => {
+ state.trash.splice(index, 1);
+ });
+ });
+ },
+ restore: function(id, index) {
+ return db.trash.restore(id).then(() => {
+ set(state => {
+ state.trash.splice(index, 1);
+ });
+ });
+ },
+ clear: function() {
+ return db.trash.clear().then(() => {
+ set(state => {
+ state.trash = [];
+ });
+ });
+ }
+ };
+}
+
+const [useStore, store] = createStore(trashStore);
+
+export { useStore, store };
diff --git a/apps/web/src/views/Trash.js b/apps/web/src/views/Trash.js
index 6b2894e0c..94cd92209 100644
--- a/apps/web/src/views/Trash.js
+++ b/apps/web/src/views/Trash.js
@@ -1,43 +1,26 @@
import React from "react";
-import { db, ev } from "../common";
import * as Icon from "react-feather";
import ListView from "../components/listview";
import { confirm } from "../components/dialogs/confirm";
-import { showSnack } from "../components/snackbar";
+import { useStore, store } from "../stores/trash-store";
const dropdownRefs = [];
-const menuItems = item => [
+const menuItems = (item, index) => [
{
title: "Restore",
- onClick: async () => {
- confirm(
- Icon.Star,
- "Restore",
- `Are you sure you want to restore this item to ${item.type}?`
- ).then(async res => {
- if (res) {
- let itemType = item.type[0] + item.type.substring(1);
- showSnack(itemType + " Restored", Icon.Check);
- await db.trash.restore(item.id);
- ev.emit(`refreshTrash`);
- }
- });
- }
+ onClick: () => store.getState().restore(item, index)
},
{
title: "Delete",
color: "red",
- onClick: async () => {
+ onClick: () => {
confirm(
- Icon.Star,
+ Icon.Trash2,
"Delete",
`Are you sure you want to permanently delete this item?`
).then(async res => {
if (res) {
- let itemType = item.type[0] + item.type.substring(1);
- showSnack(itemType + "Permanently Deleted!", Icon.Trash2);
- await db.trash.delete(item.id);
- ev.emit(`refreshTrash`);
+ await store.getState().delete(item, index);
}
});
}
@@ -45,17 +28,28 @@ const menuItems = item => [
];
function Trash() {
+ const items = useStore(store => store.trash);
+ const clearTrash = useStore(store => store.clear);
return (
<ListView
type="Trash"
- getItems={db.trash.all}
+ getItems={items}
menu={{ menuItems, dropdownRefs }}
button={{
content: "Clear Trash",
icon: Icon.Trash2,
- onClick: async () => await db.trash.clear()
+ onClick: function() {
+ confirm(
+ Icon.Trash2,
+ "Clear",
+ `This action is irreversible. Are you sure you want to proceed?s`
+ ).then(async res => {
+ if (res) {
+ await clearTrash();
+ }
+ });
+ }
}}
- onClick={item => {}}
/>
);
}