| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { zodResolver } |
| 5 | import { Save } |
| 6 | import { useAction } |
| 7 | import { useState } |
| 8 | import { Controller, useForm |
| 9 | import { toast } |
| 10 | import type { z |
| 11 | import { Button } |
| 12 | import { |
| 13 | Card, |
| 14 | CardContent, |
| 15 | CardDescription, |
| 16 | CardFooter, |
| 17 | CardHeader, |
| 18 | CardTitle, |
| 19 | } from '@/components/ui/card' |
| 20 | import { Checkbox } |
| 21 | import DatePicker from '@/components/ui/date-picker |
| 22 | import { |
| 23 | Field, |
| 24 | FieldDescription, |
| 25 | FieldError, |
| 26 | FieldGroup, |
| 27 | FieldLabel, |
| 28 | } from '@/components/ui/field' |
| 29 | import { Input } |
| 30 | import { renameProjectAction } |
| 31 | import { renameProjectSchema } |
| 32 | |
| 33 | type RenameFormValues |
| 34 | |
| 35 | export function GeneralCard({ |
| 36 | project, |
| 37 | organizationId, |
| 38 | orgSlug, |
| 39 | }: { |
| 40 | project: { |
| 41 | organizationId: string |
| 42 | orgSlug: string |
| 43 | }) { |
| 44 | const router = |
| 45 | |
| 46 | const form = |
| 47 | resolver: zodResolver(renameProjectSchema |
| 48 | defaultValues: { |
| 49 | projectId: project.id |
| 50 | organizationId, |
| 51 | name: project.name |
| 52 | slug: project.slug |
| 53 | dueDate: project.dueDate |
| 54 | }, |
| 55 | }) |
| 56 | |
| 57 | const [slugAcknowledged, |
| 58 | const watchedSlug = |
| 59 | const slugChanged = |
| 60 | |
| 61 | const { execute, |
| 62 | onSuccess({ data |
| 63 | toast.success('Project updated |
| 64 | if (data?.slug |
| 65 | router.replace(`/ |
| 66 | } else { |
| 67 | router.refresh() |
| 68 | } |
| 69 | }, |
| 70 | onError({ error |
| 71 | toast.error(error |
| 72 | }, |
| 73 | }) |
| 74 | |
| 75 | function onSubmit(data |
| 76 | execute({ |
| 77 | projectId: data.projectId |
| 78 | organizationId: data.organizationId |
| 79 | name: data.name |
| 80 | slug: data.slug |
| 81 | dueDate: data.dueDate |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | return ( |
| 86 | <Card> |
| 87 | <CardHeader> |
| 88 | <CardTitle>General</CardTitle |
| 89 | <CardDescription> |
| 90 | Manage your project name, URL slug and due date. |
| 91 | </CardDescription> |
| 92 | </CardHeader> |
| 93 | <form onSubmit={form |
| 94 | <CardContent> |
| 95 | <FieldGroup className=' |
| 96 | <Controller |
| 97 | control={form.control |
| 98 | name='name' |
| 99 | render={({ field |
| 100 | <Field className=' |
| 101 | <FieldLabel>Project Name</FieldLabel |
| 102 | <Input |
| 103 | {...field} |
| 104 | aria-invalid={fieldState.invalid |
| 105 | placeholder='My Project' |
| 106 | /> |
| 107 | {fieldState.invalid && |
| 108 | <FieldError errors={ |
| 109 | )} |
| 110 | </Field> |
| 111 | )} |
| 112 | /> |
| 113 | <Controller |
| 114 | control={form.control |
| 115 | name='slug' |
| 116 | render={({ field |
| 117 | <Field className=' |
| 118 | <FieldLabel>URL Slug</FieldLabel |
| 119 | <Input |
| 120 | {...field} |
| 121 | aria-invalid={fieldState.invalid |
| 122 | onChange={(e) |
| 123 | const newSlug = |
| 124 | .toLowerCase() |
| 125 | .replace(/[^a-z0-9 |
| 126 | field.onChange(newSlug |
| 127 | if (newSlug === project |
| 128 | setSlugAcknowledged(false) |
| 129 | } |
| 130 | }} |
| 131 | placeholder='my-project' |
| 132 | /> |
| 133 | <FieldDescription> |
| 134 | Your project will be accessible at /{orgSlug}/{field |
| 135 | </FieldDescription> |
| 136 | {fieldState.invalid && |
| 137 | <FieldError errors={ |
| 138 | )} |
| 139 | </Field> |
| 140 | )} |
| 141 | /> |
| 142 | <Controller |
| 143 | control={form.control |
| 144 | name='dueDate' |
| 145 | render={({ field |
| 146 | <Field className=' |
| 147 | <FieldLabel>Due Date</FieldLabel |
| 148 | <DatePicker |
| 149 | onChange={(date) |
| 150 | value={field.value |
| 151 | /> |
| 152 | <FieldDescription> |
| 153 | Optional target date for project completion. |
| 154 | </FieldDescription> |
| 155 | {fieldState.invalid && |
| 156 | <FieldError errors={ |
| 157 | )} |
| 158 | </Field> |
| 159 | )} |
| 160 | /> |
| 161 | {slugChanged && ( |
| 162 | <div className='rounded-lg border border-amber-500/50 bg-amber-500/5 p-3 |
| 163 | <div className='flex items-start gap-3 |
| 164 | <Checkbox |
| 165 | checked={slugAcknowledged} |
| 166 | id='slug-ack' |
| 167 | onCheckedChange={(checked) |
| 168 | setSlugAcknowledged(checked === |
| 169 | } |
| 170 | /> |
| 171 | <label className='text-sm leading-snug |
| 172 | I acknowledge that by changing the slug of this project, |
| 173 | links in previously sent emails and notifications will stop |
| 174 | working, as they still point to{' '} |
| 175 | <span className='font-medium |
| 176 | /{orgSlug}/{project |
| 177 | </span> |
| 178 | . |
| 179 | </label> |
| 180 | </div> |
| 181 | </div> |
| 182 | )} |
| 183 | </FieldGroup> |
| 184 | </CardContent> |
| 185 | <CardFooter> |
| 186 | <Button |
| 187 | className='mt-4' |
| 188 | disabled={ |
| 189 | !form.formState.isDirty |
| 190 | } |
| 191 | loading={isPending} |
| 192 | type='submit' |
| 193 | > |
| 194 | <Save className=' |
| 195 | Save Changes |
| 196 | </Button> |
| 197 | </CardFooter> |
| 198 | </form> |
| 199 | </Card> |
| 200 | ) |
| 201 | } |