Files

108 lines
4.3 KiB
TypeScript
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
import { useStore } from '@tanstack/react-form';
import { useAppForm, Form } from '@/components/form';
import { createAdminLocationInput, useCreateAdminLocation } from '@/api/admin/locations/queries';
import ExtensionFormFields from '@/components/admin/extensions/ExtensionFormFields';
import { initialExtensionValues, useExtensionPayload } from '@/extensions/forms';
2026-10-05 18:10:18 -04:00
import { Dialog } from '@/components/elements/dialog';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import Button from '@/components/elements/Button';
import { NewButton } from '@/components/elements/NewButton';
2026-10-08 18:49:15 -04:00
import { validateShortCode } from '@/components/admin/locations/locationForm';
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
type CreateLocationDialogProps = {
open: boolean;
onClose: () => void;
};
function CreateLocationDialog({ open, onClose }: CreateLocationDialogProps) {
const createLocation = useCreateAdminLocation();
const { withExtensionPayload } = useExtensionPayload('admin.location');
2026-10-05 18:10:18 -04:00
const form = useAppForm({
defaultValues: { short: '', long: '', extensions: initialExtensionValues() },
2026-10-05 18:10:18 -04:00
onSubmit: async ({ value }) => {
try {
await createLocation.mutateAsync(createAdminLocationInput(withExtensionPayload(value)));
2026-10-05 18:10:18 -04:00
onClose();
} catch {
// Error toast is handled by the mutation.
}
},
});
const isSubmitting = useStore(form.store, (state) => state.isSubmitting);
return (
<Dialog
open={open}
2026-10-08 16:53:20 -04:00
title='Create new location'
2026-10-05 18:10:18 -04:00
preventExternalClose={isSubmitting}
hideCloseIcon={isSubmitting}
onClose={onClose}
>
<SpinnerOverlay visible={isSubmitting} />
2026-10-08 16:53:20 -04:00
<Form form={form} className='m-0'>
2026-10-05 18:10:18 -04:00
<form.AppField
2026-10-08 16:53:20 -04:00
name='short'
2026-10-05 18:10:18 -04:00
validators={{
2026-10-08 16:53:20 -04:00
onChange: ({ value }) => validateShortCode(value),
2026-10-05 18:10:18 -04:00
}}
>
{(field) => (
<field.TextField
2026-10-08 16:53:20 -04:00
type='text'
id='short'
label='Short Code'
description='A short identifier used to distinguish this location from others.'
2026-10-05 18:10:18 -04:00
/>
)}
</form.AppField>
2026-10-08 16:53:20 -04:00
<div className='mt-6'>
2026-10-05 18:10:18 -04:00
<form.AppField
2026-10-08 16:53:20 -04:00
name='long'
2026-10-05 18:10:18 -04:00
validators={{
onChange: ({ value }) =>
value.length <= 191 ? undefined : 'The description must not exceed 191 characters.',
}}
>
{(field) => (
<field.TextField
2026-10-08 16:53:20 -04:00
type='text'
id='long'
label='Description'
description='A longer description of this location.'
2026-10-05 18:10:18 -04:00
/>
)}
</form.AppField>
</div>
<form.AppField name='extensions'>
{() => (
<ExtensionFormFields
form='admin.location'
mode='create'
error={createLocation.error}
className='mt-6'
/>
)}
</form.AppField>
2026-10-08 16:53:20 -04:00
<div className='flex flex-wrap justify-end mt-6'>
<Button type='button' isSecondary className='w-full sm:w-auto sm:mr-2' onClick={onClose}>
2026-10-05 18:10:18 -04:00
Cancel
</Button>
<form.AppForm>
2026-10-08 16:53:20 -04:00
<form.SubmitButton className='w-full mt-4 sm:w-auto sm:mt-0'>Create Location</form.SubmitButton>
2026-10-05 18:10:18 -04:00
</form.AppForm>
</div>
</Form>
</Dialog>
);
}
export default function CreateLocationButton() {
return (
<Dialog.Trigger trigger={({ onClick }) => <NewButton onClick={onClick}>New location</NewButton>}>
{({ open, onClose }) => <CreateLocationDialog open={open} onClose={onClose} />}
</Dialog.Trigger>
);
}