Compare commits
6 Commits
1b9d1ee7d5
...
frontend
Author | SHA1 | Date | |
---|---|---|---|
3a907399bd
|
|||
141eac7d17
|
|||
5e72b2ebc9
|
|||
056401bb22
|
|||
bf31eeb652
|
|||
719889e2bc
|
1075
backend/pdm.lock
generated
1075
backend/pdm.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -13,7 +13,7 @@ dependencies = [
|
||||
"bcrypt>=4.0.1",
|
||||
"itsdangerous>=2.1.2",
|
||||
"quart-rate-limiter>=0.7.0",
|
||||
"pydantic[email]==1.10.11",
|
||||
"pydantic[email]",
|
||||
"quart-schema>=0.14.3",
|
||||
"quart-db[postgresql]>=0.4.1",
|
||||
"httpx>=0.23.1",
|
||||
|
@@ -7,4 +7,7 @@ blueprint = Blueprint("control", __name__)
|
||||
@blueprint.get("/control/ping/")
|
||||
@rate_exempt
|
||||
async def ping() -> ResponseReturnValue:
|
||||
"""Ping the server
|
||||
Check if server is up and running.
|
||||
"""
|
||||
return {"ping": "pong"}
|
||||
|
18
frontend/package-lock.json
generated
18
frontend/package-lock.json
generated
@@ -23,7 +23,7 @@
|
||||
"@types/node": "^16.11.45",
|
||||
"@types/react": "^18.0.15",
|
||||
"@types/react-dom": "^18.0.6",
|
||||
"axios": "^1.5.0",
|
||||
"axios": "^0.27.2",
|
||||
"date-fns": "^2.29.1",
|
||||
"formik": "^2.2.9",
|
||||
"react": "^18.2.0",
|
||||
@@ -5417,13 +5417,12 @@
|
||||
}
|
||||
},
|
||||
"node_modules/axios": {
|
||||
"version": "1.5.0",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-1.5.0.tgz",
|
||||
"integrity": "sha512-D4DdjDo5CY50Qms0qGQTTw6Q44jl7zRwY7bthds06pUGfChBCTcQs+N743eFWGEd6pRTMd6A+I87aWyFV5wiZQ==",
|
||||
"version": "0.27.2",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz",
|
||||
"integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==",
|
||||
"dependencies": {
|
||||
"follow-redirects": "^1.15.0",
|
||||
"form-data": "^4.0.0",
|
||||
"proxy-from-env": "^1.1.0"
|
||||
"follow-redirects": "^1.14.9",
|
||||
"form-data": "^4.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/axios/node_modules/form-data": {
|
||||
@@ -13498,11 +13497,6 @@
|
||||
"node": ">= 0.10"
|
||||
}
|
||||
},
|
||||
"node_modules/proxy-from-env": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
|
||||
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
|
||||
},
|
||||
"node_modules/psl": {
|
||||
"version": "1.9.0",
|
||||
"resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz",
|
||||
|
@@ -18,7 +18,7 @@
|
||||
"@types/node": "^16.11.45",
|
||||
"@types/react": "^18.0.15",
|
||||
"@types/react-dom": "^18.0.6",
|
||||
"axios": "^1.5.0",
|
||||
"axios": "^0.27.2",
|
||||
"date-fns": "^2.29.1",
|
||||
"formik": "^2.2.9",
|
||||
"react": "^18.2.0",
|
||||
@@ -79,5 +79,5 @@
|
||||
"prettier": {
|
||||
"trailingComma": "all"
|
||||
},
|
||||
"proxy": "http://localhost:5050"
|
||||
"proxy": "http://127.0.0.1:5050"
|
||||
}
|
||||
|
@@ -1,8 +1,63 @@
|
||||
import { BrowserRouter, Routes } from "react-router-dom";
|
||||
import { BrowserRouter, Route, Routes } from "react-router-dom";
|
||||
|
||||
import ScrollToTop from "./components/ScrollToTop";
|
||||
import TopBar from "./components/TopBar";
|
||||
|
||||
import Register from "./pages/Register";
|
||||
import ConfirmEmail from "./pages/ConfirmEmail";
|
||||
import Login from "./pages/Login";
|
||||
import RequireAuth from "./components/RequireAuth";
|
||||
import ChangePassword from "./pages/ChangePassword";
|
||||
import ForgottenPassword from "./pages/ForgottenPassword";
|
||||
import ResetPassword from "./pages/ResetPassword";
|
||||
|
||||
import CreateTodo from "./pages/CreateTodo";
|
||||
import EditTodo from "./pages/EditTodo";
|
||||
import Todos from "./pages/Todos";
|
||||
|
||||
const Router = () => (
|
||||
<BrowserRouter>
|
||||
<Routes>{}</Routes>
|
||||
<ScrollToTop />
|
||||
<TopBar />
|
||||
<Routes>
|
||||
<Route path="/register/" element={<Register />} />
|
||||
<Route path="/confirm-email/:token/" element={<ConfirmEmail />} />
|
||||
<Route path="/login/" element={<Login />} />
|
||||
<Route
|
||||
path="/change-password/"
|
||||
element={
|
||||
<RequireAuth>
|
||||
<ChangePassword />
|
||||
</RequireAuth>
|
||||
}
|
||||
/>
|
||||
<Route path="/forgotten-password/" element={<ForgottenPassword />} />
|
||||
<Route path="/reset-password/:token/" element={<ResetPassword />} />
|
||||
<Route
|
||||
path="/"
|
||||
element={
|
||||
<RequireAuth>
|
||||
<Todos />
|
||||
</RequireAuth>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/todos/new/"
|
||||
element={
|
||||
<RequireAuth>
|
||||
<CreateTodo />
|
||||
</RequireAuth>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/todos/:id/"
|
||||
element={
|
||||
<RequireAuth>
|
||||
<EditTodo />
|
||||
</RequireAuth>
|
||||
}
|
||||
/>
|
||||
</Routes>
|
||||
</BrowserRouter>
|
||||
);
|
||||
|
||||
|
67
frontend/src/components/AccountMenu.tsx
Normal file
67
frontend/src/components/AccountMenu.tsx
Normal file
@@ -0,0 +1,67 @@
|
||||
import axios from "axios";
|
||||
import Divider from "@mui/material/Divider";
|
||||
import IconButton from "@mui/material/IconButton";
|
||||
import Menu from "@mui/material/Menu";
|
||||
import MenuItem from "@mui/material/MenuItem";
|
||||
import AccountCircle from "@mui/icons-material/AccountCircle";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import React, { useContext, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
|
||||
import { AuthContext } from "src/AuthContext";
|
||||
import { useMutation } from "src/query";
|
||||
|
||||
const useLogout = () => {
|
||||
const { setAuthenticated } = useContext(AuthContext);
|
||||
const queryClient = useQueryClient();
|
||||
const { mutate: logout } = useMutation(
|
||||
async () => await axios.delete("/sessions/"),
|
||||
{
|
||||
onSuccess: () => {
|
||||
setAuthenticated(false);
|
||||
queryClient.clear();
|
||||
},
|
||||
},
|
||||
);
|
||||
return logout;
|
||||
};
|
||||
|
||||
const AccountMenu = () => {
|
||||
const logout = useLogout();
|
||||
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
|
||||
|
||||
const onMenuOpen = (event: React.MouseEvent<HTMLElement>) =>
|
||||
setAnchorEl(event.currentTarget);
|
||||
const onMenuClose = () => setAnchorEl(null);
|
||||
|
||||
return (
|
||||
<>
|
||||
<IconButton color="inherit" onClick={onMenuOpen}>
|
||||
<AccountCircle />
|
||||
</IconButton>
|
||||
<Menu
|
||||
anchorEl={anchorEl}
|
||||
anchorOrigin={{ horizontal: "right", vertical: "top" }}
|
||||
keepMounted
|
||||
onClose={onMenuClose}
|
||||
open={Boolean(anchorEl)}
|
||||
transformOrigin={{ horizontal: "right", vertical: "top" }}
|
||||
>
|
||||
<MenuItem component={Link} onClick={onMenuClose} to="/change-password/">
|
||||
Change password
|
||||
</MenuItem>
|
||||
<Divider />
|
||||
<MenuItem
|
||||
onClick={() => {
|
||||
logout();
|
||||
onMenuClose();
|
||||
}}
|
||||
>
|
||||
Logout
|
||||
</MenuItem>
|
||||
</Menu>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default AccountMenu;
|
61
frontend/src/components/Todo.tsx
Normal file
61
frontend/src/components/Todo.tsx
Normal file
@@ -0,0 +1,61 @@
|
||||
import Checkbox from "@mui/material/Checkbox";
|
||||
import IconButton from "@mui/material/IconButton";
|
||||
import ListItem from "@mui/material/ListItem";
|
||||
import ListItemButton from "@mui/material/ListItemButton";
|
||||
import ListItemIcon from "@mui/material/ListItemIcon";
|
||||
import ListItemText from "@mui/material/ListItemText";
|
||||
import Skeleton from "@mui/material/Skeleton";
|
||||
import DeleteIcon from "@mui/icons-material/Delete";
|
||||
import { format } from "date-fns";
|
||||
import { Link } from "react-router-dom";
|
||||
|
||||
import { Todo as TodoModel } from "../models";
|
||||
import { useDeleteTodoMutation } from "../queries";
|
||||
|
||||
interface IProps {
|
||||
todo?: TodoModel;
|
||||
}
|
||||
|
||||
const Todo = ({ todo }: IProps) => {
|
||||
const { mutateAsync: deleteTodo } = useDeleteTodoMutation();
|
||||
let secondary;
|
||||
if (todo === undefined) {
|
||||
secondary = <Skeleton width="200px" />;
|
||||
} else if (todo.due !== null) {
|
||||
secondary = format(todo.due, "P");
|
||||
}
|
||||
return (
|
||||
<ListItem
|
||||
secondaryAction={
|
||||
<IconButton
|
||||
disabled={todo === undefined}
|
||||
edge="end"
|
||||
onClick={() => deleteTodo(todo?.id!)}
|
||||
>
|
||||
<DeleteIcon />
|
||||
</IconButton>
|
||||
}
|
||||
>
|
||||
<ListItemButton
|
||||
component={Link}
|
||||
disabled={todo === undefined}
|
||||
to={`/todos/${todo?.id}/`}
|
||||
>
|
||||
<ListItemIcon>
|
||||
<Checkbox
|
||||
checked={todo?.complete ?? false}
|
||||
disabled
|
||||
disableRipple
|
||||
edge="start"
|
||||
tabIndex={-1}
|
||||
/>
|
||||
</ListItemIcon>
|
||||
<ListItemText
|
||||
primary={todo?.task ?? <Skeleton />}
|
||||
secondary={secondary}
|
||||
/>
|
||||
</ListItemButton>
|
||||
</ListItem>
|
||||
);
|
||||
};
|
||||
export default Todo;
|
44
frontend/src/components/TodoForm.tsx
Normal file
44
frontend/src/components/TodoForm.tsx
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Form, Formik } from "formik";
|
||||
import * as yup from "yup";
|
||||
|
||||
import CheckboxField from "../components/CheckboxField";
|
||||
import DateField from "../components/DateField";
|
||||
import FormActions from "../components/FormActions";
|
||||
import TextField from "../components/TextField";
|
||||
import type { ITodoData } from "../queries";
|
||||
|
||||
interface IProps {
|
||||
initialValues: ITodoData;
|
||||
label: string;
|
||||
onSubmit: (data: ITodoData) => Promise<any>;
|
||||
}
|
||||
|
||||
const validationSchema = yup.object({
|
||||
complete: yup.boolean(),
|
||||
due: yup.date().nullable(),
|
||||
task: yup.string().required("Required"),
|
||||
});
|
||||
|
||||
const TodoForm = ({ initialValues, label, onSubmit }: IProps) => (
|
||||
<Formik<ITodoData>
|
||||
initialValues={initialValues}
|
||||
onSubmit={onSubmit}
|
||||
validationSchema={validationSchema}
|
||||
>
|
||||
{({ dirty, isSubmitting }) => (
|
||||
<Form>
|
||||
<TextField fullWidth label="Task" name="task" required />
|
||||
<DateField fullWidth label="Due" name="due" />
|
||||
<CheckboxField fullWidth label="Complete" name="complete" />
|
||||
<FormActions
|
||||
disabled={!dirty}
|
||||
isSubmitting={isSubmitting}
|
||||
label={label}
|
||||
links={[{ label: "Back", to: "/" }]}
|
||||
/>
|
||||
</Form>
|
||||
)}
|
||||
</Formik>
|
||||
);
|
||||
|
||||
export default TodoForm;
|
37
frontend/src/components/TopBar.tsx
Normal file
37
frontend/src/components/TopBar.tsx
Normal file
@@ -0,0 +1,37 @@
|
||||
import AppBar from "@mui/material/AppBar";
|
||||
import Box from "@mui/material/Box";
|
||||
import Button from "@mui/material/Button";
|
||||
import Toolbar from "@mui/material/Toolbar";
|
||||
import React, { useContext } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
|
||||
import { AuthContext } from "src/AuthContext";
|
||||
import AccountMenu from "src/components/AccountMenu";
|
||||
|
||||
const sxToolbar = {
|
||||
paddingLeft: "env(safe-area-inset-left)",
|
||||
paddingRight: "env(safe-area-inset-right)",
|
||||
paddingTop: "env(safe-area-inset-top)",
|
||||
};
|
||||
|
||||
const TopBar = () => {
|
||||
const { authenticated } = useContext(AuthContext);
|
||||
|
||||
return (
|
||||
<>
|
||||
<AppBar position="fixed">
|
||||
<Toolbar sx={sxToolbar}>
|
||||
<Box sx={{ flexGrow: 1 }}>
|
||||
<Button color="inherit" component={Link} to="/">
|
||||
Todo
|
||||
</Button>
|
||||
</Box>
|
||||
{authenticated ? <AccountMenu /> : null}
|
||||
</Toolbar>
|
||||
</AppBar>
|
||||
<Toolbar sx={{ ...sxToolbar, marginBottom: 2 }} />
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default TopBar;
|
90
frontend/src/pages/ChangePassword.tsx
Normal file
90
frontend/src/pages/ChangePassword.tsx
Normal file
@@ -0,0 +1,90 @@
|
||||
import axios from "axios";
|
||||
import { Form, Formik, FormikHelpers } from "formik";
|
||||
import { useContext } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import * as yup from "yup";
|
||||
|
||||
import FormActions from "src/components/FormActions";
|
||||
import LazyPasswordWithStrengthField from "src/components/LazyPasswordWithStrengthField";
|
||||
import PasswordField from "src/components/PasswordField";
|
||||
import Title from "src/components/Title";
|
||||
import { ToastContext } from "src/ToastContext";
|
||||
import { useMutation } from "src/query";
|
||||
|
||||
interface IForm {
|
||||
currentPassword: string;
|
||||
newPassword: string;
|
||||
}
|
||||
|
||||
const useChangePassword = () => {
|
||||
const { addToast } = useContext(ToastContext);
|
||||
const { mutateAsync: changePassword } = useMutation(
|
||||
async (data: IForm) => await axios.put("/members/password/", data),
|
||||
);
|
||||
const navigate = useNavigate();
|
||||
|
||||
return async (data: IForm, { setFieldError }: FormikHelpers<IForm>) => {
|
||||
try {
|
||||
await changePassword(data);
|
||||
addToast("Changed", "success");
|
||||
navigate("/");
|
||||
} catch (error: any) {
|
||||
if (axios.isAxiosError(error)) {
|
||||
if (error.response?.status === 400) {
|
||||
setFieldError("newPassword", "Password is too weak");
|
||||
} else if (error.response?.status === 401) {
|
||||
setFieldError("currentPassword", "Incorrect password");
|
||||
}
|
||||
} else {
|
||||
addToast("Try again", "error");
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const validationSchema = yup.object({
|
||||
currentPassword: yup.string().required("Required"),
|
||||
newPassword: yup.string().required("Required"),
|
||||
});
|
||||
|
||||
const ChangePassword = () => {
|
||||
const onSubmit = useChangePassword();
|
||||
|
||||
return (
|
||||
<>
|
||||
<Title title="Change Password" />
|
||||
<Formik<IForm>
|
||||
initialValues={{ currentPassword: "", newPassword: "" }}
|
||||
onSubmit={onSubmit}
|
||||
validationSchema={validationSchema}
|
||||
>
|
||||
{({ dirty, isSubmitting }) => (
|
||||
<Form>
|
||||
<PasswordField
|
||||
autoComplete="current-password"
|
||||
fullWidth
|
||||
label="Current password"
|
||||
name="currentPassword"
|
||||
required
|
||||
/>
|
||||
<LazyPasswordWithStrengthField
|
||||
autoComplete="new-password"
|
||||
fullWidth
|
||||
label="New password"
|
||||
name="newPassword"
|
||||
required
|
||||
/>
|
||||
<FormActions
|
||||
disabled={!dirty}
|
||||
isSubmitting={isSubmitting}
|
||||
label="Change"
|
||||
links={[{ label: "Back", to: "/" }]}
|
||||
/>
|
||||
</Form>
|
||||
)}
|
||||
</Formik>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default ChangePassword;
|
44
frontend/src/pages/ConfirmEmail.tsx
Normal file
44
frontend/src/pages/ConfirmEmail.tsx
Normal file
@@ -0,0 +1,44 @@
|
||||
import LinearProgress from "@mui/material/LinearProgress";
|
||||
import axios from "axios";
|
||||
import { useContext } from "react";
|
||||
import { useParams } from "react-router";
|
||||
import { Navigate } from "react-router-dom";
|
||||
|
||||
import { useQuery } from "../query";
|
||||
import { ToastContext } from "../ToastContext";
|
||||
|
||||
interface IParams {
|
||||
token?: string;
|
||||
}
|
||||
|
||||
const ConfirmEmail = () => {
|
||||
const { addToast } = useContext(ToastContext);
|
||||
const params = useParams() as IParams;
|
||||
const token = params.token ?? "";
|
||||
const { isLoading } = useQuery(
|
||||
["Email"],
|
||||
async () => await axios.put("/members/email/", { token }),
|
||||
{
|
||||
onError: (error: any) => {
|
||||
if (error.response?.status === 400) {
|
||||
if (error.response?.data.code === "TOKEN_INVALID") {
|
||||
addToast("Invalid token", "error");
|
||||
} else if (error.response?.data.code === "TOKEN_EXPIRED") {
|
||||
addToast("Token expired", "error");
|
||||
}
|
||||
} else {
|
||||
addToast("Try again", "error");
|
||||
}
|
||||
},
|
||||
onSuccess: () => addToast("Thanks", "success"),
|
||||
},
|
||||
);
|
||||
|
||||
if (isLoading) {
|
||||
return <LinearProgress />;
|
||||
} else {
|
||||
return <Navigate to="/" />;
|
||||
}
|
||||
};
|
||||
|
||||
export default ConfirmEmail;
|
36
frontend/src/pages/CreateTodo.tsx
Normal file
36
frontend/src/pages/CreateTodo.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import { useContext } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
import TodoForm from "../components/TodoForm";
|
||||
import Title from "../components/Title";
|
||||
import type { ITodoData } from "../queries";
|
||||
import { useCreateTodoMutation } from "../queries";
|
||||
import { ToastContext } from "../ToastContext";
|
||||
|
||||
const CreateTodo = () => {
|
||||
const navigate = useNavigate();
|
||||
const { addToast } = useContext(ToastContext);
|
||||
const { mutateAsync: createTodo } = useCreateTodoMutation();
|
||||
|
||||
const onSubmit = async (data: ITodoData) => {
|
||||
try {
|
||||
await createTodo(data);
|
||||
navigate("/");
|
||||
} catch {
|
||||
addToast("Try Again", "error");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Title title="Create a Todo" />
|
||||
<TodoForm
|
||||
initialValues={{ complete: false, due: null, task: "" }}
|
||||
label="Create"
|
||||
onSubmit={onSubmit}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default CreateTodo;
|
52
frontend/src/pages/EditTodo.tsx
Normal file
52
frontend/src/pages/EditTodo.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
import Skeleton from "@mui/material/Skeleton";
|
||||
import { useContext } from "react";
|
||||
import { useNavigate, useParams } from "react-router";
|
||||
|
||||
import TodoForm from "../components/TodoForm";
|
||||
import Title from "../components/Title";
|
||||
import type { ITodoData } from "../queries";
|
||||
import { useEditTodoMutation, useTodoQuery } from "../queries";
|
||||
import { ToastContext } from "../ToastContext";
|
||||
|
||||
interface Iparams {
|
||||
id: string;
|
||||
}
|
||||
|
||||
const EditTodo = () => {
|
||||
const navigate = useNavigate();
|
||||
const params = useParams<keyof Iparams>() as Iparams;
|
||||
const todoId = parseInt(params.id, 10);
|
||||
const { addToast } = useContext(ToastContext);
|
||||
const { data: todo } = useTodoQuery(todoId);
|
||||
const { mutateAsync: editTodo } = useEditTodoMutation(todoId);
|
||||
|
||||
const onSubmit = async (data: ITodoData) => {
|
||||
try {
|
||||
await editTodo(data);
|
||||
navigate("/");
|
||||
} catch {
|
||||
addToast("Try again", "error");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Title title="Edit todo" />
|
||||
{todo === undefined ? (
|
||||
<Skeleton height="80px" />
|
||||
) : (
|
||||
<TodoForm
|
||||
initialValues={{
|
||||
complete: todo.complete,
|
||||
due: todo.due,
|
||||
task: todo.task,
|
||||
}}
|
||||
label="Edit"
|
||||
onSubmit={onSubmit}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default EditTodo;
|
82
frontend/src/pages/ForgottenPassword.tsx
Normal file
82
frontend/src/pages/ForgottenPassword.tsx
Normal file
@@ -0,0 +1,82 @@
|
||||
import axios from "axios";
|
||||
import { Form, Formik } from "formik";
|
||||
import { useContext } from "react";
|
||||
import { useLocation, useNavigate } from "react-router";
|
||||
import * as yup from "yup";
|
||||
|
||||
import EmailField from "src/components/EmailField";
|
||||
import FormActions from "src/components/FormActions";
|
||||
import Title from "src/components/Title";
|
||||
import { useMutation } from "src/query";
|
||||
import { ToastContext } from "src/ToastContext";
|
||||
|
||||
interface IForm {
|
||||
email: string;
|
||||
}
|
||||
|
||||
const useForgottenPassword = () => {
|
||||
const navigate = useNavigate();
|
||||
const { addToast } = useContext(ToastContext);
|
||||
|
||||
const { mutateAsync: forgottenPassword } = useMutation(
|
||||
async (data: IForm) =>
|
||||
await axios.post("/members/forgotten-password/", data),
|
||||
);
|
||||
|
||||
return async (data: IForm) => {
|
||||
try {
|
||||
await forgottenPassword(data);
|
||||
addToast("Reset link sent to your email", "success");
|
||||
navigate("/login/");
|
||||
} catch {
|
||||
addToast("Try again", "error");
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const validationSchema = yup.object({
|
||||
email: yup.string().email("Email invalid").required("Required"),
|
||||
});
|
||||
|
||||
const ForgottenPassword = () => {
|
||||
const onSubmit = useForgottenPassword();
|
||||
const location = useLocation();
|
||||
|
||||
return (
|
||||
<>
|
||||
<Title title="Forgotten password" />
|
||||
<Formik<IForm>
|
||||
initialValues={{
|
||||
email: (location.state as any)?.email ?? "",
|
||||
}}
|
||||
onSubmit={onSubmit}
|
||||
validationSchema={validationSchema}
|
||||
>
|
||||
{({ dirty, isSubmitting, values }) => (
|
||||
<Form>
|
||||
<EmailField fullWidth label="Email" name="email" required />
|
||||
<FormActions
|
||||
disabled={!dirty}
|
||||
isSubmitting={isSubmitting}
|
||||
label="Send email"
|
||||
links={[
|
||||
{
|
||||
label: "Login",
|
||||
to: "/login/",
|
||||
state: { email: values.email },
|
||||
},
|
||||
{
|
||||
label: "Register",
|
||||
to: "/register/",
|
||||
state: { email: values.email },
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Form>
|
||||
)}
|
||||
</Formik>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default ForgottenPassword;
|
99
frontend/src/pages/Login.tsx
Normal file
99
frontend/src/pages/Login.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
import axios from "axios";
|
||||
import { Form, Formik, FormikHelpers } from "formik";
|
||||
import { useContext } from "react";
|
||||
import { useLocation, useNavigate } from "react-router";
|
||||
import * as yup from "yup";
|
||||
|
||||
import { AuthContext } from "../AuthContext";
|
||||
import EmailField from "../components/EmailField";
|
||||
import FormActions from "../components/FormActions";
|
||||
import PasswordField from "../components/PasswordField";
|
||||
import Title from "../components/Title";
|
||||
import { ToastContext } from "../ToastContext";
|
||||
import { useMutation } from "../query";
|
||||
|
||||
interface IForm {
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
const useLogin = () => {
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
const { addToast } = useContext(ToastContext);
|
||||
const { setAuthenticated } = useContext(AuthContext);
|
||||
const { mutateAsync: login } = useMutation(
|
||||
async (data: IForm) => await axios.post("/sessions/", data),
|
||||
);
|
||||
|
||||
return async (data: IForm, { setFieldError }: FormikHelpers<IForm>) => {
|
||||
try {
|
||||
await login(data);
|
||||
setAuthenticated(true);
|
||||
navigate((location.state as any)?.from ?? "/");
|
||||
} catch (error: any) {
|
||||
if (error.response?.status === 401) {
|
||||
setFieldError("email", "Invalid credentials");
|
||||
setFieldError("password", "Invalid credentials");
|
||||
} else {
|
||||
addToast("Try again", "error");
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const validationSchema = yup.object({
|
||||
email: yup.string().email("Email invalid").required("Required"),
|
||||
password: yup.string().required("Required"),
|
||||
});
|
||||
|
||||
const Login = () => {
|
||||
const onSubmit = useLogin();
|
||||
const location = useLocation();
|
||||
|
||||
return (
|
||||
<>
|
||||
<Title title="Login" />
|
||||
<Formik<IForm>
|
||||
initialValues={{
|
||||
email: (location.state as any)?.email ?? "",
|
||||
password: "",
|
||||
}}
|
||||
onSubmit={onSubmit}
|
||||
validationSchema={validationSchema}
|
||||
>
|
||||
{({ dirty, isSubmitting, values }) => (
|
||||
<Form>
|
||||
<EmailField fullWidth label="Email" name="email" required />
|
||||
<PasswordField
|
||||
autoComplete="password"
|
||||
fullWidth
|
||||
label="Password"
|
||||
name="password"
|
||||
required
|
||||
/>
|
||||
<FormActions
|
||||
disabled={!dirty}
|
||||
isSubmitting={isSubmitting}
|
||||
label="Login"
|
||||
links={[
|
||||
{
|
||||
label: "Reset password",
|
||||
to: "/forgotten-password/",
|
||||
state: { email: values.email },
|
||||
},
|
||||
{
|
||||
label: "Register",
|
||||
to: "/register/",
|
||||
state: { email: values.email },
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Form>
|
||||
)}
|
||||
</Formik>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default Login;
|
99
frontend/src/pages/Register.tsx
Normal file
99
frontend/src/pages/Register.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
import axios from "axios";
|
||||
import { Form, Formik, FormikHelpers } from "formik";
|
||||
import { useContext } from "react";
|
||||
import { useNavigate } from "react-router";
|
||||
import { useLocation } from "react-router-dom";
|
||||
import * as yup from "yup";
|
||||
|
||||
import EmailField from "src/components/EmailField";
|
||||
import FormActions from "src/components/FormActions";
|
||||
import LazyPasswordWithStrengthField from "src/components/LazyPasswordWithStrengthField";
|
||||
import Title from "src/components/Title";
|
||||
import { ToastContext } from "src/ToastContext";
|
||||
import { useMutation } from "src/query";
|
||||
|
||||
interface IForm {
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
const useRegister = () => {
|
||||
const navigate = useNavigate();
|
||||
const { addToast } = useContext(ToastContext);
|
||||
const { mutateAsync: register } = useMutation(
|
||||
async (data: IForm) => await axios.post("/members/", data),
|
||||
);
|
||||
|
||||
return async (data: IForm, { setFieldError }: FormikHelpers<IForm>) => {
|
||||
try {
|
||||
await register(data);
|
||||
addToast("Registered", "success");
|
||||
navigate("/login/", { state: { email: data.email } });
|
||||
} catch (error: any) {
|
||||
if (
|
||||
error.response?.status === 400 &&
|
||||
error.response?.data.code === "WEAK_PASSWORD"
|
||||
) {
|
||||
setFieldError("password", "Password is too weak");
|
||||
} else {
|
||||
addToast("Try again", "error");
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const validationSchema = yup.object({
|
||||
email: yup.string().email("Email invalid").required("Required"),
|
||||
password: yup.string().required("Required"),
|
||||
});
|
||||
|
||||
const Register = () => {
|
||||
const location = useLocation();
|
||||
const onSubmit = useRegister();
|
||||
|
||||
return (
|
||||
<>
|
||||
<Title title="Register" />
|
||||
<Formik<IForm>
|
||||
initialValues={{
|
||||
email: (location.state as any)?.email ?? "",
|
||||
password: "",
|
||||
}}
|
||||
onSubmit={onSubmit}
|
||||
validationSchema={validationSchema}
|
||||
>
|
||||
{({ dirty, isSubmitting, values }) => (
|
||||
<Form>
|
||||
<EmailField fullWidth label="Email" name="email" required />
|
||||
<LazyPasswordWithStrengthField
|
||||
autoComplete="new-password"
|
||||
fullWidth
|
||||
label="Password"
|
||||
name="password"
|
||||
required
|
||||
/>
|
||||
<FormActions
|
||||
disabled={!dirty}
|
||||
isSubmitting={isSubmitting}
|
||||
label="Register"
|
||||
links={[
|
||||
{
|
||||
label: "Login",
|
||||
to: "/login/",
|
||||
state: { email: values.email },
|
||||
},
|
||||
{
|
||||
label: "Reset password",
|
||||
to: "/forgotten-password/",
|
||||
state: { email: values.email },
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Form>
|
||||
)}
|
||||
</Formik>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default Register;
|
90
frontend/src/pages/ResetPassword.tsx
Normal file
90
frontend/src/pages/ResetPassword.tsx
Normal file
@@ -0,0 +1,90 @@
|
||||
import axios from "axios";
|
||||
import { Form, Formik, FormikHelpers } from "formik";
|
||||
import { useContext } from "react";
|
||||
import { useNavigate, useParams } from "react-router";
|
||||
import * as yup from "yup";
|
||||
|
||||
import LazyPasswordWithStrengthField from "src/components/LazyPasswordWithStrengthField";
|
||||
import FormActions from "src/components/FormActions";
|
||||
import Title from "src/components/Title";
|
||||
import { useMutation } from "src/query";
|
||||
import { ToastContext } from "src/ToastContext";
|
||||
|
||||
interface IForm {
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface IParams {
|
||||
token?: string;
|
||||
}
|
||||
|
||||
const useResetPassword = () => {
|
||||
const navigate = useNavigate();
|
||||
const params = useParams() as IParams;
|
||||
const token = params.token ?? "";
|
||||
const { addToast } = useContext(ToastContext);
|
||||
|
||||
const { mutateAsync: reset } = useMutation(
|
||||
async (password: string) =>
|
||||
await axios.put("/members/reset-password/", { password, token }),
|
||||
);
|
||||
|
||||
return async (data: IForm, { setFieldError }: FormikHelpers<IForm>) => {
|
||||
try {
|
||||
await reset(data.password);
|
||||
addToast("Success", "success");
|
||||
navigate("/login/");
|
||||
} catch (error: any) {
|
||||
if (error.response?.status === 400) {
|
||||
if (error.response?.data.code === "WEAK_PASSWORD") {
|
||||
setFieldError("newPassword", "Password is too weak");
|
||||
} else if (error.response?.data.code === "TOKEN_INVALID") {
|
||||
addToast("Invalid token", "error");
|
||||
} else if (error.response?.data.code === "TOKEN_EXPIRED") {
|
||||
addToast("Token expired", "error");
|
||||
}
|
||||
} else {
|
||||
addToast("Try again", "error");
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const validationSchema = yup.object({
|
||||
email: yup.string().email("Email invalid").required("Required"),
|
||||
});
|
||||
|
||||
const ResetPassword = () => {
|
||||
const onSubmit = useResetPassword();
|
||||
|
||||
return (
|
||||
<>
|
||||
<Title title="Reset password" />
|
||||
<Formik<IForm>
|
||||
initialValues={{ password: "" }}
|
||||
onSubmit={onSubmit}
|
||||
validationSchema={validationSchema}
|
||||
>
|
||||
{({ dirty, isSubmitting, values }) => (
|
||||
<Form>
|
||||
<LazyPasswordWithStrengthField
|
||||
autoComplete="new-password"
|
||||
fullWidth
|
||||
label="Password"
|
||||
name="password"
|
||||
required
|
||||
/>
|
||||
<FormActions
|
||||
disabled={!dirty}
|
||||
isSubmitting={isSubmitting}
|
||||
label="Reset password"
|
||||
links={[{ label: "Login", to: "/login/" }]}
|
||||
/>
|
||||
</Form>
|
||||
)}
|
||||
</Formik>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default ResetPassword;
|
38
frontend/src/pages/Todos.tsx
Normal file
38
frontend/src/pages/Todos.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
import Fab from "@mui/material/Fab";
|
||||
import List from "@mui/material/List";
|
||||
import AddIcon from "@mui/icons-material/Add";
|
||||
import { Link, Navigate } from "react-router-dom";
|
||||
|
||||
import Todo from "../components/Todo";
|
||||
import { useTodosQuery } from "../queries";
|
||||
|
||||
const Todos = () => {
|
||||
const { data: todos } = useTodosQuery();
|
||||
|
||||
if (todos?.length === 0) {
|
||||
return <Navigate to="/todos/new/" />;
|
||||
} else {
|
||||
return (
|
||||
<>
|
||||
<List>
|
||||
{todos !== undefined
|
||||
? todos.map((todo) => <Todo key={todo.id} todo={todo} />)
|
||||
: [1, 2, 3].map((id) => <Todo key={-id} />)}
|
||||
</List>
|
||||
<Fab
|
||||
component={Link}
|
||||
sx={{
|
||||
bottom: (theme) => theme.spacing(2),
|
||||
position: "fixed",
|
||||
right: (theme) => theme.spacing(2),
|
||||
}}
|
||||
to="/todos/new/"
|
||||
>
|
||||
<AddIcon />
|
||||
</Fab>
|
||||
</>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default Todos;
|
72
frontend/src/queries.ts
Normal file
72
frontend/src/queries.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import axios from "axios";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { Todo } from "./models";
|
||||
import { useMutation, useQuery } from "./query";
|
||||
|
||||
export const STALE_TIME = 1000 * 60 * 5; // 5 mins
|
||||
|
||||
export const useTodosQuery = () =>
|
||||
useQuery<Todo[]>(
|
||||
["todos"],
|
||||
async () => {
|
||||
const response = await axios.get("/todos/");
|
||||
return response.data.todos.map((json: any) => new Todo(json));
|
||||
},
|
||||
{ staleTime: STALE_TIME },
|
||||
);
|
||||
|
||||
export const useTodoQuery = (id: number) => {
|
||||
const queryClient = useQueryClient();
|
||||
return useQuery<Todo>(
|
||||
["todos", id.toString()],
|
||||
async () => {
|
||||
const response = await axios.get(`/todos/${id}/`);
|
||||
return new Todo(response.data);
|
||||
},
|
||||
{
|
||||
initialData: () => {
|
||||
return queryClient
|
||||
.getQueryData<Todo[]>(["todos"])
|
||||
?.filter((todo: Todo) => todo.id === id)[0];
|
||||
},
|
||||
staleTime: STALE_TIME,
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
export interface ITodoData {
|
||||
complete: boolean;
|
||||
due: Date | null;
|
||||
task: string;
|
||||
}
|
||||
|
||||
export const useCreateTodoMutation = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation(
|
||||
async (data: ITodoData) => await axios.post("/todos/", data),
|
||||
{
|
||||
onSuccess: () => queryClient.invalidateQueries(["todos"]),
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
export const useEditTodoMutation = (id: number) => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation(
|
||||
async (data: ITodoData) => await axios.put(`/todos/${id}/`, data),
|
||||
{
|
||||
onSuccess: () => queryClient.invalidateQueries(["todos"]),
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
export const useDeleteTodoMutation = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation(
|
||||
async (id: number) => await axios.delete(`/todos/${id}/`),
|
||||
{
|
||||
onSuccess: () => queryClient.invalidateQueries(["todos"]),
|
||||
},
|
||||
);
|
||||
};
|
Reference in New Issue
Block a user