| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { Plus, Trash2 |
| 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 | createOrgCustomFieldAction, |
| 22 | deleteOrgCustomFieldAction, |
| 23 | updateOrgCustomFieldAction, |
| 24 | } from '../actions' |
| 25 | |
| 26 | export function OrgCustomFieldsCard({ |
| 27 | organizationId, |
| 28 | customFields, |
| 29 | }: { |
| 30 | organizationId: string |
| 31 | customFields: CustomFieldDefinition |
| 32 | }) { |
| 33 | const router = |
| 34 | const [editing, |
| 35 | const [creating, |
| 36 | const [deleting, |
| 37 | |
| 38 | const createAction = |
| 39 | onSuccess() { |
| 40 | toast.success('Field added |
| 41 | setCreating(false) |
| 42 | router.refresh() |
| 43 | }, |
| 44 | onError({ error |
| 45 | toast.error(error |
| 46 | }, |
| 47 | }) |
| 48 | |
| 49 | const updateAction = |
| 50 | onSuccess() { |
| 51 | toast.success('Field updated |
| 52 | setEditing(null) |
| 53 | router.refresh() |
| 54 | }, |
| 55 | onError({ error |
| 56 | toast.error(error |
| 57 | }, |
| 58 | }) |
| 59 | |
| 60 | const deleteAction = |
| 61 | onSuccess() { |
| 62 | toast.success('Field deleted |
| 63 | setDeleting(null) |
| 64 | router.refresh() |
| 65 | }, |
| 66 | onError({ error |
| 67 | toast.error(error |
| 68 | }, |
| 69 | }) |
| 70 | |
| 71 | return ( |
| 72 | <> |
| 73 | <Card> |
| 74 | <CardHeader className=' |
| 75 | <div className='flex flex-col gap-2 |
| 76 | <CardTitle>Custom fields</CardTitle |
| 77 | <CardDescription> |
| 78 | Templates copied into every new project. Existing projects are not |
| 79 | affected by changes here. |
| 80 | </CardDescription> |
| 81 | </div> |
| 82 | <Button onClick={ |
| 83 | <Plus className=' |
| 84 | Add field |
| 85 | </Button> |
| 86 | </CardHeader> |
| 87 | <CardContent> |
| 88 | {customFields.length === |
| 89 | <p className='text-center text-muted-foreground text-sm |
| 90 | No custom fields yet. |
| 91 | </p> |
| 92 | ) : ( |
| 93 | <ul className='space-y-2 |
| 94 | {customFields.map(( |
| 95 | <li |
| 96 | className='flex items-center justify-between gap-3 rounded-md border bg-card p-3 hover:bg-accent/30' |
| 97 | key={f.id |
| 98 | > |
| 99 | <button |
| 100 | className='flex-1 text-left' |
| 101 | onClick={() => |
| 102 | type='button' |
| 103 | > |
| 104 | <div className='flex items-center gap-2 |
| 105 | <span className='font-medium |
| 106 | <Badge variant=' |
| 107 | {f.required && |
| 108 | {f.visibleToClient && |
| 109 | <Badge variant=' |
| 110 | )} |
| 111 | </div> |
| 112 | {f.type === |
| 113 | <p className='mt-1 text-muted-foreground text-xs |
| 114 | {f.options.join |
| 115 | </p> |
| 116 | )} |
| 117 | </button> |
| 118 | <Button |
| 119 | aria-label={`Delete custom field ${f |
| 120 | onClick={() => |
| 121 | size='icon' |
| 122 | variant='ghost' |
| 123 | > |
| 124 | <Trash2 className=' |
| 125 | </Button> |
| 126 | </li> |
| 127 | ))} |
| 128 | </ul> |
| 129 | )} |
| 130 | </CardContent> |
| 131 | </Card> |
| 132 | |
| 133 | <CustomFieldEditorDialog |
| 134 | isPending={createAction.isPending |
| 135 | mode='create' |
| 136 | onOpenChange={setCreating} |
| 137 | onSubmit={(payload) |
| 138 | createAction.execute({ |
| 139 | organizationId, |
| 140 | definition: payload, |
| 141 | }) |
| 142 | }} |
| 143 | open={creating} |
| 144 | /> |
| 145 | |
| 146 | <CustomFieldEditorDialog |
| 147 | initial={editing ?? |
| 148 | isPending={updateAction.isPending |
| 149 | mode='edit' |
| 150 | onOpenChange={(open) |
| 151 | if (!open) { |
| 152 | setEditing(null) |
| 153 | } |
| 154 | }} |
| 155 | onSubmit={(payload) |
| 156 | if (!editing) { |
| 157 | return |
| 158 | } |
| 159 | updateAction.execute({ |
| 160 | organizationId, |
| 161 | fieldId: editing.id |
| 162 | definition: payload, |
| 163 | }) |
| 164 | }} |
| 165 | open={!!editing} |
| 166 | /> |
| 167 | |
| 168 | <ConfirmDeleteDialog |
| 169 | description={ |
| 170 | <> |
| 171 | Delete custom field{' '} |
| 172 | <span className='font-semibold |
| 173 | cannot be undone. |
| 174 | </> |
| 175 | } |
| 176 | loading={deleteAction.isPending |
| 177 | onConfirm={() => |
| 178 | if (!deleting) { |
| 179 | return |
| 180 | } |
| 181 | deleteAction.execute({ |
| 182 | }} |
| 183 | onOpenChange={(open) |
| 184 | if (!open) { |
| 185 | setDeleting(null) |
| 186 | } |
| 187 | }} |
| 188 | open={!!deleting} |
| 189 | title='Delete custom field' |
| 190 | /> |
| 191 | </> |
| 192 | ) |
| 193 | } |