fix: project identifier check in project settings (#649)

This commit is contained in:
Aaryan Khandelwal 2023-03-31 16:04:17 +05:30 committed by GitHub
parent 480e2c4d7f
commit e3005b7776
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -68,16 +68,6 @@ const GeneralSettings: NextPage<UserAuth> = ({ 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<UserAuth> = ({ isMember, isOwner, isViewer, isGu
});
}, [projectDetails, reset]);
const updateProject = async (payload: Partial<IProject>) => {
if (!workspaceSlug || !projectDetails) return;
await projectService
.updateProject(workspaceSlug as string, projectDetails.id, payload)
.then((res) => {
mutate<IProject>(
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<UserAuth> = ({ 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<IProject>(
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 (