import { useEffect } from "react"; import { useForm } from "react-hook-form"; import ContentEditable from "react-contenteditable"; import { api } from "~/utils/api"; import { HiXMark, HiOutlineBarsArrowDown, HiOutlineBarsArrowUp, } from "react-icons/hi2"; import { Switch } from "@headlessui/react"; import CheckboxDropdown from "~/components/CheckboxDropdown"; import { useBoard } from "~/providers/board"; import { useModal } from "~/providers/modal"; import { usePopup } from "~/providers/popup"; import { type NewCardInput } from "~/types/router.types"; type NewCardFormInput = NewCardInput & { isCreateAnotherEnabled: boolean; }; interface NewCardFormProps { listPublicId: string; } function classNames(...classes: string[]): string { return classes.filter(Boolean).join(" "); } export function NewCardForm({ listPublicId }: NewCardFormProps) { const { boardData, addCard, refetchBoard } = useBoard(); const { showPopup } = usePopup(); const { closeModal } = useModal(); const { register, handleSubmit, reset, setValue, watch } = useForm({ defaultValues: { title: "", description: "", listPublicId, labelPublicIds: [], memberPublicIds: [], isCreateAnotherEnabled: false, position: "start", }, }); const labelPublicIds = watch("labelPublicIds") || []; const memberPublicIds = watch("memberPublicIds") || []; const isCreateAnotherEnabled = watch("isCreateAnotherEnabled"); const position = watch("position"); const createCard = api.card.create.useMutation({ onSuccess: async () => { await refetchBoard(); }, onError: async () => { closeModal(); await refetchBoard(); showPopup({ header: "Unable to create card", message: "Please try again later, or contact customer support.", }); }, }); useEffect(() => { const titleElement: HTMLElement | null = document?.querySelector("#title"); if (titleElement) titleElement.focus(); }, []); const formattedLabels = boardData?.labels.map((label) => ({ key: label.publicId, value: label.name, selected: labelPublicIds.includes(label.publicId), })) ?? []; const formattedLists = boardData?.lists.map((list) => ({ key: list.publicId, value: list.name, selected: list.publicId === watch("listPublicId"), })) ?? []; const formattedMembers = boardData?.workspace?.members?.map((member) => ({ key: member.publicId, value: member.user?.name ?? "", selected: memberPublicIds.includes(member.publicId), })) ?? []; const onSubmit = (data: NewCardInput) => { addCard(data); const isCreateAnotherEnabled = watch("isCreateAnotherEnabled"); if (!isCreateAnotherEnabled) closeModal(); reset({ title: "", listPublicId: watch("listPublicId"), labelPublicIds: [], memberPublicIds: [], isCreateAnotherEnabled, }); createCard.mutate({ title: data.title, description: data.description, listPublicId: data.listPublicId, labelPublicIds: data.labelPublicIds, memberPublicIds: data.memberPublicIds, position: data.position, }); }; const handleToggleCreateAnother = (): void => { setValue("isCreateAnotherEnabled", !isCreateAnotherEnabled); }; const handleSelectList = (listPublicId: string): void => { setValue("listPublicId", listPublicId); }; const handleSelectMembers = (memberPublicId: string): void => { const currentIndex = memberPublicIds.indexOf(memberPublicId); if (currentIndex === -1) { setValue("memberPublicIds", [...memberPublicIds, memberPublicId]); } else { const newMemberPublicIds = [...memberPublicIds]; newMemberPublicIds.splice(currentIndex, 1); setValue("memberPublicIds", newMemberPublicIds); } }; const handleSelectLabels = (labelPublicId: string): void => { const currentIndex = labelPublicIds.indexOf(labelPublicId); if (currentIndex === -1) { setValue("labelPublicIds", [...labelPublicIds, labelPublicId]); } else { const newLabelPublicIds = [...labelPublicIds]; newLabelPublicIds.splice(currentIndex, 1); setValue("labelPublicIds", newLabelPublicIds); } }; const selectedList = formattedLists.find((item) => item.selected); return (

New card

setValue("description", e.target.value)} className="block min-h-[70px] w-full rounded-md border-0 bg-dark-300 bg-white/5 px-3 py-1.5 text-light-900 text-neutral-900 shadow-sm ring-1 ring-inset ring-light-600 focus:ring-2 focus:ring-inset focus:ring-light-600 focus-visible:outline-none dark:text-dark-1000 dark:ring-dark-700 dark:focus:ring-dark-700 sm:text-sm sm:leading-6" />
handleSelectList(list.key) } >
{selectedList?.value}
handleSelectMembers(list.key) } >
{!memberPublicIds.length ? ( "Members" ) : (
{memberPublicIds.map((memberPublicId) => { const member = formattedMembers.find( (member) => member.key === memberPublicId, ); return ( {member?.value ?.split(" ") .map((namePart) => namePart.charAt(0).toUpperCase(), ) .join("")} ); })}
)}
handleSelectLabels(list.key) } >
{!labelPublicIds.length ? ( "Labels" ) : ( <>
1 ? "flex -space-x-[2px] overflow-hidden" : "flex items-center" } > {labelPublicIds.map((labelPublicId) => { const label = boardData?.labels.find( (label) => label.publicId === labelPublicId, ); return ( <> {labelPublicIds.length === 1 && (
{label?.name}
)} ); })}
{labelPublicIds.length > 1 && (
{`${labelPublicIds.length} labels`}
)} )}
Create more Create another
); }