Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added Features: Form Reset, Conditional Modal Behavior, and Adhered t… #46

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 113 additions & 75 deletions src/components/TaskForm/CreateTaskDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use client"

import React, { useRef } from "react"
import { Button } from "@/components/ui/button"
import {
Dialog,
Expand All @@ -25,20 +24,23 @@ import { useMediaQuery } from "@/hooks/useMediaQuery"
import { TaskInsert } from "@/lib/supabase/types"
import { createTask } from "@/services/tasks/api"
import { PlusIcon, ReloadIcon } from "@radix-ui/react-icons"
import * as React from "react"
import { toast } from "sonner"
import { TaskForm } from "./TaskForm"
import { Checkbox } from "@/components/ui/checkbox"

type Props = {
projectId: string
}

export function CreateTaskDialog({ projectId }: Props) {
const [open, setOpen] = React.useState(false)
const [createAnother, setCreateAnother] = React.useState(false)
const isDesktop = useMediaQuery("(min-width: 640px)")

const [isCreatePending, startCreateTransition] = React.useTransition()
const formRef = useRef<any>(null)

const onSubmit = (taskInsert: TaskInsert) => {
const onSubmit = async (taskInsert: TaskInsert) => {
startCreateTransition(async () => {
const { error } = await createTask(taskInsert)

Expand All @@ -47,88 +49,124 @@ export function CreateTaskDialog({ projectId }: Props) {
return
}

setOpen(false)
toast.success("Task created")
if (createAnother) {
formRef.current?.resetForm()
toast.success("Task created. Ready to create another task.")
} else {
setOpen(false)
toast.success("Task created")
}
})
}
const handleCheckboxChange = (checked: boolean | "indeterminate") => {
setCreateAnother(checked === true)
}

const renderDialogContent = () => (
<>
<DialogHeader>
<DialogTitle>Create task</DialogTitle>
<DialogDescription>
Fill in the details below to create a new task.
</DialogDescription>
</DialogHeader>
<TaskForm
ref={formRef}
task={{ projectId, title: "" }}
onSubmit={onSubmit}
>
{({ canSubmit, isSubmitting }) => (
<DialogFooter className="gap-2 pt-4 sm:space-x-0">
<div className="flex items-center space-x-2">
<Checkbox
checked={createAnother}
onCheckedChange={handleCheckboxChange}
className="border border-gray-500"
/>
<span className="text-sm font-medium">Create Another Task</span>
</div>
<DialogClose asChild>
<Button type="button" variant="outline">
Cancel
</Button>
</DialogClose>
<Button disabled={!canSubmit || isCreatePending}>
{isSubmitting ||
(isCreatePending && (
<ReloadIcon
className="mr-2 size-4 animate-spin"
aria-hidden="true"
/>
))}
Create
</Button>
</DialogFooter>
)}
</TaskForm>
</>
)

if (isDesktop)
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="outline" size="sm">
<PlusIcon className="mr-2 size-4" aria-hidden="true" />
New task
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Create task</DialogTitle>
<DialogDescription>
Fill in the details below to create a new task.
</DialogDescription>
</DialogHeader>
<TaskForm task={{ projectId, title: "" }} onSubmit={onSubmit}>
{({ canSubmit, isSubmitting }) => (
<DialogFooter className="gap-2 pt-4 sm:space-x-0">
<DialogClose asChild>
<Button type="button" variant="outline">
Cancel
</Button>
</DialogClose>
<Button disabled={!canSubmit || isCreatePending}>
{isSubmitting ||
(isCreatePending && (
<ReloadIcon
className="mr-2 size-4 animate-spin"
aria-hidden="true"
/>
))}
Create
</Button>
</DialogFooter>
)}
</TaskForm>
</DialogContent>
</Dialog>
)
const renderDrawerContent = () => (
<>
<DrawerHeader>
<DrawerTitle>Create task</DrawerTitle>
<DrawerDescription>
Fill in the details below to create a new task.
</DrawerDescription>
</DrawerHeader>
<TaskForm
ref={formRef}
task={{ projectId, title: "" }}
onSubmit={onSubmit}
>
{({ canSubmit, isSubmitting }) => (
<DrawerFooter className="gap-2 sm:space-x-0">
<div className="flex items-center space-x-2">
<Checkbox
checked={createAnother}
onCheckedChange={handleCheckboxChange}
className="border border-gray-500"
/>
<span className="text-sm font-medium">Create Another Task</span>
</div>
<DrawerClose asChild>
<Button variant="outline">Cancel</Button>
</DrawerClose>
<Button disabled={!canSubmit || isCreatePending}>
{isSubmitting ||
(isCreatePending && (
<ReloadIcon
className="mr-2 size-4 animate-spin"
aria-hidden="true"
/>
))}
Create
</Button>
</DrawerFooter>
)}
</TaskForm>
</>
)

return (
return isDesktop ? (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="outline" size="sm">
<PlusIcon className="mr-2 size-4" aria-hidden="true" />
New task
</Button>
</DialogTrigger>
<DialogContent>{renderDialogContent()}</DialogContent>
</Dialog>
) : (
<Drawer open={open} onOpenChange={setOpen}>
<DrawerTrigger asChild>
<Button variant="outline" size="sm">
<PlusIcon className="mr-2 size-4" aria-hidden="true" />
New task
</Button>
</DrawerTrigger>

<DrawerContent>
<DrawerHeader>
<DrawerTitle>Create task</DrawerTitle>
<DrawerDescription>
Fill in the details below to create a new task.
</DrawerDescription>
</DrawerHeader>
<TaskForm task={{ projectId, title: "" }} onSubmit={onSubmit}>
{({ canSubmit, isSubmitting }) => (
<DrawerFooter className="gap-2 sm:space-x-0">
<DrawerClose asChild>
<Button variant="outline">Cancel</Button>
</DrawerClose>
<Button disabled={!canSubmit || isCreatePending}>
{isSubmitting ||
(isCreatePending && (
<ReloadIcon
className="mr-2 size-4 animate-spin"
aria-hidden="true"
/>
))}
Create
</Button>
</DrawerFooter>
)}
</TaskForm>
</DrawerContent>
<DrawerContent>{renderDrawerContent()}</DrawerContent>
</Drawer>
)
}
129 changes: 68 additions & 61 deletions src/components/TaskForm/TaskForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { Task, TaskInsert } from "@/lib/supabase/types"
import { TaskStatus, TaskStatusValues } from "@/types/schemas"
import { useForm } from "@tanstack/react-form"
import { ReactNode } from "react"
import { ReactNode, useImperativeHandle, forwardRef } from "react"
import { Input } from "../ui/input"
import { Label } from "../ui/label"
import { getStatusIcon } from "@/lib/utils"
Expand All @@ -28,40 +28,46 @@ type Props = {
}) => ReactNode
}

export const TaskForm = ({ task, onSubmit, children }: Props) => {
const form = useForm({
defaultValues: {
title: task.title ?? "",
status: (task.status ?? "") as TaskStatus | "",
deadline: task.deadline ? new Date(task.deadline) : ("" as const),
},
onSubmit: ({ value }) =>
onSubmit({
title: value.title,
projectId: task.projectId,
status: value.status !== "" ? value.status : undefined,
deadline:
value.deadline !== "" ? value.deadline.toDateString() : undefined,
}),
})
export const TaskForm = forwardRef(
({ task, onSubmit, children }: Props, ref) => {
const form = useForm({
defaultValues: {
title: task.title ?? "",
status: (task.status ?? "") as TaskStatus | "",
deadline: task.deadline ? new Date(task.deadline) : ("" as const),
},
onSubmit: ({ value }) =>
onSubmit({
title: value.title,
projectId: task.projectId,
status: value.status !== "" ? value.status : undefined,
deadline:
value.deadline !== "" ? value.deadline.toDateString() : undefined,
}),
})

return (
<form
onSubmit={(e) => {
e.preventDefault()
form.handleSubmit()
}}
className="flex flex-col gap-4"
>
<form.Field
name="title"
validators={{
onChange: ({ value }) =>
value.length < 1 ? "Title is required" : undefined,
useImperativeHandle(ref, () => ({
resetForm() {
form.reset()
},
}))

return (
<form
onSubmit={(e) => {
e.preventDefault()
form.handleSubmit()
}}
className="flex flex-col gap-4"
>
{(field) => {
return (
<form.Field
name="title"
validators={{
onChange: ({ value }) =>
value.length < 1 ? "Title is required" : undefined,
}}
>
{(field) => (
<div className="flex flex-col gap-2">
<Label htmlFor={"task-title"}>Title</Label>
<Input
Expand All @@ -79,18 +85,16 @@ export const TaskForm = ({ task, onSubmit, children }: Props) => {
</p>
))}
</div>
)
}}
</form.Field>
<form.Field
name="status"
validators={{
onChange: ({ value }) =>
value === "" ? "Status is required" : undefined,
}}
>
{(field) => {
return (
)}
</form.Field>
<form.Field
name="status"
validators={{
onChange: ({ value }) =>
value === "" ? "Status is required" : undefined,
}}
>
{(field) => (
<div className="flex flex-col gap-2">
<Label htmlFor={"task-status"}>Status</Label>
<Select
Expand Down Expand Up @@ -125,12 +129,10 @@ export const TaskForm = ({ task, onSubmit, children }: Props) => {
</p>
))}
</div>
)
}}
</form.Field>
<form.Field name="deadline">
{(field) => {
return (
)}
</form.Field>
<form.Field name="deadline">
{(field) => (
<div className="flex flex-col gap-2">
<Label htmlFor={"task-deadline"}>Deadline</Label>
<DatePicker
Expand All @@ -151,15 +153,20 @@ export const TaskForm = ({ task, onSubmit, children }: Props) => {
</p>
))}
</div>
)
}}
</form.Field>
)}
</form.Field>

<form.Subscribe
selector={(formState) => [formState.canSubmit, formState.isSubmitting]}
>
{([canSubmit, isSubmitting]) => children({ canSubmit, isSubmitting })}
</form.Subscribe>
</form>
)
}
<form.Subscribe
selector={(formState) => [
formState.canSubmit,
formState.isSubmitting,
]}
>
{([canSubmit, isSubmitting]) => children({ canSubmit, isSubmitting })}
</form.Subscribe>
</form>
)
},
)

TaskForm.displayName = "TaskForm"