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