2019-12-05 17:48:54 +05:00
|
|
|
import React, { useState, useEffect } from "react";
|
2020-01-09 18:32:40 +05:00
|
|
|
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";
|
2019-12-06 17:25:28 +05:00
|
|
|
import Notebook from "../components/notebook";
|
2020-01-07 14:49:19 +05:00
|
|
|
import { CreateNotebookDialog, ask } from "../components/dialogs";
|
2020-01-09 18:32:40 +05:00
|
|
|
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 (
|
2020-01-09 18:32:40 +05:00
|
|
|
<>
|
|
|
|
|
<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", {
|
2020-01-09 18:32:40 +05:00
|
|
|
title: notebooks[index].title,
|
|
|
|
|
topics: notebooks[index].topics,
|
2020-01-09 23:28:13 +05:00
|
|
|
notebook: notebooks[index]
|
2020-01-09 18:32:40 +05:00
|
|
|
});
|
|
|
|
|
}}
|
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)
|
|
|
|
|
})
|
|
|
|
|
}
|
2020-01-09 18:32:40 +05:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
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
|
|
|
/>
|
2020-01-09 18:32:40 +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 };
|