import { useEffect } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; // react-hook-form import { useForm } from "react-hook-form"; // services import stateService from "services/project_state.service"; // hooks import useProjectMembers from "hooks/use-project-members"; // components import { FiltersList } from "components/core"; import { SelectFilters } from "components/views"; // ui import { Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui"; // helpers import { checkIfArraysHaveSameElements } from "helpers/array.helper"; import { getStatesList } from "helpers/state.helper"; // types import { IQuery, IView } from "types"; import issuesService from "services/issue.service"; // fetch-keys import { PROJECT_ISSUE_LABELS, STATES_LIST } from "constants/fetch-keys"; type Props = { handleFormSubmit: (values: IView) => Promise; handleClose: () => void; status: boolean; data?: IView | null; preLoadedData?: Partial | null; }; const defaultValues: Partial = { name: "", description: "", }; export const ViewForm: React.FC = ({ handleFormSubmit, handleClose, status, data, preLoadedData }) => { const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { register, formState: { errors, isSubmitting }, handleSubmit, reset, watch, setValue, } = useForm({ defaultValues, }); const filters = watch("query"); const { data: stateGroups } = useSWR( workspaceSlug && projectId && (filters?.state ?? []).length > 0 ? STATES_LIST(projectId as string) : null, workspaceSlug && (filters?.state ?? []).length > 0 ? () => stateService.getStates(workspaceSlug as string, projectId as string) : null ); const states = getStatesList(stateGroups); const { data: labels } = useSWR( workspaceSlug && projectId && (filters?.labels ?? []).length > 0 ? PROJECT_ISSUE_LABELS(projectId.toString()) : null, workspaceSlug && projectId && (filters?.labels ?? []).length > 0 ? () => issuesService.getIssueLabels(workspaceSlug.toString(), projectId.toString()) : null ); const { members } = useProjectMembers(workspaceSlug?.toString(), projectId?.toString()); const handleCreateUpdateView = async (formData: IView) => { await handleFormSubmit(formData); reset({ ...defaultValues, }); }; const clearAllFilters = () => { setValue("query", { assignees: null, created_by: null, labels: null, priority: null, state: null, start_date: null, target_date: null, type: null, }); }; useEffect(() => { reset({ ...defaultValues, ...preLoadedData, ...data, }); }, [data, preLoadedData, reset]); useEffect(() => { if (status && data) { setValue("query", data.query_data); } }, [data, status, setValue]); return (

{status ? "Update" : "Create"} View