diff --git a/client/src/pages/Projects/Projects.tsx b/client/src/pages/Projects/Projects.tsx index 3457d2f..40454d1 100644 --- a/client/src/pages/Projects/Projects.tsx +++ b/client/src/pages/Projects/Projects.tsx @@ -2,9 +2,10 @@ import { Box } from "@mui/material"; import useSWR from "swr"; import { useAuth } from "../../hooks/useAuth"; import { createSWRFetcher } from "../../utils/swrFetcher"; +import { NewProjectAction } from "./components/NewProjectAction"; import { Project } from "./components/Project"; -type APIProjectList = { +export type APIProjectList = { data: { id: number; name: string; @@ -17,15 +18,18 @@ export const Projects = () => { const { data, mutate } = useSWR("projects", fetcher); return ( - - {data?.data.map((project) => ( - - ))} - + <> + + + {data?.data.map((project) => ( + + ))} + + ); }; diff --git a/client/src/pages/Projects/components/NewProjectAction.tsx b/client/src/pages/Projects/components/NewProjectAction.tsx new file mode 100644 index 0000000..6cfbf9f --- /dev/null +++ b/client/src/pages/Projects/components/NewProjectAction.tsx @@ -0,0 +1,84 @@ +import { + Box, + Button, + Dialog, + DialogActions, + DialogContent, + DialogTitle, + TextField, +} from "@mui/material"; +import { useState } from "react"; +import { SubmitHandler, useForm } from "react-hook-form"; +import { KeyedMutator } from "swr"; +import { useAuth } from "../../../hooks/useAuth"; +import { APIProjectList } from "../Projects"; + +type NewProjectForm = { + name: string; +}; + +type NewProjectActionProps = { + mutate: KeyedMutator; +}; + +export const NewProjectAction = ({ mutate }: NewProjectActionProps) => { + const { register, handleSubmit, reset } = useForm(); + const [open, setOpen] = useState(false); + const [isLoading, setIsLoading] = useState(false); + const { apiClient } = useAuth(); + + const onSubmit: SubmitHandler = (data) => { + setIsLoading(true); + apiClient("projects", { + method: "POST", + body: JSON.stringify(data), + }).then(async () => { + reset(); + await mutate(); + setIsLoading(false); + setOpen(false); + }); + }; + + const handleClose = () => { + setOpen(false); + reset(); + }; + + const handleOpen = () => { + setOpen(true); + }; + + return ( + <> + + + + +
+ New project + + + + + + + +
+
+ + ); +};