"use client"; import { useState } from "react"; import Link from "next/link"; import { useParams } from "next/navigation"; import { HiOutlinePlusSmall } from "react-icons/hi2"; import { DragDropContext, Droppable, type DropResult, Draggable, } from "react-beautiful-dnd"; import { useFormik } from "formik"; import { api } from "~/trpc/react"; import { useBoard } from "~/app/providers/board"; import { useModal } from "~/app/providers/modal"; import Modal from "~/app/_components/modal"; import { NewCardForm } from "./NewCardForm"; import { NewListForm } from "./NewListForm"; interface List { publicId: string; name: string; cards?: Card[]; } interface Card { publicId: string; title: string; } interface FormValues { boardId: string; name: string; } type PublicListId = string; export default function BoardPage() { const params = useParams(); const { boardData, setBoardData, updateCard, updateList } = useBoard(); const { openModal, modalContentType } = useModal(); const [selectedPublicListId, setSelectedPublicListId] = useState(""); const boardId = params?.id?.length ? params.id[0] : null; const updateBoard = api.board.update.useMutation(); const formik = useFormik({ initialValues: { boardId: boardId ?? "", name: boardData?.name ? boardData.name : "", }, onSubmit: (values: FormValues) => { updateBoard.mutate({ boardId: values.boardId, name: values.name, }); }, enableReinitialize: true, }); if (!boardId) return <>; api.board.byId.useQuery( { id: boardId }, { onSuccess: (data) => { if (data) setBoardData(data); }, }, ); const openNewListForm = (publicBoardId: string) => { openModal("NEW_LIST"); setSelectedPublicListId(publicBoardId); }; const openNewCardForm = (publicListId: PublicListId) => { openModal("NEW_CARD"); setSelectedPublicListId(publicListId); }; const onDragEnd = ({ source, destination, draggableId, type, }: DropResult): void => { if (!destination) { return; } if (type === "LIST") { const updatedLists = Array.from(boardData.lists); const removedList = updatedLists.splice(source.index, 1)[0]; if (removedList) { updatedLists.splice(destination.index, 0, removedList); setBoardData({ ...boardData, lists: updatedLists }); } updateList({ boardId, listId: draggableId, currentIndex: source.index, newIndex: destination.index, }); } if (type === "CARD") { const updatedLists = Array.from(boardData.lists); const sourceList = updatedLists.find( (list) => list.publicId === source.droppableId, ); const destinationList = updatedLists.find( (list) => list.publicId === destination.droppableId, ); const removedCard = sourceList?.cards.splice(source.index, 1)[0]; if (sourceList && destinationList && removedCard) { destinationList.cards.splice(destination.index, 0, removedCard); setBoardData({ ...boardData, lists: updatedLists }); } updateCard({ cardId: draggableId, currentListId: source.droppableId, newListId: destination.droppableId, currentIndex: source.index, newIndex: destination.index, }); } }; return (
{(provided) => (
{boardData?.lists?.map((list: List, index) => ( {(provided) => (

{list.name}

{(provided) => (
{list.cards?.map((card, index) => ( {(provided) => ( {card.title} )} ))} {provided.placeholder}
)}
)}
))}
{provided.placeholder}
)}
{modalContentType === "NEW_CARD" && ( )} {modalContentType === "NEW_LIST" && ( )}
); }