|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useState } from 'react' |
| 4 | +import { |
| 5 | + Chip, |
| 6 | + ChipCombobox, |
| 7 | + ChipDatePicker, |
| 8 | + ChipModal, |
| 9 | + ChipModalBody, |
| 10 | + ChipModalError, |
| 11 | + ChipModalField, |
| 12 | + ChipModalFooter, |
| 13 | + ChipModalHeader, |
| 14 | +} from '@sim/emcn' |
| 15 | +import type { Person, PersonBody } from '@/lib/api/contracts/people' |
| 16 | +import { memberProfileFieldsSchema, PROFILE_TEXT_MAX_LENGTH } from '@/lib/people/validation' |
| 17 | +import { usePeoplePicker } from '@/app/o/[organizationId]/people/use-people-picker' |
| 18 | +import { useUpdatePerson } from '@/hooks/queries/people' |
| 19 | + |
| 20 | +type TextField = 'title' | 'department' | 'location' |
| 21 | + |
| 22 | +/** The picker's value for no manager; membership ids are never this. */ |
| 23 | +const NO_MANAGER = 'no-manager' |
| 24 | + |
| 25 | +const TEXT_FIELDS: readonly { field: TextField; title: string; placeholder: string }[] = [ |
| 26 | + { field: 'title', title: 'Title', placeholder: 'Staff Engineer' }, |
| 27 | + { field: 'department', title: 'Department', placeholder: 'Engineering' }, |
| 28 | + { field: 'location', title: 'Location', placeholder: 'San Francisco' }, |
| 29 | +] |
| 30 | + |
| 31 | +interface PersonModalProps { |
| 32 | + organizationId: string |
| 33 | + person: Person |
| 34 | + onClose: () => void |
| 35 | +} |
| 36 | + |
| 37 | +/** Edits one person's profile; managers are chosen from the organization's people. */ |
| 38 | +export function PersonModal({ organizationId, person, onClose }: PersonModalProps) { |
| 39 | + const update = useUpdatePerson(organizationId) |
| 40 | + const [draft, setDraft] = useState<PersonBody>({ |
| 41 | + title: person.title, |
| 42 | + department: person.department, |
| 43 | + location: person.location, |
| 44 | + startDate: person.startDate, |
| 45 | + managerMemberId: person.managerMemberId, |
| 46 | + }) |
| 47 | + const [errors, setErrors] = useState<Partial<Record<keyof PersonBody, string>>>({}) |
| 48 | + /** |
| 49 | + * The chosen manager's name, kept with the choice: the search results may no longer include them. |
| 50 | + * Only a stored manager who is suspended has an id and no name. |
| 51 | + */ |
| 52 | + const [managerName, setManagerName] = useState(person.manager?.name ?? null) |
| 53 | + const { people, pickerProps } = usePeoplePicker(organizationId, 'members', person.memberId) |
| 54 | + const managerLabel = draft.managerMemberId ? (managerName ?? 'Suspended member') : 'No manager' |
| 55 | + |
| 56 | + const change = <K extends keyof PersonBody>(field: K, value: PersonBody[K]) => { |
| 57 | + setDraft((current) => ({ ...current, [field]: value })) |
| 58 | + setErrors((current) => ({ ...current, [field]: undefined })) |
| 59 | + if (update.isError) update.reset() |
| 60 | + } |
| 61 | + |
| 62 | + const save = () => { |
| 63 | + const parsed = memberProfileFieldsSchema.safeParse(draft) |
| 64 | + if (!parsed.success) { |
| 65 | + setErrors( |
| 66 | + Object.fromEntries( |
| 67 | + parsed.error.issues.map((issue) => [String(issue.path[0]), issue.message]) |
| 68 | + ) |
| 69 | + ) |
| 70 | + return |
| 71 | + } |
| 72 | + update.mutate({ memberId: person.memberId, body: parsed.data }, { onSuccess: onClose }) |
| 73 | + } |
| 74 | + |
| 75 | + const title = `Edit ${person.name}` |
| 76 | + return ( |
| 77 | + <ChipModal |
| 78 | + open |
| 79 | + onOpenChange={(open) => !open && onClose()} |
| 80 | + dismissDisabled={update.isPending} |
| 81 | + srTitle={title} |
| 82 | + > |
| 83 | + <ChipModalHeader onClose={onClose}>{title}</ChipModalHeader> |
| 84 | + <ChipModalBody> |
| 85 | + {TEXT_FIELDS.map(({ field, title: label, placeholder }) => ( |
| 86 | + <ChipModalField |
| 87 | + key={field} |
| 88 | + type='input' |
| 89 | + disabled={update.isPending} |
| 90 | + title={label} |
| 91 | + placeholder={placeholder} |
| 92 | + maxLength={PROFILE_TEXT_MAX_LENGTH} |
| 93 | + value={draft[field] ?? ''} |
| 94 | + onChange={(value) => change(field, value || null)} |
| 95 | + error={errors[field]} |
| 96 | + /> |
| 97 | + ))} |
| 98 | + <ChipModalField type='custom' title='Start date' error={errors.startDate}> |
| 99 | + {() => ( |
| 100 | + <div className='flex items-center gap-2'> |
| 101 | + <ChipDatePicker |
| 102 | + value={draft.startDate ?? undefined} |
| 103 | + onChange={(value) => change('startDate', value)} |
| 104 | + placeholder='Not recorded' |
| 105 | + disabled={update.isPending} |
| 106 | + fullWidth |
| 107 | + /> |
| 108 | + {draft.startDate && ( |
| 109 | + <Chip disabled={update.isPending} onClick={() => change('startDate', null)}> |
| 110 | + Clear |
| 111 | + </Chip> |
| 112 | + )} |
| 113 | + </div> |
| 114 | + )} |
| 115 | + </ChipModalField> |
| 116 | + <ChipModalField type='custom' title='Manager' error={errors.managerMemberId}> |
| 117 | + {() => ( |
| 118 | + <ChipCombobox |
| 119 | + {...pickerProps} |
| 120 | + aria-label='Manager' |
| 121 | + options={[{ value: NO_MANAGER, label: 'No manager' }, ...pickerProps.options]} |
| 122 | + value={draft.managerMemberId ?? NO_MANAGER} |
| 123 | + onChange={(value) => { |
| 124 | + const manager = people.find((candidate) => candidate.memberId === value) |
| 125 | + setManagerName(manager?.name ?? null) |
| 126 | + change('managerMemberId', manager?.memberId ?? null) |
| 127 | + }} |
| 128 | + overlayContent={managerLabel} |
| 129 | + searchPlaceholder='Search people...' |
| 130 | + emptyMessage='No one matches' |
| 131 | + disabled={update.isPending} |
| 132 | + className='w-full' |
| 133 | + /> |
| 134 | + )} |
| 135 | + </ChipModalField> |
| 136 | + <ChipModalError>{update.error?.message}</ChipModalError> |
| 137 | + </ChipModalBody> |
| 138 | + <ChipModalFooter |
| 139 | + onCancel={onClose} |
| 140 | + primaryAction={{ |
| 141 | + label: update.isPending ? 'Saving' : 'Save', |
| 142 | + disabled: update.isPending, |
| 143 | + onClick: save, |
| 144 | + }} |
| 145 | + /> |
| 146 | + </ChipModal> |
| 147 | + ) |
| 148 | +} |
0 commit comments