feat: update board name

This commit is contained in:
Henry
2023-11-28 23:15:39 +00:00
parent 143245eaf8
commit e1e0ea1615
3 changed files with 67 additions and 21 deletions

View File

@@ -10,6 +10,7 @@ import {
type DropResult, type DropResult,
Draggable, Draggable,
} from "react-beautiful-dnd"; } from "react-beautiful-dnd";
import { useFormik } from "formik";
import { api } from "~/trpc/react"; import { api } from "~/trpc/react";
import { useBoard } from "~/app/providers/board"; import { useBoard } from "~/app/providers/board";
@@ -29,6 +30,11 @@ interface Card {
title: string; title: string;
} }
interface FormValues {
boardId: string;
name: string;
}
type PublicListId = string; type PublicListId = string;
export default function BoardPage() { export default function BoardPage() {
@@ -38,7 +44,23 @@ export default function BoardPage() {
const [selectedPublicListId, setSelectedPublicListId] = const [selectedPublicListId, setSelectedPublicListId] =
useState<PublicListId>(""); useState<PublicListId>("");
const boardId = params?.id?.length && params.id[0]; 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 <></>; if (!boardId) return <></>;
@@ -113,9 +135,20 @@ export default function BoardPage() {
return ( return (
<div> <div>
<div className="mb-8 flex w-full justify-between"> <div className="mb-8 flex w-full justify-between">
<h1 className="font-medium tracking-tight text-dark-1000 sm:text-[1.2rem]"> <form
{boardData?.name} onSubmit={formik.handleSubmit}
</h1> className="focus-visible:outline-none"
>
<input
type="name"
id="name"
name="name"
value={formik.values.name}
onChange={formik.handleChange}
onBlur={formik.submitForm}
className="block border-0 bg-transparent p-0 py-1.5 font-medium tracking-tight text-dark-1000 focus:ring-0 focus-visible:outline-none sm:text-[1.2rem] sm:leading-6"
/>
</form>
<div> <div>
<button <button
type="button" type="button"

View File

@@ -74,4 +74,17 @@ export const boardRouter = createTRPCRouter({
createdBy: userId, createdBy: userId,
}); });
}), }),
update: publicProcedure
.input(
z.object({
boardId: z.string().min(12),
name: z.string().min(1),
}))
.mutation(({ ctx, input }) => {
const userId = ctx.session?.user.id;
if (!userId) return;
return ctx.db.update(boards).set({ name: input.name }).where(eq(boards.publicId, input.boardId));
}),
}); });