From 16c79e5d9229cafedb995dcf16733f9af4d800a4 Mon Sep 17 00:00:00 2001 From: mazuh Date: Mon, 29 Jan 2024 23:45:29 -0300 Subject: [PATCH] after creating a project, go to its page --- .../ProjectsManagementContext.ts | 4 +++- .../ProjectsManagementContextProvider.tsx | 14 +++++++++---- .../ProjectsManagementPage.tsx | 20 ++++++++++++++++--- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/features/projects-management/ProjectsManagementContext.ts b/src/features/projects-management/ProjectsManagementContext.ts index 7bfbaae..299f841 100644 --- a/src/features/projects-management/ProjectsManagementContext.ts +++ b/src/features/projects-management/ProjectsManagementContext.ts @@ -6,7 +6,9 @@ import { ProjectListingItem } from "./projects-management-entities"; export interface ProjectsManagementContextValue { items: ProjectListingItem[]; - create: (creating: Pick) => Promise; + create: ( + creating: Pick + ) => Promise; update: (updating: ProjectListingItem) => Promise; remove: (removing: ProjectListingItem) => Promise; isLoading: boolean; diff --git a/src/features/projects-management/ProjectsManagementContextProvider.tsx b/src/features/projects-management/ProjectsManagementContextProvider.tsx index 797d33c..feffba6 100644 --- a/src/features/projects-management/ProjectsManagementContextProvider.tsx +++ b/src/features/projects-management/ProjectsManagementContextProvider.tsx @@ -42,10 +42,16 @@ export function ProjectsManagementContextProvider({ async ({ name }) => { setError(null); setIsLoading(true); - persistNewProjectListingItem(name) - .catch((error) => setError(error)) - .finally(() => setIsLoading(false)) - .then(() => pullProjectsListing()); + return persistNewProjectListingItem(name) + .then((created: ProjectListingItem) => { + pullProjectsListing(); + return created; + }) + .catch((error) => { + setError(error); + return null; + }) + .finally(() => setIsLoading(false)); }, [pullProjectsListing] ); diff --git a/src/features/projects-management/ProjectsManagementPage.tsx b/src/features/projects-management/ProjectsManagementPage.tsx index f31527e..f277c89 100644 --- a/src/features/projects-management/ProjectsManagementPage.tsx +++ b/src/features/projects-management/ProjectsManagementPage.tsx @@ -33,6 +33,7 @@ import { ProjectListingItem } from "./projects-management-entities"; import { cn } from "@/lib/utils"; import { useProjectsManagement } from "./ProjectsManagementContext"; import { ProjectsManagementContextProvider } from "./ProjectsManagementContextProvider"; +import { useLocation } from "wouter"; export function ProjectsManagementPage() { return ( @@ -134,6 +135,7 @@ interface ProjectModalByButtonProps { } function ProjectModalByButton(props: ProjectModalByButtonProps) { + const [, setLocation] = useLocation(); const { create, update } = useProjectsManagement(); const [isOpen, setIsOpen] = useState(false); @@ -153,12 +155,24 @@ function ProjectModalByButton(props: ProjectModalByButtonProps) { }, }); - const handleSubmit = (values: z.infer) => { + const handleSubmit = async (values: z.infer) => { if (props.project) { - return update({ ...values, uuid: props.project.uuid }).then(close); + return update({ ...values, uuid: props.project.uuid }) + .then(close) + .catch(() => + form.setError("name", { message: "Something went wrong" }) + ); } - return create(values).then(close); + return create(values).then((created) => { + if (!created) { + form.setError("name", { message: "Something went wrong" }); + return; + } + + close(); + setLocation(`/project/${created.uuid}`); + }); }; return (