From e3005b7776a4776bd162befe7a4be363289f1740 Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal <65252264+aaryan610@users.noreply.github.com> Date: Fri, 31 Mar 2023 16:04:17 +0530 Subject: [PATCH] fix: project identifier check in project settings (#649) --- .../projects/[projectId]/settings/index.tsx | 73 +++++++++---------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/apps/app/pages/[workspaceSlug]/projects/[projectId]/settings/index.tsx b/apps/app/pages/[workspaceSlug]/projects/[projectId]/settings/index.tsx index 21778bad3..072e70bad 100644 --- a/apps/app/pages/[workspaceSlug]/projects/[projectId]/settings/index.tsx +++ b/apps/app/pages/[workspaceSlug]/projects/[projectId]/settings/index.tsx @@ -68,16 +68,6 @@ const GeneralSettings: NextPage = ({ isMember, isOwner, isViewer, isGu defaultValues, }); - const checkIdentifier = (value: string) => { - if (!workspaceSlug) return; - - projectService - .checkProjectIdentifierAvailability(workspaceSlug as string, value) - .then((response) => { - if (response.exists) setError("identifier", { message: "Identifier already exists" }); - }); - }; - useEffect(() => { if (projectDetails) reset({ @@ -88,6 +78,33 @@ const GeneralSettings: NextPage = ({ isMember, isOwner, isViewer, isGu }); }, [projectDetails, reset]); + const updateProject = async (payload: Partial) => { + if (!workspaceSlug || !projectDetails) return; + + await projectService + .updateProject(workspaceSlug as string, projectDetails.id, payload) + .then((res) => { + mutate( + PROJECT_DETAILS(projectDetails.id), + (prevData) => ({ ...prevData, ...res }), + false + ); + mutate(PROJECTS_LIST(workspaceSlug as string)); + setToastAlert({ + type: "success", + title: "Success!", + message: "Project updated successfully", + }); + }) + .catch(() => { + setToastAlert({ + type: "error", + title: "Error!", + message: "Project could not be updated. Please try again.", + }); + }); + }; + const onSubmit = async (formData: IProject) => { if (!workspaceSlug || !projectDetails) return; @@ -101,34 +118,14 @@ const GeneralSettings: NextPage = ({ isMember, isOwner, isViewer, isGu icon: formData.icon, }; - await projectService - .checkProjectIdentifierAvailability(workspaceSlug as string, payload.identifier ?? "") - .then(async (res) => { - if (res.exists) setError("identifier", { message: "Identifier already exists" }); - else - await projectService - .updateProject(workspaceSlug as string, projectDetails.id, payload) - .then((res) => { - mutate( - PROJECT_DETAILS(projectDetails.id), - (prevData) => ({ ...prevData, ...res }), - false - ); - mutate(PROJECTS_LIST(workspaceSlug as string)); - setToastAlert({ - type: "success", - title: "Success!", - message: "Project updated successfully", - }); - }) - .catch(() => { - setToastAlert({ - type: "error", - title: "Error!", - message: "Project could not be updated. Please try again.", - }); - }); - }); + if (projectDetails.identifier !== formData.identifier) + await projectService + .checkProjectIdentifierAvailability(workspaceSlug as string, payload.identifier ?? "") + .then(async (res) => { + if (res.exists) setError("identifier", { message: "Identifier already exists" }); + else await updateProject(payload); + }); + else await updateProject(payload); }; return (