import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { api } from "~/utils/api"; import { HiXMark } from "react-icons/hi2"; import { Switch } from "@headlessui/react"; import CheckboxDropdown from "~/components/CheckboxDropdown"; import { useBoard } from "~/providers/board"; import { useModal } from "~/providers/modal"; interface FormData { title: string; listPublicId: string; labelPublicIds: string[]; memberPublicIds: string[]; isCreateAnotherEnabled: boolean; } interface NewCardFormProps { listPublicId: string; } function classNames(...classes: string[]): string { return classes.filter(Boolean).join(" "); } export function NewCardForm({ listPublicId }: NewCardFormProps) { const utils = api.useUtils(); const { boardData } = useBoard(); const { closeModal } = useModal(); const { register, handleSubmit, reset, setValue, watch } = useForm({ defaultValues: { title: "", listPublicId, labelPublicIds: [], memberPublicIds: [], isCreateAnotherEnabled: false, }, }); const labelPublicIds = watch("labelPublicIds") || []; const memberPublicIds = watch("memberPublicIds") || []; const isCreateAnotherEnabled = watch("isCreateAnotherEnabled"); const refetchBoard = () => utils.board.byId.refetch({ id: boardData.publicId }); const createCard = api.card.create.useMutation({ onSuccess: async () => { try { await refetchBoard(); if (!isCreateAnotherEnabled) closeModal(); reset({ title: "", listPublicId: watch("listPublicId"), labelPublicIds: [], memberPublicIds: [], isCreateAnotherEnabled: watch("isCreateAnotherEnabled"), }); } catch (e) { console.log(e); } }, }); 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: FormData) => { createCard.mutate({ title: data.title, listPublicId: data.listPublicId, labelsPublicIds: data.labelPublicIds, memberPublicIds: data.memberPublicIds, }); }; 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

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
); }