|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { |
| 4 | + Button, |
| 5 | + Input, |
| 6 | + Select, |
| 7 | + SelectContent, |
| 8 | + SelectItem, |
| 9 | + SelectTrigger, |
| 10 | + SelectValue, |
| 11 | + Text, |
| 12 | +} from '@trycompai/design-system'; |
| 13 | +import { Upload } from '@trycompai/design-system/icons'; |
| 14 | +import { useRef, useState, type ChangeEvent, type DragEvent } from 'react'; |
| 15 | +import { |
| 16 | + FormFooterInfo, |
| 17 | + FormFooterRow, |
| 18 | + LabelRow, |
| 19 | +} from './BackgroundCheckFormHelpers'; |
| 20 | + |
| 21 | +export const VENDOR_OPTIONS = [ |
| 22 | + { value: 'checkr', label: 'Checkr' }, |
| 23 | + { value: 'sterling', label: 'Sterling' }, |
| 24 | + { value: 'hireright', label: 'HireRight' }, |
| 25 | + { value: 'goodhire', label: 'Goodhire' }, |
| 26 | + { value: 'other', label: 'Other' }, |
| 27 | +] as const; |
| 28 | + |
| 29 | +export interface AttachFormValues { |
| 30 | + vendor: string; |
| 31 | + reportDate: string; |
| 32 | + file: File | null; |
| 33 | +} |
| 34 | + |
| 35 | +interface AttachFormProps { |
| 36 | + values: AttachFormValues; |
| 37 | + onChange: (next: AttachFormValues) => void; |
| 38 | + onSubmit: () => void; |
| 39 | + onBack?: () => void; |
| 40 | + submitting: boolean; |
| 41 | + canSubmit: boolean; |
| 42 | +} |
| 43 | + |
| 44 | +const MAX_FILE_BYTES = 25 * 1024 * 1024; |
| 45 | + |
| 46 | +export function BackgroundCheckAttachForm({ |
| 47 | + values, |
| 48 | + onChange, |
| 49 | + onSubmit, |
| 50 | + onBack, |
| 51 | + submitting, |
| 52 | + canSubmit, |
| 53 | +}: AttachFormProps) { |
| 54 | + const inputRef = useRef<HTMLInputElement | null>(null); |
| 55 | + const [dragOver, setDragOver] = useState(false); |
| 56 | + const [fileError, setFileError] = useState<string | null>(null); |
| 57 | + |
| 58 | + const setField = <K extends keyof AttachFormValues>(key: K, value: AttachFormValues[K]) => { |
| 59 | + onChange({ ...values, [key]: value }); |
| 60 | + }; |
| 61 | + |
| 62 | + const acceptFile = (file: File | undefined) => { |
| 63 | + if (!file) return; |
| 64 | + if (file.size > MAX_FILE_BYTES) { |
| 65 | + setFileError('File exceeds 25 MB limit.'); |
| 66 | + return; |
| 67 | + } |
| 68 | + if (file.type && file.type !== 'application/pdf') { |
| 69 | + setFileError('Only PDF files are accepted.'); |
| 70 | + return; |
| 71 | + } |
| 72 | + setFileError(null); |
| 73 | + setField('file', file); |
| 74 | + }; |
| 75 | + |
| 76 | + const handleFileChange = (event: ChangeEvent<HTMLInputElement>) => { |
| 77 | + acceptFile(event.target.files?.[0]); |
| 78 | + }; |
| 79 | + |
| 80 | + const handleBrowse = () => inputRef.current?.click(); |
| 81 | + |
| 82 | + const handleDrop = (event: DragEvent<HTMLDivElement>) => { |
| 83 | + event.preventDefault(); |
| 84 | + setDragOver(false); |
| 85 | + acceptFile(event.dataTransfer.files?.[0]); |
| 86 | + }; |
| 87 | + |
| 88 | + return ( |
| 89 | + <form noValidate onSubmit={(event) => event.preventDefault()}> |
| 90 | + <div className="mb-4 grid gap-4 md:grid-cols-2"> |
| 91 | + <div> |
| 92 | + <LabelRow htmlFor="bg-attach-vendor" required> |
| 93 | + Vendor |
| 94 | + </LabelRow> |
| 95 | + <Select |
| 96 | + value={values.vendor} |
| 97 | + onValueChange={(next) => setField('vendor', next ?? '')} |
| 98 | + > |
| 99 | + <SelectTrigger id="bg-attach-vendor"> |
| 100 | + <SelectValue placeholder="Select a vendor" /> |
| 101 | + </SelectTrigger> |
| 102 | + <SelectContent> |
| 103 | + {VENDOR_OPTIONS.map((option) => ( |
| 104 | + <SelectItem key={option.value} value={option.value}> |
| 105 | + {option.label} |
| 106 | + </SelectItem> |
| 107 | + ))} |
| 108 | + </SelectContent> |
| 109 | + </Select> |
| 110 | + </div> |
| 111 | + <div> |
| 112 | + <LabelRow htmlFor="bg-attach-date" hint="As shown on the report"> |
| 113 | + Report date |
| 114 | + </LabelRow> |
| 115 | + <div className="font-mono"> |
| 116 | + <Input |
| 117 | + id="bg-attach-date" |
| 118 | + type="date" |
| 119 | + value={values.reportDate} |
| 120 | + onChange={(event) => setField('reportDate', event.target.value)} |
| 121 | + /> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + <input |
| 126 | + ref={inputRef} |
| 127 | + type="file" |
| 128 | + accept="application/pdf" |
| 129 | + className="sr-only" |
| 130 | + onChange={handleFileChange} |
| 131 | + aria-label="Background check PDF" |
| 132 | + /> |
| 133 | + <div |
| 134 | + onDragOver={(event) => { |
| 135 | + event.preventDefault(); |
| 136 | + setDragOver(true); |
| 137 | + }} |
| 138 | + onDragLeave={() => setDragOver(false)} |
| 139 | + onDrop={handleDrop} |
| 140 | + className={`mb-4 rounded-[var(--radius)] border-[1.5px] border-dashed px-4 py-7 text-center transition-colors ${ |
| 141 | + dragOver |
| 142 | + ? 'border-primary bg-[oklch(0.985_0.012_167)]' |
| 143 | + : 'border-border bg-muted' |
| 144 | + }`} |
| 145 | + > |
| 146 | + <div className="mb-1.5 flex justify-center text-muted-foreground"> |
| 147 | + <Upload size={20} /> |
| 148 | + </div> |
| 149 | + <Text size="sm"> |
| 150 | + {values.file ? ( |
| 151 | + <>Selected: {values.file.name}</> |
| 152 | + ) : ( |
| 153 | + <> |
| 154 | + Drop the PDF here, or{' '} |
| 155 | + <button |
| 156 | + type="button" |
| 157 | + onClick={handleBrowse} |
| 158 | + className="text-primary underline underline-offset-2 hover:opacity-80" |
| 159 | + > |
| 160 | + browse files |
| 161 | + </button> |
| 162 | + </> |
| 163 | + )} |
| 164 | + </Text> |
| 165 | + <Text size="xs" variant="muted"> |
| 166 | + PDF · up to 25 MB · stored encrypted in your evidence vault |
| 167 | + </Text> |
| 168 | + {fileError && ( |
| 169 | + <p className="mt-2 text-xs text-destructive">{fileError}</p> |
| 170 | + )} |
| 171 | + </div> |
| 172 | + <FormFooterRow |
| 173 | + info={ |
| 174 | + <FormFooterInfo> |
| 175 | + We extract status and key fields automatically. You'll get to confirm before |
| 176 | + saving. |
| 177 | + </FormFooterInfo> |
| 178 | + } |
| 179 | + > |
| 180 | + {onBack && ( |
| 181 | + <Button type="button" variant="outline" size="lg" onClick={onBack} disabled={submitting}> |
| 182 | + Back |
| 183 | + </Button> |
| 184 | + )} |
| 185 | + <Button |
| 186 | + type="button" |
| 187 | + size="lg" |
| 188 | + loading={submitting} |
| 189 | + disabled={!canSubmit || submitting} |
| 190 | + onClick={onSubmit} |
| 191 | + > |
| 192 | + Attach report |
| 193 | + </Button> |
| 194 | + </FormFooterRow> |
| 195 | + </form> |
| 196 | + ); |
| 197 | +} |
0 commit comments