import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { HiXMark } from "react-icons/hi2"; import { useBoard } from "~/providers/board"; import { useModal } from "~/providers/modal"; import { usePopup } from "~/providers/popup"; import { api } from "~/utils/api"; import Button from "~/components/Button"; import Input from "~/components/Input"; import Toggle from "~/components/Toggle"; import { type NewListInput } from "~/types/router.types"; type NewListFormInput = NewListInput & { isCreateAnotherEnabled: boolean; }; export function NewListForm({ boardPublicId }: { boardPublicId: string }) { const { refetchBoard, addList } = useBoard(); const { closeModal } = useModal(); const { showPopup } = usePopup(); const { register, handleSubmit, reset, setValue, watch } = useForm({ defaultValues: { name: "", boardPublicId: boardPublicId, isCreateAnotherEnabled: false, }, }); const isCreateAnotherEnabled = watch("isCreateAnotherEnabled"); const createList = api.list.create.useMutation({ onSuccess: async () => { await refetchBoard(); }, onError: async () => { closeModal(); await refetchBoard(); showPopup({ header: "Unable to create list", message: "Please try again later, or contact customer support.", }); }, }); useEffect(() => { const nameElement: HTMLElement | null = document?.querySelector("#list-name"); if (nameElement) nameElement.focus(); }, []); const onSubmit = (data: NewListInput) => { addList(data); const isCreateAnotherEnabled = watch("isCreateAnotherEnabled"); if (!isCreateAnotherEnabled) closeModal(); reset({ name: "", isCreateAnotherEnabled, }); createList.mutate({ name: data.name, boardPublicId, }); }; return (

New list

setValue("isCreateAnotherEnabled", !isCreateAnotherEnabled) } />
); }