diff --git a/web/components/project/form.tsx b/web/components/project/form.tsx index 7fd741cb9..58515d030 100644 --- a/web/components/project/form.tsx +++ b/web/components/project/form.tsx @@ -1,7 +1,7 @@ import { FC, useEffect, useState } from "react"; import { Controller, useForm } from "react-hook-form"; // hooks -import { useEventTracker, useProject, useWorkspace } from "hooks/store"; +import { useEventTracker, useProject } from "hooks/store"; import useToast from "hooks/use-toast"; // components import EmojiIconPicker from "components/emoji-icon-picker"; @@ -19,22 +19,19 @@ import { NETWORK_CHOICES } from "constants/project"; // services import { ProjectService } from "services/project"; import { PROJECT_UPDATED } from "constants/event-tracker"; - export interface IProjectDetailsForm { project: IProject; workspaceSlug: string; + projectId: string; isAdmin: boolean; } - const projectService = new ProjectService(); - export const ProjectDetailsForm: FC = (props) => { - const { project, workspaceSlug, isAdmin } = props; + const { project, workspaceSlug, projectId, isAdmin } = props; // states const [isLoading, setIsLoading] = useState(false); // store hooks const { captureProjectEvent } = useEventTracker(); - const { currentWorkspace } = useWorkspace(); const { updateProject } = useProject(); // toast alert const { setToastAlert } = useToast(); @@ -47,6 +44,7 @@ export const ProjectDetailsForm: FC = (props) => { setError, reset, formState: { errors, dirtyFields }, + getValues, } = useForm({ defaultValues: { ...project, @@ -56,26 +54,23 @@ export const ProjectDetailsForm: FC = (props) => { }); useEffect(() => { - if (!project) return; - reset({ - ...project, - emoji_and_icon: project.emoji ?? project.icon_prop, - workspace: (project.workspace as IWorkspace).id, - }); - }, [project, reset]); - + if (project && projectId !== getValues("id")) { + reset({ + ...project, + emoji_and_icon: project.emoji ?? project.icon_prop, + workspace: (project.workspace as IWorkspace).id, + }); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [project, projectId]); const handleIdentifierChange = (event: React.ChangeEvent) => { const { value } = event.target; - const alphanumericValue = value.replace(/[^a-zA-Z0-9]/g, ""); const formattedValue = alphanumericValue.toUpperCase(); - setValue("identifier", formattedValue); }; - const handleUpdateChange = async (payload: Partial) => { if (!workspaceSlug || !project) return; - return updateProject(workspaceSlug.toString(), project.id, payload) .then((res) => { const changed_properties = Object.keys(dirtyFields); @@ -107,11 +102,9 @@ export const ProjectDetailsForm: FC = (props) => { }); }); }; - const onSubmit = async (formData: IProject) => { if (!workspaceSlug) return; setIsLoading(true); - const payload: Partial = { name: formData.name, network: formData.network, @@ -119,7 +112,6 @@ export const ProjectDetailsForm: FC = (props) => { description: formData.description, cover_image: formData.cover_image, }; - if (typeof formData.emoji_and_icon === "object") { payload.emoji = null; payload.icon_prop = formData.emoji_and_icon; @@ -127,7 +119,6 @@ export const ProjectDetailsForm: FC = (props) => { payload.emoji = formData.emoji_and_icon; payload.icon_prop = null; } - if (project.identifier !== formData.identifier) await projectService .checkProjectIdentifierAvailability(workspaceSlug as string, payload.identifier ?? "") @@ -136,20 +127,16 @@ export const ProjectDetailsForm: FC = (props) => { else await handleUpdateChange(payload); }); else await handleUpdateChange(payload); - setTimeout(() => { setIsLoading(false); }, 300); }; - const currentNetwork = NETWORK_CHOICES.find((n) => n.key === project?.network); const selectedNetwork = NETWORK_CHOICES.find((n) => n.key === watch("network")); - return (
- {watch("cover_image")!}
@@ -180,7 +167,6 @@ export const ProjectDetailsForm: FC = (props) => {
-
= (props) => { )} />
-

Description

= (props) => { )} />
-

Identifier

@@ -280,7 +264,6 @@ export const ProjectDetailsForm: FC = (props) => { )} />
-

Network

= (props) => { />
-
<>