| 1 | import { zodResolver } from '@hookform/resolvers/zod' |
| 2 | import { format } |
| 3 | import { CalendarIcon, ChevronDown |
| 4 | import { type Dispatch, |
| 5 | import { Controller, useForm |
| 6 | import type z from |
| 7 | import { Button } |
| 8 | import { Calendar } |
| 9 | import { |
| 10 | Collapsible, |
| 11 | CollapsibleContent, |
| 12 | CollapsibleTrigger, |
| 13 | } from '@/components/ui/collapsible' |
| 14 | import { |
| 15 | Dialog, |
| 16 | DialogClose, |
| 17 | DialogContent, |
| 18 | DialogFooter, |
| 19 | DialogHeader, |
| 20 | DialogTitle, |
| 21 | } from '@/components/ui/dialog' |
| 22 | import { |
| 23 | Field, |
| 24 | FieldDescription, |
| 25 | FieldError, |
| 26 | FieldGroup, |
| 27 | FieldLabel, |
| 28 | } from '@/components/ui/field' |
| 29 | import { Input } |
| 30 | import { |
| 31 | Popover, |
| 32 | PopoverContent, |
| 33 | PopoverTrigger, |
| 34 | } from '@/components/ui/popover' |
| 35 | import { Textarea } |
| 36 | import { cn } |
| 37 | import { createProjectSchema } |
| 38 | |
| 39 | export default function CreateProjectDialog |
| 40 | dialogOpen, |
| 41 | setDialogOpen, |
| 42 | orgSlug, |
| 43 | organizationId, |
| 44 | handleSubmit, |
| 45 | isPending, |
| 46 | }: { |
| 47 | dialogOpen: boolean |
| 48 | setDialogOpen: Dispatch |
| 49 | organizationId: string |
| 50 | orgSlug: string |
| 51 | handleSubmit: (values |
| 52 | isPending: boolean |
| 53 | }) { |
| 54 | const [advancedOpen, |
| 55 | |
| 56 | const form = |
| 57 | resolver: zodResolver(createProjectSchema |
| 58 | defaultValues: { |
| 59 | organizationId, |
| 60 | orgSlug, |
| 61 | name: '', |
| 62 | description: '', |
| 63 | invoiceFromName: '', |
| 64 | invoiceFromAddress: '', |
| 65 | invoiceToName: '', |
| 66 | invoiceToAddress: '', |
| 67 | }, |
| 68 | }) |
| 69 | |
| 70 | useEffect(() => |
| 71 | if (!dialogOpen) |
| 72 | form.reset({ |
| 73 | organizationId, |
| 74 | orgSlug, |
| 75 | name: '', |
| 76 | description: '', |
| 77 | dueDate: undefined, |
| 78 | invoiceFromName: '', |
| 79 | invoiceFromAddress: '', |
| 80 | invoiceToName: '', |
| 81 | invoiceToAddress: '', |
| 82 | }) |
| 83 | setAdvancedOpen(false) |
| 84 | } |
| 85 | }, [dialogOpen, |
| 86 | |
| 87 | return ( |
| 88 | <Dialog onOpenChange={ |
| 89 | <DialogContent> |
| 90 | <DialogHeader> |
| 91 | <DialogTitle>Create a new project</DialogTitle |
| 92 | </DialogHeader> |
| 93 | <form className='space-y-4 |
| 94 | <FieldGroup> |
| 95 | <Controller |
| 96 | control={form.control |
| 97 | name='name' |
| 98 | render={({ field |
| 99 | <Field |
| 100 | className='col-span-full gap-1' |
| 101 | data-invalid={fieldState.invalid |
| 102 | > |
| 103 | <FieldLabel>Name</FieldLabel |
| 104 | <Input |
| 105 | {...field} |
| 106 | aria-invalid={fieldState.invalid |
| 107 | autoComplete='off' |
| 108 | autoFocus |
| 109 | name='project-title' |
| 110 | onChange={(e) |
| 111 | field.onChange(e |
| 112 | }} |
| 113 | placeholder='My Project' |
| 114 | /> |
| 115 | <FieldDescription>The name of your project.</FieldDescription |
| 116 | {fieldState.invalid && |
| 117 | <FieldError errors={ |
| 118 | )} |
| 119 | </Field> |
| 120 | )} |
| 121 | /> |
| 122 | |
| 123 | <Controller |
| 124 | control={form.control |
| 125 | name='description' |
| 126 | render={({ field |
| 127 | <Field |
| 128 | className='col-span-full gap-1' |
| 129 | data-invalid={fieldState.invalid |
| 130 | > |
| 131 | <FieldLabel>Description</FieldLabel |
| 132 | <Input |
| 133 | {...field} |
| 134 | aria-invalid={fieldState.invalid |
| 135 | autoComplete='off' |
| 136 | name='project-description' |
| 137 | placeholder='Optional description' |
| 138 | /> |
| 139 | <FieldDescription> |
| 140 | A brief description of what this project is about. |
| 141 | </FieldDescription> |
| 142 | {fieldState.invalid && |
| 143 | <FieldError errors={ |
| 144 | )} |
| 145 | </Field> |
| 146 | )} |
| 147 | /> |
| 148 | <Controller |
| 149 | control={form.control |
| 150 | name='dueDate' |
| 151 | render={({ field |
| 152 | <Field |
| 153 | className='col-span-full gap-1' |
| 154 | data-invalid={fieldState.invalid |
| 155 | > |
| 156 | <FieldLabel>Due Date</FieldLabel |
| 157 | <Popover> |
| 158 | <PopoverTrigger asChild> |
| 159 | <Button |
| 160 | className={cn( |
| 161 | 'w-full justify-start text-left font-normal', |
| 162 | !field.value && |
| 163 | )} |
| 164 | size='lg' |
| 165 | type='button' |
| 166 | variant='outline' |
| 167 | > |
| 168 | <CalendarIcon className=' |
| 169 | {field.value |
| 170 | ? format(field. |
| 171 | : 'Pick a date'} |
| 172 | </Button> |
| 173 | </PopoverTrigger> |
| 174 | <PopoverContent align=' |
| 175 | <Calendar |
| 176 | disabled={(date) |
| 177 | mode='single' |
| 178 | onSelect={field.onChange |
| 179 | selected={field.value |
| 180 | /> |
| 181 | </PopoverContent> |
| 182 | </Popover> |
| 183 | <FieldDescription> |
| 184 | Optional deadline for this project. |
| 185 | </FieldDescription> |
| 186 | {fieldState.invalid && |
| 187 | <FieldError errors={ |
| 188 | )} |
| 189 | </Field> |
| 190 | )} |
| 191 | /> |
| 192 | |
| 193 | <Collapsible |
| 194 | className='col-span-full' |
| 195 | onOpenChange={setAdvancedOpen} |
| 196 | open={advancedOpen} |
| 197 | > |
| 198 | <CollapsibleTrigger asChild> |
| 199 | <Button |
| 200 | className='w-full justify-between font-medium' |
| 201 | type='button' |
| 202 | variant='ghost' |
| 203 | > |
| 204 | Advanced |
| 205 | <ChevronDown |
| 206 | className={cn( |
| 207 | 'size-4 transition-transform', |
| 208 | advancedOpen && 'rotate-180' |
| 209 | )} |
| 210 | /> |
| 211 | </Button> |
| 212 | </CollapsibleTrigger> |
| 213 | <CollapsibleContent className=' |
| 214 | <Controller |
| 215 | control={form.control |
| 216 | name='invoiceFromName' |
| 217 | render={({ field |
| 218 | <Field className=' |
| 219 | <FieldLabel>From Name</FieldLabel |
| 220 | <Input |
| 221 | {...field} |
| 222 | aria-invalid={fieldState.invalid |
| 223 | autoComplete='off' |
| 224 | placeholder='Acme Inc.' |
| 225 | value={field.value |
| 226 | /> |
| 227 | <FieldDescription> |
| 228 | The sender name shown on this project's invoices. |
| 229 | </FieldDescription> |
| 230 | {fieldState.invalid && |
| 231 | <FieldError errors={ |
| 232 | )} |
| 233 | </Field> |
| 234 | )} |
| 235 | /> |
| 236 | <Controller |
| 237 | control={form.control |
| 238 | name='invoiceFromAddress' |
| 239 | render={({ field |
| 240 | <Field className=' |
| 241 | <FieldLabel>From Address</FieldLabel |
| 242 | <Textarea |
| 243 | {...field} |
| 244 | aria-invalid={fieldState.invalid |
| 245 | placeholder={'Ticklemore Street, Devon, UK'} |
| 246 | rows={3} |
| 247 | value={field.value |
| 248 | /> |
| 249 | <FieldDescription> |
| 250 | The sender address shown on this project's invoices. |
| 251 | </FieldDescription> |
| 252 | {fieldState.invalid && |
| 253 | <FieldError errors={ |
| 254 | )} |
| 255 | </Field> |
| 256 | )} |
| 257 | /> |
| 258 | <Controller |
| 259 | control={form.control |
| 260 | name='invoiceToName' |
| 261 | render={({ field |
| 262 | <Field className=' |
| 263 | <FieldLabel>Bill To Name</FieldLabel |
| 264 | <Input |
| 265 | {...field} |
| 266 | aria-invalid={fieldState.invalid |
| 267 | autoComplete='off' |
| 268 | placeholder='Client / company name' |
| 269 | value={field.value |
| 270 | /> |
| 271 | <FieldDescription> |
| 272 | The client name billed on this project's invoices. |
| 273 | </FieldDescription> |
| 274 | {fieldState.invalid && |
| 275 | <FieldError errors={ |
| 276 | )} |
| 277 | </Field> |
| 278 | )} |
| 279 | /> |
| 280 | <Controller |
| 281 | control={form.control |
| 282 | name='invoiceToAddress' |
| 283 | render={({ field |
| 284 | <Field className=' |
| 285 | <FieldLabel>Bill To Address</FieldLabel |
| 286 | <Textarea |
| 287 | {...field} |
| 288 | aria-invalid={fieldState.invalid |
| 289 | placeholder={'Ticklemore Street, Devon, UK'} |
| 290 | rows={3} |
| 291 | value={field.value |
| 292 | /> |
| 293 | <FieldDescription> |
| 294 | The client address billed on this project's invoices. |
| 295 | </FieldDescription> |
| 296 | {fieldState.invalid && |
| 297 | <FieldError errors={ |
| 298 | )} |
| 299 | </Field> |
| 300 | )} |
| 301 | /> |
| 302 | </CollapsibleContent> |
| 303 | </Collapsible> |
| 304 | </FieldGroup> |
| 305 | <DialogFooter> |
| 306 | <Button loading={ |
| 307 | Create Project |
| 308 | </Button> |
| 309 | <DialogClose asChild> |
| 310 | <Button type=' |
| 311 | Cancel |
| 312 | </Button> |
| 313 | </DialogClose> |
| 314 | </DialogFooter> |
| 315 | </form> |
| 316 | </DialogContent> |
| 317 | </Dialog> |
| 318 | ) |
| 319 | } |