feat: create new list immediately adds to the board

This commit is contained in:
Henry
2024-08-13 14:53:21 +01:00
parent 1e3a903211
commit 18de6f03d8
4 changed files with 108 additions and 81 deletions

View File

@@ -12,6 +12,7 @@ import { usePopup } from "~/providers/popup";
import { import {
type GetBoardByIdOutput, type GetBoardByIdOutput,
type NewCardInput, type NewCardInput,
type NewListInput,
type ReorderCardInput, type ReorderCardInput,
type ReorderListInput, type ReorderListInput,
} from "~/types/router.types"; } from "~/types/router.types";
@@ -22,6 +23,7 @@ interface BoardContextProps {
updateList: (params: ReorderListInput) => void; updateList: (params: ReorderListInput) => void;
updateCard: (params: ReorderCardInput) => void; updateCard: (params: ReorderCardInput) => void;
addCard: (params: NewCardInput) => void; addCard: (params: NewCardInput) => void;
addList: (params: NewListInput) => void;
refetchBoard: () => void; refetchBoard: () => void;
} }
@@ -129,6 +131,23 @@ export const BoardProvider: React.FC<{ children: ReactNode }> = ({
setBoardData({ ...boardData, lists: updatedLists }); 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 = ({ const updateList = ({
boardId, boardId,
listId, listId,
@@ -159,6 +178,7 @@ export const BoardProvider: React.FC<{ children: ReactNode }> = ({
updateList, updateList,
updateCard, updateCard,
addCard, addCard,
addList,
refetchBoard, refetchBoard,
}} }}
> >

View File

@@ -39,7 +39,7 @@ export default function ListDropdown({
<Menu.Item> <Menu.Item>
<button <button
onClick={handleOpenDeleteListConfirmation} onClick={handleOpenDeleteListConfirmation}
className="m-1 w-full rounded-[5px] px-3 py-2 text-left text-sm hover:bg-light-400 dark:hover:bg-dark-400" className="m-1 w-full rounded-[5px] px-3 py-2 text-left text-sm text-neutral-900 hover:bg-light-200 dark:text-dark-1000 dark:hover:bg-dark-400"
> >
Delete list Delete list
</button> </button>

View File

@@ -31,7 +31,6 @@ function classNames(...classes: string[]): string {
} }
export function NewCardForm({ listPublicId }: NewCardFormProps) { export function NewCardForm({ listPublicId }: NewCardFormProps) {
const utils = api.useUtils();
const { boardData, addCard, refetchBoard } = useBoard(); const { boardData, addCard, refetchBoard } = useBoard();
const { showPopup } = usePopup(); const { showPopup } = usePopup();
const { closeModal } = useModal(); const { closeModal } = useModal();

View File

@@ -1,44 +1,49 @@
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { api } from "~/utils/api"; import { api } from "~/utils/api";
import { useForm } from "react-hook-form";
import { HiXMark } from "react-icons/hi2"; import { HiXMark } from "react-icons/hi2";
import { Switch } from "@headlessui/react"; import { Switch } from "@headlessui/react";
import { useBoard } from "~/providers/board"; import { useBoard } from "~/providers/board";
import { useModal } from "~/providers/modal"; import { useModal } from "~/providers/modal";
import { usePopup } from "~/providers/popup";
import { type NewListInput } from "~/types/router.types"; import { type NewListInput } from "~/types/router.types";
import { Formik, Form, Field } from "formik";
function classNames(...classes: string[]): string { function classNames(...classes: string[]): string {
return classes.filter(Boolean).join(" "); return classes.filter(Boolean).join(" ");
} }
export function NewListForm({ boardPublicId }: { boardPublicId: string }) { type NewListFormInput = NewListInput & {
const utils = api.useUtils(); isCreateAnotherEnabled: boolean;
const { boardData } = useBoard(); };
const { closeModal } = useModal();
const [isCreateAnotherEnabled, setIsCreateAnotherEnabled] = useState(false);
const refetchBoard = async () => { export function NewListForm({ boardPublicId }: { boardPublicId: string }) {
if (boardData?.publicId) { const { refetchBoard, addList } = useBoard();
try { const { closeModal } = useModal();
await utils.board.byId.refetch({ boardPublicId: boardData.publicId }); const { showPopup } = usePopup();
} catch (e) {
console.error(e); const { register, handleSubmit, reset, setValue, watch } =
} useForm<NewListFormInput>({
} defaultValues: {
}; name: "",
boardPublicId: boardPublicId,
isCreateAnotherEnabled: false,
},
});
const isCreateAnotherEnabled = watch("isCreateAnotherEnabled");
const createList = api.list.create.useMutation({ const createList = api.list.create.useMutation({
onSuccess: () => { onSuccess: () => refetchBoard(),
try { onError: () => {
if (!isCreateAnotherEnabled) closeModal(); closeModal();
return refetchBoard(); refetchBoard();
} catch (e) { showPopup({
console.log(e); 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(); 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 ( return (
<> <>
<div className="flex w-full items-center justify-between pb-4"> <div className="flex w-full items-center justify-between pb-4">
@@ -62,65 +82,53 @@ export function NewListForm({ boardPublicId }: { boardPublicId: string }) {
</button> </button>
</div> </div>
<Formik <form onSubmit={handleSubmit(onSubmit)}>
initialValues={{ <label
name: "", htmlFor="list-name"
boardPublicId: "", className="block pb-2 text-sm font-normal leading-6 text-neutral-900 dark:text-dark-1000"
}} >
onSubmit={(values: NewListInput, { resetForm }) => { Name
createList.mutate({ </label>
name: values.name, <input
boardPublicId, id="list-name"
}); {...register("name", { required: true })}
resetForm(); className="block w-full rounded-md border-0 bg-dark-300 bg-white/5 py-1.5 text-neutral-900 shadow-sm ring-1 ring-inset ring-light-600 focus:ring-2 focus:ring-inset focus:ring-light-600 dark:text-dark-1000 dark:ring-dark-700 dark:focus:ring-dark-700 sm:text-sm sm:leading-6"
}} />
> <div className="mt-3 flex items-center justify-end">
<Form> <span className="mr-2 text-xs text-light-900 dark:text-dark-900">
<label Create more
htmlFor="list-name" </span>
className="block pb-2 text-sm font-normal leading-6 text-neutral-900 dark:text-dark-1000" <Switch
checked={isCreateAnotherEnabled}
onChange={() =>
setValue("isCreateAnotherEnabled", !isCreateAnotherEnabled)
}
className={classNames(
isCreateAnotherEnabled
? "bg-indigo-600"
: "bg-light-800 dark:bg-dark-800",
"relative inline-flex h-4 w-6 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none",
)}
> >
Name <span className="sr-only">Create more</span>
</label> <span
<Field aria-hidden="true"
id="list-name"
name="name"
className="block w-full rounded-md border-0 bg-dark-300 bg-white/5 py-1.5 text-neutral-900 shadow-sm ring-1 ring-inset ring-light-600 focus:ring-2 focus:ring-inset focus:ring-light-600 dark:text-dark-1000 dark:ring-dark-700 dark:focus:ring-dark-700 sm:text-sm sm:leading-6"
/>
<div className="mt-3 flex items-center justify-end">
<span className="mr-2 text-xs text-light-900 dark:text-dark-900">
Create more
</span>
<Switch
checked={isCreateAnotherEnabled}
onChange={setIsCreateAnotherEnabled}
className={classNames( className={classNames(
isCreateAnotherEnabled isCreateAnotherEnabled ? "translate-x-2" : "translate-x-0",
? "bg-indigo-600" "pointer-events-none inline-block h-3 w-3 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out",
: "bg-light-800 dark:bg-dark-800",
"relative inline-flex h-4 w-6 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none",
)} )}
> />
<span className="sr-only">Create more</span> </Switch>
<span </div>
aria-hidden="true" <div className="mt-5 sm:mt-6">
className={classNames( <button
isCreateAnotherEnabled ? "translate-x-2" : "translate-x-0", type="submit"
"pointer-events-none inline-block h-3 w-3 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out", className="inline-flex w-full justify-center rounded-md bg-light-1000 px-3 py-2 text-sm font-semibold text-light-50 shadow-sm focus-visible:outline-none dark:bg-dark-1000 dark:text-dark-50"
)} >
/> Create list
</Switch> </button>
</div> </div>
<div className="mt-5 sm:mt-6"> </form>
<button
type="submit"
className="inline-flex w-full justify-center rounded-md bg-light-1000 px-3 py-2 text-sm font-semibold text-light-50 shadow-sm focus-visible:outline-none dark:bg-dark-1000 dark:text-dark-50"
>
Create list
</button>
</div>
</Form>
</Formik>
</> </>
); );
} }