A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
refactor: signup & login dialogs
thecodrr9c8a6c881ec6
diff --git a/apps/web/src/components/dialogs/dialog.js b/apps/web/src/components/dialogs/dialog.jsindex 70f19103a..f71068163 100644--- a/apps/web/src/components/dialogs/dialog.js+++ b/apps/web/src/components/dialogs/dialog.js@@ -4,98 +4,99 @@ import { Flex, Text, Button as RebassButton } from "rebass";import ThemeProvider from "../theme-provider";import * as Icon from "../icons";import Modal from "react-modal";-import { useTheme } from "emotion-theming";function Dialog(props) {- const theme = useTheme();return (<ThemeProvider>- <Modal- isOpen={props.isOpen || false}- shouldCloseOnOverlayClick={true}- onRequestClose={props.negativeButton.onClick}- 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: 20,- paddingLeft: 20,- overflowY: "hidden"- },- overlay: {- zIndex: 999,- background: theme.colors.overlay- }- }}- >- <Flex flexDirection="column">- <Flex- flexDirection="row"- alignItems="center"- alignSelf="center"- justifyContent="center"- color="primary"- sx={{ paddingBottom: 2 }}- >- <props.icon size={props.iconSize || 38} color="primary" />- <Text- mx={1}- as="span"- variant="title"- fontSize={"heading"}- textAlign="center"+ {theme => (+ <Modal+ isOpen={props.isOpen || false}+ shouldCloseOnOverlayClick={true}+ onRequestClose={props.negativeButton.onClick}+ 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: 20,+ paddingLeft: 20,+ overflowY: "hidden"+ },+ overlay: {+ zIndex: 999,+ background: theme.colors.overlay+ }+ }}+ >+ <Flex flexDirection="column">+ <Flex+ flexDirection="row"+ alignItems="center"+ alignSelf="center"+ justifyContent="center"+ color="primary"+ sx={{ paddingBottom: 2 }}>- {props.title}- </Text>- </Flex>- {props.content}- <Flex- flexDirection="row"- sx={{ marginTop: 3 }}- justifyContent="center"- alignItems="center"- >- {props.positiveButton && (- <RebassButton- variant="primary"- sx={{ opacity: props.positiveButton.disabled ? 0.7 : 1 }}+ <props.icon size={props.iconSize || 38} color="primary" />+ <Textmx={1}- width={"50%"}- disabled={props.positiveButton.disabled || false}- onClick={- !props.positiveButton.disabled && props.positiveButton.onClick- }+ as="span"+ variant="title"+ fontSize={"heading"}+ textAlign="center">- {props.positiveButton.loading ? (- <Icon.Loading rotate={true} color="static" />- ) : (- props.positiveButton.text || "OK"- )}- </RebassButton>- )}+ {props.title}+ </Text>+ </Flex>+ {props.content}+ <Flex+ flexDirection="row"+ sx={{ marginTop: 3 }}+ justifyContent="center"+ alignItems="center"+ >+ {props.positiveButton && (+ <RebassButton+ variant="primary"+ sx={{ opacity: props.positiveButton.disabled ? 0.7 : 1 }}+ mx={1}+ width={"50%"}+ disabled={props.positiveButton.disabled || false}+ onClick={+ !props.positiveButton.disabled &&+ props.positiveButton.onClick+ }+ >+ {props.positiveButton.loading ? (+ <Icon.Loading rotate={true} color="static" />+ ) : (+ props.positiveButton.text || "OK"+ )}+ </RebassButton>+ )}- {props.negativeButton && (- <RebassButton- variant="secondary"- width={"50%"}- onClick={props.negativeButton.onClick}- >- {props.negativeButton.text || "Cancel"}- </RebassButton>- )}+ {props.negativeButton && (+ <RebassButton+ variant="secondary"+ width={"50%"}+ onClick={props.negativeButton.onClick}+ >+ {props.negativeButton.text || "Cancel"}+ </RebassButton>+ )}+ </Flex></Flex>- </Flex>- </Modal>+ </Modal>+ )}</ThemeProvider>);}diff --git a/apps/web/src/components/dialogs/logindialog.js b/apps/web/src/components/dialogs/logindialog.jsindex 54d278c83..61d054b45 100644--- a/apps/web/src/components/dialogs/logindialog.js+++ b/apps/web/src/components/dialogs/logindialog.js@@ -1,96 +1,48 @@import React, { useState } from "react";-import { Flex, Box, Button, Text } from "rebass";-import { Input } from "@rebass/forms";+import { Button, Text } from "rebass";+import Input from "../inputs";import * as Icon from "../icons";import Dialog, { showDialog } from "./dialog";import { showSignUpDialog } from "./signupdialog";import { useStore } from "../../stores/user-store";+import PasswordInput from "../inputs/password";+import Form from "../form";function LoginDialog(props) {- const [username, setUsername] = useState();- const [password, setPassword] = useState();- const [errorMessage, setErrorMessage] = useState();+ const { onClose } = props;+ const [error, setError] = useState();const isLoggingIn = useStore(store => store.isLoggingIn);const login = useStore(store => store.login);+ const form = { error: true };+return (<DialogisOpen={true}title={"Login"}icon={Icon.Login}- onCloseClick={props.onClose}- negativeButton={{ onClick: props.onClose }}+ onCloseClick={onClose}+ negativeButton={{ onClick: onClose }}positiveButton={{text: "Login",loading: isLoggingIn,disabled: isLoggingIn,onClick: () => {- setErrorMessage();- if (username === "" || username === undefined) {- setErrorMessage("Please enter your username.");- return;- }-- if (password === "" || password === undefined) {- setErrorMessage("Please enter your password.");- return;- }-- login(username, password)- .then(() => {- props.onClose();- })- .catch(e => {- setErrorMessage(e.message);- });+ setError();+ if (!form.error) return;+ login(form)+ .then(onClose)+ .catch(e => setError(e.message));}}}content={- <Box my={1}>- <Input- autoFocus- variant="default"- placeholder="Username"- onChange={e => {- setUsername(e.target.value);- }}- ></Input>- <Input- type="password"- variant="default"- placeholder="Password"- sx={{ marginTop: 2 }}- onChange={e => {- setPassword(e.target.value);- }}- ></Input>- <Flex- flexDirection="row"- justifyContent="space-between"- sx={{ marginTop: 2 }}- >- <Button- variant="links"- onClick={() => {- showSignUpDialog();- }}- >- Create a New Account- </Button>- </Flex>- <Flex- flexDirection="row"- justifyContent="space-between"- sx={{ marginTop: errorMessage ? 2 : 0 }}- >- <Text- fontSize="subBody"- color="red"- sx={{ display: errorMessage ? "flex" : "none" }}- >- {errorMessage}- </Text>- </Flex>- </Box>+ <Form mt={1} gutter={2} form={form}>+ <Input autoFocus name="username" title="Username" />+ <PasswordInput />+ <Button variant="anchor" onClick={showSignUpDialog()}>+ Create a New Account+ </Button>+ {error && <Text variant="error">{error}</Text>}+ </Form>}/>);diff --git a/apps/web/src/components/dialogs/signupdialog.js b/apps/web/src/components/dialogs/signupdialog.jsindex e0ba76fc8..02ad64a7f 100644--- a/apps/web/src/components/dialogs/signupdialog.js+++ b/apps/web/src/components/dialogs/signupdialog.js@@ -1,107 +1,43 @@import React, { useState } from "react";-import { Flex, Box, Text } from "rebass";-import { Input } from "@rebass/forms";+import { Text } from "rebass";+import Input from "../inputs";import * as Icon from "../icons";import Dialog, { showDialog } from "./dialog";-import { db } from "../../common";+//import { db } from "../../common";+import EmailInput from "../inputs/email";+import PasswordInput from "../inputs/password";+import Form from "../form";function SignUpDialog(props) {- const [username, setUserName] = useState();- const [email, setEmail] = useState();- const [password, setPassword] = useState();- const [confirmPassword, setConfirmPassword] = useState();- const [errorMessage, setErrorMessage] = useState();+ const { onClose } = props;+ const [error, setError] = useState();+ const form = { error: true };+return (<DialogisOpen={true}title={"Sign Up"}icon={Icon.User}- onCloseClick={props.onClose}- negativeButton={{ onClick: props.onClose }}+ onCloseClick={onClose}+ negativeButton={{ onClick: onClose }}positiveButton={{text: "Sign Up",onClick: () => {- setErrorMessage();-- if (username === "" || username === undefined) {- setErrorMessage("Please enter your username.");- return;- }-- if (email === "" || email === undefined) {- setErrorMessage("Please enter your email address.");- return;- }-- if (password !== confirmPassword) {- setErrorMessage("Passwords do not match! Please try again.");- return;- }-- if (password === undefined || password === "") {- setErrorMessage("Please enter password.");- return;- }-- db.user- .signup(username, email, password)- .then(() => {- props.onClose();- })- .catch(() => {- setErrorMessage("Couldn't signup. Please try again.");- });+ setError();+ if (form.error) return;+ /* db.user+ .signup(form)+ .then(onClose)+ .catch(error => setError(`Couldn't signup. Error: ${error}`)); */}}}content={- <Box my={1}>- <Input- variant="default"- placeholder="Username"- onChange={e => {- setUserName(e.target.value);- }}- ></Input>- <Input- variant="default"- placeholder="Email"- sx={{ marginTop: 2 }}- onChange={e => {- setEmail(e.target.value);- }}- ></Input>- <Input- type="password"- variant="default"- placeholder="Password"- sx={{ marginTop: 2 }}- onChange={e => {- setPassword(e.target.value);- }}- ></Input>- <Input- type="password"- variant="default"- placeholder="Confirm Password"- sx={{ marginTop: 2 }}- onChange={e => {- setConfirmPassword(e.target.value);- }}- ></Input>- <Flex- flexDirection="row"- justifyContent="space-between"- sx={{ marginTop: 2 }}- >- <Text- fontSize="subBody"- color="red"- sx={{ display: errorMessage ? "flex" : "none" }}- >- {errorMessage}- </Text>- </Flex>- </Box>+ <Form mt={1} gutter={2} form={form}>+ <Input autoFocus title="Username" name="username" />+ <EmailInput />+ <PasswordInput confirm />+ {error && <Text variant="error">{error}</Text>}+ </Form>}/>);diff --git a/apps/web/src/components/form/index.js b/apps/web/src/components/form/index.jsnew file mode 100644index 000000000..cc2df3021--- /dev/null+++ b/apps/web/src/components/form/index.js@@ -0,0 +1,14 @@+import React from "react";+import { Box } from "rebass";++function Form(props) {+ const { gutter, children, form } = props;+ const childrenWithGutter = React.Children.map(children, (child, index) => {+ if (!child) return;+ const props = { mt: index && gutter, form };+ return React.cloneElement(child, props);+ });+ return <Box {...props}>{childrenWithGutter}</Box>;+}++export default Form;diff --git a/apps/web/src/components/inputs/email.js b/apps/web/src/components/inputs/email.jsnew file mode 100644index 000000000..94eee74a6--- /dev/null+++ b/apps/web/src/components/inputs/email.js@@ -0,0 +1,16 @@+import React from "react";+import Input from "./index";+import { isValidEmail } from "../../utils/validation";++function EmailInput() {+ return (+ <Input+ title="Email"+ name="email"+ type="email"+ error="Email is not valid."+ validate={isValidEmail}+ />+ );+}+export default EmailInput;diff --git a/apps/web/src/components/inputs/index.js b/apps/web/src/components/inputs/index.jsnew file mode 100644index 000000000..ad654150b--- /dev/null+++ b/apps/web/src/components/inputs/index.js@@ -0,0 +1,42 @@+import React, { useState } from "react";+import { Input as RebassInput } from "@rebass/forms";+import { Box, Text } from "rebass";+import { toTitleCase } from "../../utils/string";++function Input(props) {+ const { required = true, validate, variant = "input" } = props;+ const { name, form, title } = props;+ const [themeVariant, setThemeVariant] = useState(variant);+ const [error, setError] = useState(props.error);+ return (+ <Box>+ <RebassInput+ {...props}+ variant={themeVariant}+ placeholder={title}+ onChange={event => {+ const { value } = event.target;+ const isValid = !validate || validate(value, form);+ if ((!value.trim() && required) || !isValid) {+ setThemeVariant("error");+ if (form) form.error = true;+ const error =+ !isValid || !name+ ? props.error+ : `${toTitleCase(name)} is required.`;+ setError(error);+ } else {+ setThemeVariant(variant);+ if (form) {+ form[name] = value;+ form.error = false;+ }+ }+ }}+ />+ {themeVariant === "error" && <Text variant="error">{error}</Text>}+ </Box>+ );+}++export default Input;diff --git a/apps/web/src/components/inputs/password.js b/apps/web/src/components/inputs/password.jsnew file mode 100644index 000000000..f68616df0--- /dev/null+++ b/apps/web/src/components/inputs/password.js@@ -0,0 +1,20 @@+import React from "react";+import Input from "./index";++function PasswordInput(props) {+ const { confirm } = props;+ return (+ <>+ <Input title="Password" type="password" name="password" />+ {confirm && (+ <Input+ title="Confirm Password"+ type="password"+ error="Passwords do not match"+ validate={(password, form) => form.password === password}+ />+ )}+ </>+ );+}+export default PasswordInput;diff --git a/apps/web/src/components/theme-provider/index.js b/apps/web/src/components/theme-provider/index.jsindex e80ba09dc..60034a85e 100644--- a/apps/web/src/components/theme-provider/index.js+++ b/apps/web/src/components/theme-provider/index.js@@ -12,7 +12,11 @@ function ThemeProvider(props) {injectCss(factory.transform("css"));const theme = factory.construct({ theme: themeType, accent, scale: 1 });return (- <EmotionThemeProvider theme={theme}>{props.children}</EmotionThemeProvider>+ <EmotionThemeProvider theme={theme}>+ {props.children instanceof Function+ ? props.children(theme)+ : props.children}+ </EmotionThemeProvider>);}export default ThemeProvider;diff --git a/apps/web/src/theme/colorscheme/dark.js b/apps/web/src/theme/colorscheme/dark.jsindex 565711054..31e1e0d4b 100644--- a/apps/web/src/theme/colorscheme/dark.js+++ b/apps/web/src/theme/colorscheme/dark.js@@ -5,7 +5,6 @@ class DarkColorSchemeFactory {constructor(accent) {return {primary: accent,- shade: hexToRGB(accent, 0.1),placeholder: hexToRGB("#ffffff", 0.6),background: "#1f1f1f",accent: "#000",@@ -17,7 +16,7 @@ class DarkColorSchemeFactory {overlay: "rgba(255, 255, 255, 0.5)",secondary: "black",icon: "#dbdbdb",- ...new StaticColorSchemeFactory()+ ...new StaticColorSchemeFactory(accent)};}}diff --git a/apps/web/src/theme/colorscheme/light.js b/apps/web/src/theme/colorscheme/light.jsindex 16ae7f9a4..5ba3a8c8f 100644--- a/apps/web/src/theme/colorscheme/light.js+++ b/apps/web/src/theme/colorscheme/light.js@@ -15,9 +15,8 @@ class LightColorSchemeFactory {overlay: "rgba(0, 0, 0, 0.1)",secondary: "white",icon: "#3b3b3b",- shade: hexToRGB(accent, 0.1),placeholder: hexToRGB("#000000", 0.6),- ...new StaticColorSchemeFactory()+ ...new StaticColorSchemeFactory(accent)};}}diff --git a/apps/web/src/theme/colorscheme/static.js b/apps/web/src/theme/colorscheme/static.jsindex 2cd6ee339..fdb50d02c 100644--- a/apps/web/src/theme/colorscheme/static.js+++ b/apps/web/src/theme/colorscheme/static.js@@ -1,6 +1,10 @@+import { hexToRGB } from "../../utils/color";+class StaticColorSchemeFactory {- constructor() {+ constructor(accent) {return {+ shade: hexToRGB(accent, 0.1),+ dimPrimary: hexToRGB(accent, 0.7),fontTertiary: "gray",transparent: "transparent",static: "white",diff --git a/apps/web/src/theme/variants/input.js b/apps/web/src/theme/variants/input.jsindex 2d7eaf231..b785c8881 100644--- a/apps/web/src/theme/variants/input.js+++ b/apps/web/src/theme/variants/input.js@@ -1,7 +1,7 @@class InputFactory {constructor() {return {- default: new Default(),+ input: new Default(),error: new Error()};}@@ -22,7 +22,7 @@ class Default {borderColor: "primary"},":hover": {- borderColor: "shade"+ borderColor: "dimPrimary"}};}@@ -31,7 +31,7 @@ class Default {class Error {constructor() {return {- variant: "forms.default",+ variant: "forms.input",borderColor: "red",":focus": {outline: "none",diff --git a/apps/web/src/theme/variants/text.js b/apps/web/src/theme/variants/text.jsindex d1fc1b3d8..48416d090 100644--- a/apps/web/src/theme/variants/text.js+++ b/apps/web/src/theme/variants/text.js@@ -4,7 +4,8 @@ class TextFactory {default: new Default(),heading: new Heading(),title: new Title(),- body: new Body()+ body: new Body(),+ error: new Error()};}}@@ -41,6 +42,12 @@ class Title {class Body {constructor() {- return { variant: "text.default", fontWeight: "body", fontSize: "body" };+ return { variant: "text.default", fontSize: "body" };+ }+}++class Error {+ constructor() {+ return { variant: "text.default", fontSize: "subBody", color: "error" };}}diff --git a/apps/web/src/utils/validation.js b/apps/web/src/utils/validation.jsnew file mode 100644index 000000000..cf8378367--- /dev/null+++ b/apps/web/src/utils/validation.js@@ -0,0 +1,7 @@+function isValidEmail(email) {+ return /^([a-zA-Z0-9_\-.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9-]+\.)+))([a-zA-Z0-9]{1,30})(\]?)$/.test(+ );+}++export { isValidEmail };