Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/frontend/src/components/NERAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ interface NERAutocompleteProps {
errorMessage?: FieldError;
required?: boolean;
disabled?: boolean;
noOptionsText?: React.ReactNode;
onInputChange?: (event: React.SyntheticEvent, value: string) => void;
}

const NERAutocomplete: React.FC<NERAutocompleteProps> = ({
Expand All @@ -42,7 +44,9 @@ const NERAutocomplete: React.FC<NERAutocompleteProps> = ({
filterSelectedOptions,
errorMessage,
required = true,
disabled = false
disabled = false,
noOptionsText,
onInputChange
}) => {
const theme = useTheme();

Expand Down Expand Up @@ -78,6 +82,8 @@ const NERAutocomplete: React.FC<NERAutocompleteProps> = ({
disablePortal
id={id}
onChange={onChange}
onInputChange={onInputChange}
noOptionsText={noOptionsText}
options={options}
sx={autocompleteStyle}
disabled={disabled}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ const ReimbursementRequestFormView: React.FC<ReimbursementRequestFormViewProps>
const [datePickerOpen, setDatePickerOpen] = useState(false);
const [showAddRefundSourceModal, setShowAddRefundSourceModal] = useState(false);
const [newVendorName, setNewVendorName] = useState<string>('');
const [showCreateVendorField, setShowCreateVendorField] = useState<boolean>(false);
const { mutateAsync: createVendor } = useCreateVendor();
const user = useCurrentUser();

Expand Down Expand Up @@ -342,69 +341,47 @@ const ReimbursementRequestFormView: React.FC<ReimbursementRequestFormViewProps>
const mappedVendors = allVendors
.sort((a, b) => a.name.localeCompare(b.name))
.map(vendorsToAutocomplete);

const handleCreateVendor = async () => {
const name = newVendorName.trim();
if (!name) return;
try {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should also reset the vendor name to an empty string in this try block after the call

const created = await createVendor({ name });
setValue('vendorId', created.vendorId);
onChange(created.vendorId);
toast.success(`Vendor '${created.name}' created.`);
} catch (err: any) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use any, use unknown

toast.error(err.message || 'Failed to create vendor');
}
};

return (
<Box>
<Box sx={{ display: 'flex', gap: 1, alignItems: 'center' }}>
<Box sx={{ flex: 1 }}>
<NERAutocomplete
id="vendor-autocomplete"
options={mappedVendors}
value={mappedVendors.find((v) => v.id === value) || null}
onChange={(_, newValue) => {
if (newValue) {
onChange(newValue.id);
}
}}
size="small"
placeholder="Select Existing Vendor"
errorMessage={errors.vendorId}
/>
</Box>
<Typography fontWeight="bold" sx={{ whiteSpace: 'nowrap', lineHeight: 1 }}>
OR
</Typography>
<Button
variant="outlined"
size="small"
onClick={() => setShowCreateVendorField((prev) => !prev)}
sx={{ whiteSpace: 'nowrap' }}
>
{showCreateVendorField ? 'Cancel' : 'Create Vendor'}
</Button>
</Box>
{showCreateVendorField && (
<Box sx={{ mt: 2, display: 'flex', gap: 1 }}>
<TextField
value={newVendorName}
onChange={(e) => setNewVendorName(e.target.value)}
placeholder="New Vendor Name"
variant="outlined"
size="small"
fullWidth
/>
<NERAutocomplete
id="vendor-autocomplete"
options={mappedVendors}
value={mappedVendors.find((v) => v.id === value) || null}
onChange={(_, newValue) => {
onChange(newValue ? newValue.id : '');
}}
onInputChange={(_, inputValue) => setNewVendorName(inputValue)}
noOptionsText={
newVendorName.trim() ? (
<Button
variant="contained"
variant="text"
size="small"
disabled={!newVendorName.trim()}
onClick={async () => {
try {
const created = await createVendor({ name: newVendorName.trim() });
setValue('vendorId', created.vendorId);
onChange(created.vendorId);
setNewVendorName('');
setShowCreateVendorField(false);
toast.success(`Vendor '${created.name}' created.`);
} catch (err: any) {
toast.error(err.message || 'Failed to create vendor');
}
}}
sx={{ whiteSpace: 'nowrap' }}
onMouseDown={(e) => e.preventDefault()}
onClick={handleCreateVendor}
>
Save
Add "{newVendorName.trim()}" as a new vendor
</Button>
</Box>
)}
</Box>
) : (
'No options'
)
}
size="small"
placeholder="Select Vendor"
errorMessage={errors.vendorId}
/>
);
}}
/>
Expand Down
Loading