Files
notesnook/apps/web/src/views/Notebooks.js

88 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-12-05 17:48:54 +05:00
import React, { useState, useEffect } from "react";
import { Flex } from "rebass";
2020-01-03 16:34:41 +05:00
import { db, ev } from "../common";
2019-12-05 17:48:54 +05:00
import { showSnack } from "../components/snackbar";
import Notebook from "../components/notebook";
import { CreateNotebookDialog, ask } from "../components/dialogs";
import ListContainer from "../components/list-container";
2019-12-03 18:09:31 +05:00
2020-01-09 23:28:13 +05:00
const Notebooks = props => {
2019-12-03 18:09:31 +05:00
const [open, setOpen] = useState(false);
2019-12-05 17:48:54 +05:00
const [notebooks, setNotebooks] = useState([]);
useEffect(() => {
2020-01-03 16:34:41 +05:00
function onRefresh() {
2019-12-05 17:48:54 +05:00
setNotebooks(db.getNotebooks());
2020-01-03 16:34:41 +05:00
}
onRefresh();
ev.addListener("refreshNotebooks", onRefresh);
2019-12-05 17:48:54 +05:00
return () => {
2020-01-03 16:34:41 +05:00
ev.removeListener("refreshNotebooks", onRefresh);
2019-12-05 17:48:54 +05:00
Notebooks.onRefresh = undefined;
};
2020-01-09 15:42:15 +05:00
}, []);
2020-01-02 17:26:14 +05:00
2019-12-03 18:09:31 +05:00
return (
<>
<ListContainer
itemsLength={notebooks.length}
item={index => (
<Notebook
index={index}
item={notebooks[index]}
onClick={() => {
2020-01-09 23:28:13 +05:00
props.navigator.navigate("topics", {
title: notebooks[index].title,
topics: notebooks[index].topics,
2020-01-09 23:28:13 +05:00
notebook: notebooks[index]
});
}}
2020-01-09 23:28:13 +05:00
onTopicClick={(notebook, topic) =>
props.navigator.navigate("notes", {
title: notebook.title,
subtitle: topic.title,
notes: db.getTopic(notebook.dateCreated, topic.title)
})
}
/>
)}
button={{
content: "Create a notebook",
onClick: async () => setOpen(true)
}}
/>
2019-12-05 17:48:54 +05:00
<CreateNotebookDialog
open={open}
onDone={async (topics, title, description) => {
if (await db.addNotebook({ title, description, topics })) {
setNotebooks(db.getNotebooks());
setOpen(false);
} else {
showSnack("Please fill out the notebook title.");
}
2019-12-03 18:09:31 +05:00
}}
2019-12-05 17:48:54 +05:00
close={() => setOpen(false)}
2019-12-04 10:28:56 +05:00
/>
</>
2019-12-03 18:09:31 +05:00
);
};
2020-01-09 23:28:13 +05:00
const NotebooksContainer = props => {
2020-01-09 15:42:15 +05:00
useEffect(() => {
2020-01-09 23:28:13 +05:00
const NotebookNavigator = require("../navigation/navigators/nbnavigator")
.default;
2020-01-10 09:22:19 +05:00
//HOTFIX
NotebookNavigator.history = [];
NotebookNavigator.lastRoute = undefined;
2020-01-09 15:42:15 +05:00
NotebookNavigator.navigate("notebooks");
}, []);
return (
<Flex
className="NotebookNavigator"
flexDirection="column"
flex="1 1 auto"
/>
);
};
2020-01-09 23:28:13 +05:00
export { NotebooksContainer, Notebooks };