| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { Download, Plus |
| 5 | import { useAction } |
| 6 | import { useState } |
| 7 | import { toast } |
| 8 | import { ConfirmDeleteDialog } |
| 9 | import { CustomFieldEditorDialog } |
| 10 | import { Badge } |
| 11 | import { Button } |
| 12 | import { |
| 13 | Card, |
| 14 | CardContent, |
| 15 | CardDescription, |
| 16 | CardHeader, |
| 17 | CardTitle, |
| 18 | } from '@/components/ui/card' |
| 19 | import type { CustomFieldDefinition |
| 20 | import { |
| 21 | createProjectCustomFieldAction, |
| 22 | deleteProjectCustomFieldAction, |
| 23 | updateProjectCustomFieldAction, |
| 24 | } from '../actions' |
| 25 | import { ImportOrgFieldsDialog } |
| 26 | |
| 27 | export function ProjectCustomFieldsCard({ |
| 28 | organizationId, |
| 29 | projectId, |
| 30 | projectFields, |
| 31 | orgFields, |
| 32 | }: { |
| 33 | organizationId: string |
| 34 | projectId: string |
| 35 | projectFields: CustomFieldDefinition |
| 36 | orgFields: CustomFieldDefinition |
| 37 | }) { |
| 38 | const router = |
| 39 | const [editing, |
| 40 | const [creating, |
| 41 | const [importing, |
| 42 | const [deleting, |
| 43 | |
| 44 | const createAction = |
| 45 | onSuccess() { |
| 46 | toast.success('Field added |
| 47 | setCreating(false) |
| 48 | router.refresh() |
| 49 | }, |
| 50 | onError({ error |
| 51 | toast.error(error |
| 52 | }, |
| 53 | }) |
| 54 | |
| 55 | const updateAction = |
| 56 | onSuccess() { |
| 57 | toast.success('Field updated |
| 58 | setEditing(null) |
| 59 | router.refresh() |
| 60 | }, |
| 61 | onError({ error |
| 62 | toast.error(error |
| 63 | }, |
| 64 | }) |
| 65 | |
| 66 | const deleteAction = |
| 67 | onSuccess() { |
| 68 | toast.success('Field deleted |
| 69 | setDeleting(null) |
| 70 | router.refresh() |
| 71 | }, |
| 72 | onError({ error |
| 73 | toast.error(error |
| 74 | }, |
| 75 | }) |
| 76 | |
| 77 | return ( |
| 78 | <> |
| 79 | <Card> |
| 80 | <CardHeader className=' |
| 81 | <div className='flex flex-col gap-2 |
| 82 | <CardTitle>Custom fields</CardTitle |
| 83 | <CardDescription> |
| 84 | Extra fields shown on the timesheet form for this project. |
| 85 | </CardDescription> |
| 86 | </div> |
| 87 | <Button onClick={ |
| 88 | <Plus /> |
| 89 | Add field |
| 90 | </Button> |
| 91 | </CardHeader> |
| 92 | <CardContent> |
| 93 | {projectFields.length === |
| 94 | <div className='flex flex-col items-center gap-3 py-10 text-center text-sm |
| 95 | <p className='text-muted-foreground |
| 96 | |
| 97 | <Button onClick={ |
| 98 | <Download /> |
| 99 | Import from workspace |
| 100 | </Button> |
| 101 | </div> |
| 102 | ) : ( |
| 103 | <> |
| 104 | <ul className='space-y-2 |
| 105 | {projectFields.map(( |
| 106 | <li |
| 107 | className='flex items-center justify-between gap-3 rounded-md border bg-card p-3 hover:bg-accent/30' |
| 108 | key={f.id |
| 109 | > |
| 110 | <button |
| 111 | className='flex-1 text-left' |
| 112 | onClick={() => |
| 113 | type='button' |
| 114 | > |
| 115 | <div className='flex items-center gap-2 |
| 116 | <span className='font-medium |
| 117 | <Badge variant=' |
| 118 | {f.required && |
| 119 | <Badge variant=' |
| 120 | )} |
| 121 | {f.visibleToClient && |
| 122 | <Badge variant=' |
| 123 | )} |
| 124 | </div> |
| 125 | {f.type === |
| 126 | <p className='mt-1 text-muted-foreground text-xs |
| 127 | {f.options.join |
| 128 | </p> |
| 129 | )} |
| 130 | </button> |
| 131 | <Button |
| 132 | aria-label={`Delete custom field ${f |
| 133 | onClick={() => |
| 134 | size='icon' |
| 135 | variant='ghost' |
| 136 | > |
| 137 | <Trash2 className=' |
| 138 | </Button> |
| 139 | </li> |
| 140 | ))} |
| 141 | </ul> |
| 142 | <div className='mt-4 |
| 143 | <Button |
| 144 | onClick={() => |
| 145 | size='sm' |
| 146 | variant='outline' |
| 147 | > |
| 148 | <Download /> |
| 149 | Import from workspace |
| 150 | </Button> |
| 151 | </div> |
| 152 | </> |
| 153 | )} |
| 154 | </CardContent> |
| 155 | </Card> |
| 156 | |
| 157 | <CustomFieldEditorDialog |
| 158 | isPending={createAction.isPending |
| 159 | mode='create' |
| 160 | onOpenChange={setCreating} |
| 161 | onSubmit={(payload) |
| 162 | createAction.execute({ |
| 163 | organizationId, |
| 164 | projectId, |
| 165 | definition: payload, |
| 166 | }) |
| 167 | }} |
| 168 | open={creating} |
| 169 | /> |
| 170 | |
| 171 | <CustomFieldEditorDialog |
| 172 | initial={editing ?? |
| 173 | isPending={updateAction.isPending |
| 174 | mode='edit' |
| 175 | onOpenChange={(open) |
| 176 | if (!open) { |
| 177 | setEditing(null) |
| 178 | } |
| 179 | }} |
| 180 | onSubmit={(payload) |
| 181 | if (!editing) { |
| 182 | return |
| 183 | } |
| 184 | updateAction.execute({ |
| 185 | organizationId, |
| 186 | projectId, |
| 187 | fieldId: editing.id |
| 188 | definition: payload, |
| 189 | }) |
| 190 | }} |
| 191 | open={!!editing} |
| 192 | /> |
| 193 | |
| 194 | <ImportOrgFieldsDialog |
| 195 | onOpenChange={setImporting} |
| 196 | open={importing} |
| 197 | organizationId={organizationId} |
| 198 | orgFields={orgFields} |
| 199 | projectFields={projectFields} |
| 200 | projectId={projectId} |
| 201 | /> |
| 202 | |
| 203 | <ConfirmDeleteDialog |
| 204 | description={ |
| 205 | <> |
| 206 | Delete custom field{' '} |
| 207 | <span className='font-semibold |
| 208 | on existing entries will be removed. |
| 209 | </> |
| 210 | } |
| 211 | loading={deleteAction.isPending |
| 212 | onConfirm={() => |
| 213 | if (!deleting) { |
| 214 | return |
| 215 | } |
| 216 | deleteAction.execute({ |
| 217 | organizationId, |
| 218 | projectId, |
| 219 | fieldId: deleting.id |
| 220 | }) |
| 221 | }} |
| 222 | onOpenChange={(open) |
| 223 | if (!open) { |
| 224 | setDeleting(null) |
| 225 | } |
| 226 | }} |
| 227 | open={!!deleting} |
| 228 | title='Delete custom field' |
| 229 | /> |
| 230 | </> |
| 231 | ) |
| 232 | } |