summaryrefslogtreecommitdiff
path: root/lib/tasks/table/update-task-sheet.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tasks/table/update-task-sheet.tsx')
-rw-r--r--lib/tasks/table/update-task-sheet.tsx230
1 files changed, 230 insertions, 0 deletions
diff --git a/lib/tasks/table/update-task-sheet.tsx b/lib/tasks/table/update-task-sheet.tsx
new file mode 100644
index 00000000..1f4f5aa8
--- /dev/null
+++ b/lib/tasks/table/update-task-sheet.tsx
@@ -0,0 +1,230 @@
+"use client"
+
+import * as React from "react"
+import { tasks, type Task } from "@/db/schema/tasks"
+import { zodResolver } from "@hookform/resolvers/zod"
+import { Loader } from "lucide-react"
+import { useForm } from "react-hook-form"
+import { toast } from "sonner"
+
+import { Button } from "@/components/ui/button"
+import {
+ Form,
+ FormControl,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@/components/ui/form"
+import {
+ Select,
+ SelectContent,
+ SelectGroup,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select"
+import {
+ Sheet,
+ SheetClose,
+ SheetContent,
+ SheetDescription,
+ SheetFooter,
+ SheetHeader,
+ SheetTitle,
+} from "@/components/ui/sheet"
+import { Textarea } from "@/components/ui/textarea"
+
+import { modifiTask } from "@/lib//tasks/service"
+import { updateTaskSchema, type UpdateTaskSchema } from "@/lib/tasks/validations"
+
+interface UpdateTaskSheetProps
+ extends React.ComponentPropsWithRef<typeof Sheet> {
+ task: Task | null
+}
+
+export function UpdateTaskSheet({ task, ...props }: UpdateTaskSheetProps) {
+ const [isUpdatePending, startUpdateTransition] = React.useTransition()
+
+ const form = useForm<UpdateTaskSchema>({
+ resolver: zodResolver(updateTaskSchema),
+ defaultValues: {
+ title: task?.title ?? "",
+ label: task?.label,
+ status: task?.status,
+ priority: task?.priority,
+ },
+ })
+
+ function onSubmit(input: UpdateTaskSchema) {
+ startUpdateTransition(async () => {
+ if (!task) return
+
+ const { error } = await modifiTask({
+ id: task.id,
+ ...input,
+ })
+
+ if (error) {
+ toast.error(error)
+ return
+ }
+
+ form.reset()
+ props.onOpenChange?.(false)
+ toast.success("Task updated")
+ })
+ }
+
+ return (
+ <Sheet {...props}>
+ <SheetContent className="flex flex-col gap-6 sm:max-w-md">
+ <SheetHeader className="text-left">
+ <SheetTitle>Update task</SheetTitle>
+ <SheetDescription>
+ Update the task details and save the changes
+ </SheetDescription>
+ </SheetHeader>
+ <Form {...form}>
+ <form
+ onSubmit={form.handleSubmit(onSubmit)}
+ className="flex flex-col gap-4"
+ >
+ <FormField
+ control={form.control}
+ name="title"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Title</FormLabel>
+ <FormControl>
+ <Textarea
+ placeholder="Do a kickflip"
+ className="resize-none"
+ {...field}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="label"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Label</FormLabel>
+ <Select
+ onValueChange={field.onChange}
+ defaultValue={field.value}
+ >
+ <FormControl>
+ <SelectTrigger className="capitalize">
+ <SelectValue placeholder="Select a label" />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ <SelectGroup>
+ {tasks.label.enumValues.map((item) => (
+ <SelectItem
+ key={item}
+ value={item}
+ className="capitalize"
+ >
+ {item}
+ </SelectItem>
+ ))}
+ </SelectGroup>
+ </SelectContent>
+ </Select>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="status"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Status</FormLabel>
+ <Select
+ onValueChange={field.onChange}
+ defaultValue={field.value}
+ >
+ <FormControl>
+ <SelectTrigger className="capitalize">
+ <SelectValue placeholder="Select a status" />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ <SelectGroup>
+ {tasks.status.enumValues.map((item) => (
+ <SelectItem
+ key={item}
+ value={item}
+ className="capitalize"
+ >
+ {item}
+ </SelectItem>
+ ))}
+ </SelectGroup>
+ </SelectContent>
+ </Select>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="priority"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Priority</FormLabel>
+ <Select
+ onValueChange={field.onChange}
+ defaultValue={field.value}
+ >
+ <FormControl>
+ <SelectTrigger className="capitalize">
+ <SelectValue placeholder="Select a priority" />
+ </SelectTrigger>
+ </FormControl>
+ <SelectContent>
+ <SelectGroup>
+ {tasks.priority.enumValues.map((item) => (
+ <SelectItem
+ key={item}
+ value={item}
+ className="capitalize"
+ >
+ {item}
+ </SelectItem>
+ ))}
+ </SelectGroup>
+ </SelectContent>
+ </Select>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <SheetFooter className="gap-2 pt-2 sm:space-x-0">
+ <SheetClose asChild>
+ <Button type="button" variant="outline">
+ Cancel
+ </Button>
+ </SheetClose>
+ <Button disabled={isUpdatePending}>
+ {isUpdatePending && (
+ <Loader
+ className="mr-2 size-4 animate-spin"
+ aria-hidden="true"
+ />
+ )}
+ Save
+ </Button>
+ </SheetFooter>
+ </form>
+ </Form>
+ </SheetContent>
+ </Sheet>
+ )
+}