import React, { useEffect } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; import { useForm, Controller } from "react-hook-form"; import { TwitterPicker } from "react-color"; import { Popover, Transition } from "@headlessui/react"; // store import { observer } from "mobx-react-lite"; import { useMobxStore } from "lib/mobx/store-provider"; // hooks import useToast from "hooks/use-toast"; // ui import { CustomSelect } from "components/ui"; import { Button, Input, Tooltip } from "@plane/ui"; // types import type { IState } from "types"; // fetch-keys import { STATES_LIST } from "constants/fetch-keys"; // constants import { GROUP_CHOICES } from "constants/project"; type Props = { data: IState | null; onClose: () => void; groupLength: number; selectedGroup: StateGroup | null; }; export type StateGroup = "backlog" | "unstarted" | "started" | "completed" | "cancelled" | null; const defaultValues: Partial = { name: "", description: "", color: "rgb(var(--color-text-200))", group: "backlog", }; export const CreateUpdateStateInline: React.FC = observer((props) => { const { data, onClose, selectedGroup, groupLength } = props; // router const router = useRouter(); const { workspaceSlug, projectId } = router.query; // store const { projectState: projectStateStore } = useMobxStore(); // hooks const { setToastAlert } = useToast(); const { handleSubmit, formState: { errors, isSubmitting }, watch, reset, control, } = useForm({ defaultValues, }); /** * @description pre-populate form with data if data is present */ useEffect(() => { if (!data) return; reset(data); }, [data, reset]); /** * @description pre-populate form with default values if data is not present */ useEffect(() => { if (data) return; reset({ ...defaultValues, group: selectedGroup ?? "backlog", }); }, [selectedGroup, data, reset]); const handleClose = () => { onClose(); reset({ name: "", color: "#000000", group: "backlog" }); }; const handleCreate = async (formData: IState) => { if (!workspaceSlug || !projectId || isSubmitting) return; await projectStateStore .createState(workspaceSlug.toString(), projectId.toString(), formData) .then(() => { handleClose(); setToastAlert({ type: "success", title: "Success!", message: "State created successfully.", }); }) .catch((error) => { if (error.status === 400) setToastAlert({ type: "error", title: "Error!", message: "State with that name already exists. Please try again with another name.", }); else setToastAlert({ type: "error", title: "Error!", message: "State could not be created. Please try again.", }); }); }; const handleUpdate = async (formData: IState) => { if (!workspaceSlug || !projectId || !data || isSubmitting) return; await projectStateStore .updateState(workspaceSlug.toString(), projectId.toString(), data.id, formData) .then(() => { mutate(STATES_LIST(projectId.toString())); handleClose(); setToastAlert({ type: "success", title: "Success!", message: "State updated successfully.", }); }) .catch((error) => { if (error.status === 400) setToastAlert({ type: "error", title: "Error!", message: "Another state exists with the same name. Please try again with another name.", }); else setToastAlert({ type: "error", title: "Error!", message: "State could not be updated. Please try again.", }); }); }; const onSubmit = async (formData: IState) => { const payload: IState = { ...formData, }; if (data) await handleUpdate(payload); else await handleCreate(payload); }; return (
{({ open }) => ( <> {watch("color") && watch("color") !== "" && ( )} ( onChange(value.hex)} /> )} /> )}
( )} /> {data && ( (
k === value.toString()) ? GROUP_CHOICES[value.toString() as keyof typeof GROUP_CHOICES] : "Select group" } input > {Object.keys(GROUP_CHOICES).map((key) => ( {GROUP_CHOICES[key as keyof typeof GROUP_CHOICES]} ))}
)} /> )} ( )} /> ); });