Skip to content

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

refactor: move createnbdialog to its own file

thecodrrb9eb241ab511
diff --git a/apps/web/src/components/dialogs/add-notebook-dialog.js b/apps/web/src/components/dialogs/add-notebook-dialog.js
new file mode 100644
index 000000000..cadb862d5
--- /dev/null
+++ b/apps/web/src/components/dialogs/add-notebook-dialog.js
@@ -0,0 +1,129 @@
+import React from "react";
+import { Flex, Box, Text, Button as RebassButton } from "rebass";
+import { Input, Checkbox, Label } from "@rebass/forms";
+import * as Icon from "react-feather";
+import Dialog, { showDialog } from "./dialog";
+import { showSnack } from "../snackbar";
+
+export default class AddNotebookDialog extends React.Component {
+ title = [];
+ description = [];
+ _inputRefs = [];
+ lastLength = 0;
+ state = {
+ topics: [],
+ open: false
+ };
+
+ addTopic(index) {
+ this.setState({ topics: this.state.topics.splice(index + 1, 0, "") }, () =>
+ setTimeout(() => {
+ this._inputRefs[index + 1].focus();
+ }, 0)
+ );
+ }
+
+ render() {
+ const props = this.props;
+ return (
+ <Dialog
+ open={props.isOpen}
+ title="Notebook"
+ icon={Icon.BookOpen}
+ content={
+ <Box my={1}>
+ <Input
+ variant="default"
+ onChange={e => (this.title = e.target.value)}
+ placeholder="Enter name"
+ />
+ <Input
+ variant="default"
+ sx={{ marginTop: 1 }}
+ onChange={e => (this.description = e.target.value)}
+ placeholder="Enter description (optional)"
+ />
+ <Label alignItems="center" my={1}>
+ <Checkbox variant="checkbox" />
+ Locked?
+ </Label>
+ <Text variant="body" fontWeight="bold" my={1}>
+ Topics (optional):
+ </Text>
+ <Box sx={{ maxHeight: 44 * 5, overflowY: "auto", marginBottom: 1 }}>
+ {this.topics.map((value, index) => (
+ <Flex
+ key={index.toString()}
+ flexDirection="row"
+ sx={{ marginBottom: 1 }}
+ >
+ <Input
+ ref={ref => (this._inputRefs[index] = ref)}
+ variant="default"
+ value={this.topics[index]}
+ placeholder="Topic name"
+ onFocus={e => {
+ this.lastLength = e.nativeEvent.target.value.length;
+ }}
+ onChange={e => {
+ const { topics } = this.state;
+ topics[index] = e.target.value;
+ this.setState({
+ topics
+ });
+ }}
+ onKeyUp={e => {
+ if (e.nativeEvent.key === "Enter") {
+ this.addTopic(index);
+ } else if (
+ e.nativeEvent.key === "Backspace" &&
+ this.lastLength === 0 &&
+ index > 0
+ ) {
+ this.setState(
+ {
+ topics: this.state.topics.splice(index, 1)
+ },
+ () => {
+ setTimeout(() => {
+ this._inputRefs[index - 1].focus();
+ }, 0);
+ }
+ );
+ }
+ this.lastLength = e.nativeEvent.target.value.length;
+ }}
+ />
+ <RebassButton
+ variant="tertiary"
+ sx={{ marginLeft: 1 }}
+ px={2}
+ py={1}
+ onClick={() => this.addTopic(index)}
+ >
+ <Box height={20}>
+ <Icon.Plus size={20} />
+ </Box>
+ </RebassButton>
+ </Flex>
+ ))}
+ </Box>
+ </Box>
+ }
+ positiveButton={{
+ text: "Add",
+ click: () => {
+ if (!this.title.trim().length)
+ return showSnack("Please enter the notebook title.");
+ props.onDone({
+ title: this.title,
+ description: this.description,
+ topics: this.state.topics
+ });
+ }
+ }}
+ negativeButton={{ text: "Cancel", click: props.close }}
+ />
+ );
+ }
+}
diff --git a/apps/web/src/components/dialogs/dialog.js b/apps/web/src/components/dialogs/dialog.js
new file mode 100644
index 000000000..ea041d8ef
--- /dev/null
+++ b/apps/web/src/components/dialogs/dialog.js
@@ -0,0 +1,117 @@
+import React, { useState, useEffect } from "react";
+import ReactDOM from "react-dom";
+import { Flex, Box, Text, Button as RebassButton, Button } from "rebass";
+import { Input, Checkbox, Label } from "@rebass/forms";
+import * as Icon from "react-feather";
+import { ThemeProvider } from "../../utils/theme";
+import { db } from "../../common";
+import Modal from "react-modal";
+
+export default class Dialog extends React.Component {
+ render() {
+ const props = this.props;
+ return (
+ <ThemeProvider>
+ {theme => (
+ <Modal
+ isOpen={true}
+ shouldCloseOnOverlayClick={true}
+ onRequestClose={props.closeCick}
+ style={{
+ content: {
+ top: "50%",
+ left: "50%",
+ right: "auto",
+ bottom: "auto",
+ marginRight: "-50%",
+ transform: "translate(-50%, -50%)",
+ borderWidth: 0,
+ borderRadius: theme.radii["default"],
+ backgroundColor: theme.colors.background,
+ color: theme.colors.text,
+ boxShadow: theme.shadows["3"],
+ width: "25%",
+ paddingRight: 40,
+ paddingLeft: 40,
+ overflowY: "hidden"
+ },
+ overlay: {
+ zIndex: 999,
+ background: theme.colors.overlay
+ }
+ }}
+ >
+ <Flex flexDirection="column">
+ <Flex
+ flexDirection="row"
+ alignItems="center"
+ alignSelf="center"
+ justifyContent="center"
+ color="primary"
+ py={2}
+ >
+ <Box height={props.iconSize || 32}>
+ <props.icon size={props.iconSize || 32} />
+ </Box>
+ <Text
+ mx={2}
+ as="span"
+ variant="title"
+ fontSize={22}
+ textAlign="center"
+ >
+ {props.title}
+ </Text>
+ </Flex>
+ {props.content}
+ <Flex
+ flexDirection="row"
+ my={1}
+ justifyContent="center"
+ alignItems="center"
+ >
+ {props.positiveButton && (
+ <RebassButton
+ variant="primary"
+ sx={{ opacity: props.positiveButton.disabled ? 0.7 : 1 }}
+ mx={1}
+ width={"25%"}
+ disabled={props.positiveButton.disabled || false}
+ onClick={props.positiveButton.click}
+ >
+ {props.positiveButton.text || "OK"}
+ </RebassButton>
+ )}
+
+ {props.negativeButton && (
+ <RebassButton
+ variant="secondary"
+ width={"25%"}
+ onClick={props.negativeButton.onClick}
+ >
+ {props.negativeButton.text || "Cancel"}
+ </RebassButton>
+ )}
+ </Flex>
+ </Flex>
+ </Modal>
+ )}
+ </ThemeProvider>
+ );
+ }
+}
+
+export const showDialog = dialog => {
+ const root = document.getElementById("dialogContainer");
+ const perform = (resolve, result) => {
+ ReactDOM.unmountComponentAtNode(root);
+ resolve(result);
+ };
+ if (root) {
+ return new Promise(resolve => {
+ const PropDialog = dialog(perform.bind(this, resolve));
+ ReactDOM.render(<PropDialog />, root);
+ });
+ }
+ return Promise.reject("No element with id 'dialogContainer'");
+};
diff --git a/apps/web/src/components/dialogs/index.js b/apps/web/src/components/dialogs/index.js
index 64c2c0c44..e2f0bd1be 100644
--- a/apps/web/src/components/dialogs/index.js
+++ b/apps/web/src/components/dialogs/index.js
@@ -3,106 +3,15 @@ import ReactDOM from "react-dom";
import { Flex, Box, Text, Button as RebassButton, Button } from "rebass";
import { Input, Checkbox, Label } from "@rebass/forms";
import * as Icon from "react-feather";
-import { ThemeProvider } from "../../utils/theme";
import { db } from "../../common";
-import Modal from "react-modal";
+import Dialog, { showDialog } from "./dialog";
-const Dialog = props => {
- const [open, setOpen] = useState(false);
- Dialog.close = () => setOpen(false);
- useEffect(() => {
- setOpen(props.open);
- }, [props.open]);
-
- return (
- <ThemeProvider>
- {theme => (
- <Modal
- isOpen={open}
- shouldCloseOnOverlayClick={true}
- onRequestClose={props.closeCick}
- style={{
- content: {
- top: "50%",
- left: "50%",
- right: "auto",
- bottom: "auto",
- marginRight: "-50%",
- transform: "translate(-50%, -50%)",
- borderWidth: 0,
- borderRadius: theme.radii["default"],
- backgroundColor: theme.colors.background,
- color: theme.colors.text,
- boxShadow: theme.shadows["3"],
- width: "25%",
- paddingRight: 40,
- paddingLeft: 40,
- overflowY: "hidden"
- },
- overlay: {
- zIndex: 999,
- background: theme.colors.overlay
- }
- }}
- >
- <Flex flexDirection="column">
- <Flex
- flexDirection="row"
- alignItems="center"
- alignSelf="center"
- justifyContent="center"
- color="primary"
- py={2}
- >
- <Box height={props.iconSize || 32}>
- <props.icon size={props.iconSize || 32} />
- </Box>
- <Text
- mx={2}
- as="span"
- variant="title"
- fontSize={22}
- textAlign="center"
- >
- {props.title}
- </Text>
- </Flex>
- {props.content}
- <Flex
- flexDirection="row"
- my={1}
- justifyContent="center"
- alignItems="center"
- >
- {props.positiveButton && (
- <RebassButton
- variant="primary"
- sx={{ opacity: props.positiveButton.disabled ? 0.7 : 1 }}
- mx={1}
- width={"25%"}
- disabled={props.positiveButton.disabled || false}
- onClick={props.positiveButton.click}
- >
- {props.positiveButton.text || "OK"}
- </RebassButton>
- )}
-
- {props.negativeButton && (
- <RebassButton
- variant="secondary"
- width={"25%"}
- onClick={props.negativeButton.onClick}
- >
- {props.negativeButton.text || "Cancel"}
- </RebassButton>
- )}
- </Flex>
- </Flex>
- </Modal>
- )}
- </ThemeProvider>
- );
-};
+/* import React, { useState, useEffect } from "react";
+import ReactDOM from "react-dom";
+import { Flex, Box, Text, Button as RebassButton, Button } from "rebass";
+import { Input, Checkbox, Label } from "@rebass/forms";
+import * as Icon from "react-feather";
+import { db } from "../../common";
const inputRefs = [];
export const CreateNotebookDialog = props => {
@@ -492,3 +401,4 @@ export const moveNote = (noteId, notebook) => {
}
return Promise.reject("No element with id 'dialogContainer'");
};
+ */
diff --git a/apps/web/src/views/Notebooks.js b/apps/web/src/views/Notebooks.js
index c3f433e30..80604314a 100644
--- a/apps/web/src/views/Notebooks.js
+++ b/apps/web/src/views/Notebooks.js
@@ -3,14 +3,14 @@ import { Flex } from "rebass";
import { db, ev } from "../common";
import { showSnack } from "../components/snackbar";
import Notebook from "../components/notebook";
-import { CreateNotebookDialog } from "../components/dialogs";
+import AddNotebookDialog from "../components/dialogs/addnotebookdialog";
import ListContainer from "../components/list-container";
import useStore from "../common/store";
const Notebooks = props => {
const [open, setOpen] = useState(false);
const notebooks = useStore(state => state.notebooks);
-
+ const addNotebook = useStore(state => state.addNotebook);
return (
<>
<ListContainer
@@ -42,6 +42,13 @@ const Notebooks = props => {
onClick: async () => setOpen(true)
}}
/>
+ <AddNotebookDialog
+ isOpen={open}
+ onDone={addNotebook}
+ close={() => {
+ setOpen(false);
+ }}
+ />
</>
);
};