Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

CreateOrEditTodoItems.tsx

145 lines4.4 KB

1import { generateKeyBetween } from "fractional-indexing";
2import { nanoid } from "nanoid";
3import { useCallback, useState } from "react";
4import z from "zod";
5import { todoItemsCollection } from "@/collections/todoItems";
6import { Button } from "@/components/ui/button";
7import {
8 Dialog,
9 DialogClose,
10 DialogContent,
11 DialogFooter,
12 DialogHeader,
13 DialogTitle,
14 DialogTrigger,
15} from "@/components/ui/dialog";
16import type { TodoItemRecord } from "@/db/schema";
17import { useAppForm } from "@/hooks/app.form";
18​
19export function CreateOrEditTodoItems({
20 todoItem,
21 children,
22}: {
23 todoItem: Partial<TodoItemRecord> &
24 Pick<TodoItemRecord, "boardId" | "projectId">;
25 children: React.ReactNode;
26}) {
27 const [isOpen, setIsOpen] = useState(false);
28 const isNewItem = !todoItem.id;
29 const actionName = isNewItem ? "Create Task" : "Edit Task";
30​
31 const form = useAppForm({
32 defaultValues: {
33 title: todoItem.title || "",
34 description: todoItem.description || "",
35 },
36 onSubmit: async ({ value }) => {
37 const itemId = todoItem.id || nanoid();
38​
39 // NOTE: It'd be better to use a manual transaction here to ensure both operations
40 // succeed or fail together. However, we use D1 for the db, and it doesn't suppor
41 // transactions yet, so we can't make an endpoint that does both of these operations
42​
43 if (isNewItem) {
44 // Find the first position in the board to prepend the new item
45 const itemsInBoard = todoItemsCollection.toArray
46 .filter((item) => item.boardId === todoItem.boardId)
47 .sort((a, b) => (a.position < b.position ? -1 : 1));
48​
49 const firstPosition = itemsInBoard[0]?.position;
50 const newPosition = generateKeyBetween(null, firstPosition ?? null);
51​
52 todoItemsCollection.insert({
53 id: itemId,
54 boardId: todoItem.boardId,
55 projectId: todoItem.projectId,
56 title: value.title,
57 description: value.description,
58 position: newPosition,
59 priority: 0,
60 createdAt: new Date(),
61 });
62 } else {
63 todoItemsCollection.update(itemId, (item) => {
64 item.title = value.title;
65 item.description = value.description;
66 });
67 }
68​
69 setIsOpen(false);
70 },
71 });
72​
73 const handleOpenChange = useCallback(
74 (newVal: boolean) => {
75 if (newVal) {
76 setIsOpen(true);
77 } else {
78 form.reset();
79 setIsOpen(false);
80 }
81 },
82 [form],
83 );
84​
85 return (
86 <Dialog open={isOpen} onOpenChange={handleOpenChange}>
87 <DialogTrigger asChild>{children}</DialogTrigger>
88 <DialogContent
89 className="sm:max-w-[425px]"
90 onKeyDown={(e) => {
91 // Prevent <SPACE> from dragging the draggable task
92 // when the dialog is open
93 e.stopPropagation();
94​
95 // Cmd/Ctrl + Enter to submit the form
96 if ((e.metaKey || e.ctrlKey) && e.key === "Enter") {
97 form.handleSubmit();
98 }
99 }}
100 >
101 <DialogHeader>
102 <DialogTitle>{actionName}</DialogTitle>
103 </DialogHeader>
104 <form
105 onSubmit={(e) => {
106 e.preventDefault();
107 form.handleSubmit(e);
108 }}
109 >
110 <div className="grid gap-4 py-4">
111 <form.AppField
112 name="title"
113 validators={{
114 onSubmit: z.string().min(1, "Title is required"),
115 }}
116 children={(field) => <field.TextField label="Title" />}
117 />
118​
119 {/* <Label htmlFor="name" className="text-right"> */}
120 {/* Title */}
121 {/* </Label> */}
122 {/* <Input id="name" value="Pedro Duarte" className="col-span-3" /> */}
123 <form.AppField
124 name="description"
125 validators={{
126 onSubmit: z.string().min(1, "Description is required"),
127 }}
128 children={(field) => <field.TextArea label="Description" />}
129 />
130 {/* <Label htmlFor="username" className="text-right"> */}
131 {/* Description */}
132 {/* </Label> */}
133 {/* <Input id="username" value="@peduarte" className="col-span-3" /> */}
134 </div>
135 <DialogFooter>
136 <DialogClose asChild>
137 <Button variant="outline">Cancel</Button>
138 </DialogClose>
139 <Button type="submit">Save changes</Button>
140 </DialogFooter>
141 </form>
142 </DialogContent>
143 </Dialog>
144 );