From 18de6f03d86ab8000cd32595a9de4a4f9d2f48b4 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 13 Aug 2024 14:53:21 +0100 Subject: [PATCH] feat: create new list immediately adds to the board --- src/providers/board.tsx | 20 +++ src/views/board/components/ListDropdown.tsx | 2 +- src/views/board/components/NewCardForm.tsx | 1 - src/views/board/components/NewListForm.tsx | 166 ++++++++++---------- 4 files changed, 108 insertions(+), 81 deletions(-) diff --git a/src/providers/board.tsx b/src/providers/board.tsx index 98fcab88..c4772176 100644 --- a/src/providers/board.tsx +++ b/src/providers/board.tsx @@ -12,6 +12,7 @@ import { usePopup } from "~/providers/popup"; import { type GetBoardByIdOutput, type NewCardInput, + type NewListInput, type ReorderCardInput, type ReorderListInput, } from "~/types/router.types"; @@ -22,6 +23,7 @@ interface BoardContextProps { updateList: (params: ReorderListInput) => void; updateCard: (params: ReorderCardInput) => void; addCard: (params: NewCardInput) => void; + addList: (params: NewListInput) => void; refetchBoard: () => void; } @@ -129,6 +131,23 @@ export const BoardProvider: React.FC<{ children: ReactNode }> = ({ setBoardData({ ...boardData, lists: updatedLists }); }; + const addList = ({ name, boardPublicId }: NewListInput) => { + if (!boardData) return; + + const newList = { + publicId: generateUID(), + name, + boardId: 1, + boardPublicId, + cards: [], + index: boardData.lists.length, + }; + + const updatedLists = [...boardData.lists, newList]; + + setBoardData({ ...boardData, lists: updatedLists }); + }; + const updateList = ({ boardId, listId, @@ -159,6 +178,7 @@ export const BoardProvider: React.FC<{ children: ReactNode }> = ({ updateList, updateCard, addCard, + addList, refetchBoard, }} > diff --git a/src/views/board/components/ListDropdown.tsx b/src/views/board/components/ListDropdown.tsx index b62846d5..8114fd7c 100644 --- a/src/views/board/components/ListDropdown.tsx +++ b/src/views/board/components/ListDropdown.tsx @@ -39,7 +39,7 @@ export default function ListDropdown({ diff --git a/src/views/board/components/NewCardForm.tsx b/src/views/board/components/NewCardForm.tsx index 861ce2bd..11fdef58 100644 --- a/src/views/board/components/NewCardForm.tsx +++ b/src/views/board/components/NewCardForm.tsx @@ -31,7 +31,6 @@ function classNames(...classes: string[]): string { } export function NewCardForm({ listPublicId }: NewCardFormProps) { - const utils = api.useUtils(); const { boardData, addCard, refetchBoard } = useBoard(); const { showPopup } = usePopup(); const { closeModal } = useModal(); diff --git a/src/views/board/components/NewListForm.tsx b/src/views/board/components/NewListForm.tsx index 5ea3f3f4..b47ef9c2 100644 --- a/src/views/board/components/NewListForm.tsx +++ b/src/views/board/components/NewListForm.tsx @@ -1,44 +1,49 @@ import { useState, useEffect } from "react"; import { api } from "~/utils/api"; +import { useForm } from "react-hook-form"; import { HiXMark } from "react-icons/hi2"; import { Switch } from "@headlessui/react"; import { useBoard } from "~/providers/board"; import { useModal } from "~/providers/modal"; +import { usePopup } from "~/providers/popup"; import { type NewListInput } from "~/types/router.types"; -import { Formik, Form, Field } from "formik"; - function classNames(...classes: string[]): string { return classes.filter(Boolean).join(" "); } -export function NewListForm({ boardPublicId }: { boardPublicId: string }) { - const utils = api.useUtils(); - const { boardData } = useBoard(); - const { closeModal } = useModal(); - const [isCreateAnotherEnabled, setIsCreateAnotherEnabled] = useState(false); +type NewListFormInput = NewListInput & { + isCreateAnotherEnabled: boolean; +}; - const refetchBoard = async () => { - if (boardData?.publicId) { - try { - await utils.board.byId.refetch({ boardPublicId: boardData.publicId }); - } catch (e) { - console.error(e); - } - } - }; +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: () => { - try { - if (!isCreateAnotherEnabled) closeModal(); - return refetchBoard(); - } catch (e) { - console.log(e); - } + onSuccess: () => refetchBoard(), + onError: () => { + closeModal(); + refetchBoard(); + showPopup({ + header: "Unable to create list", + message: "Please try again later, or contact customer support.", + }); }, }); @@ -48,6 +53,21 @@ export function NewListForm({ boardPublicId }: { boardPublicId: string }) { 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 ( <>
@@ -62,65 +82,53 @@ export function NewListForm({ boardPublicId }: { boardPublicId: string }) {
- { - createList.mutate({ - name: values.name, - boardPublicId, - }); - resetForm(); - }} - > -
-