add isSubmitting state to BeerForm

This commit is contained in:
Aaron William Po
2023-02-20 16:12:42 -05:00
parent c818dc6525
commit cee7942f1c
5 changed files with 37 additions and 9 deletions

View File

@@ -49,7 +49,10 @@ const BeerForm: FunctionComponent<BeerFormProps> = ({
const [error, setError] = useState(''); const [error, setError] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const onSubmit: SubmitHandler<BeerPostT> = async (data) => { const onSubmit: SubmitHandler<BeerPostT> = async (data) => {
setIsSubmitting(true);
switch (formType) { switch (formType) {
case 'create': { case 'create': {
try { try {
@@ -86,6 +89,7 @@ const BeerForm: FunctionComponent<BeerFormProps> = ({
error={!!errors.name} error={!!errors.name}
type="text" type="text"
id="name" id="name"
disabled={isSubmitting}
/> />
</FormSegment> </FormSegment>
{formType === 'create' && breweries.length && ( {formType === 'create' && breweries.length && (
@@ -96,6 +100,7 @@ const BeerForm: FunctionComponent<BeerFormProps> = ({
</FormInfo> </FormInfo>
<FormSegment> <FormSegment>
<FormSelect <FormSelect
disabled={isSubmitting}
formRegister={register('breweryId')} formRegister={register('breweryId')}
error={!!errors.breweryId} error={!!errors.breweryId}
id="breweryId" id="breweryId"
@@ -117,6 +122,7 @@ const BeerForm: FunctionComponent<BeerFormProps> = ({
<FormError>{errors.abv?.message}</FormError> <FormError>{errors.abv?.message}</FormError>
</FormInfo> </FormInfo>
<FormTextInput <FormTextInput
disabled={isSubmitting}
placeholder="12" placeholder="12"
formValidationSchema={register('abv', { valueAsNumber: true })} formValidationSchema={register('abv', { valueAsNumber: true })}
error={!!errors.abv} error={!!errors.abv}
@@ -130,6 +136,7 @@ const BeerForm: FunctionComponent<BeerFormProps> = ({
<FormError>{errors.ibu?.message}</FormError> <FormError>{errors.ibu?.message}</FormError>
</FormInfo> </FormInfo>
<FormTextInput <FormTextInput
disabled={isSubmitting}
placeholder="52" placeholder="52"
formValidationSchema={register('ibu', { valueAsNumber: true })} formValidationSchema={register('ibu', { valueAsNumber: true })}
error={!!errors.ibu} error={!!errors.ibu}
@@ -145,6 +152,7 @@ const BeerForm: FunctionComponent<BeerFormProps> = ({
</FormInfo> </FormInfo>
<FormSegment> <FormSegment>
<FormTextArea <FormTextArea
disabled={isSubmitting}
placeholder="Ratione cumque quas quia aut impedit ea culpa facere. Ut in sit et quas reiciendis itaque." placeholder="Ratione cumque quas quia aut impedit ea culpa facere. Ut in sit et quas reiciendis itaque."
error={!!errors.description} error={!!errors.description}
formValidationSchema={register('description')} formValidationSchema={register('description')}
@@ -159,6 +167,7 @@ const BeerForm: FunctionComponent<BeerFormProps> = ({
</FormInfo> </FormInfo>
<FormSegment> <FormSegment>
<FormSelect <FormSelect
disabled={isSubmitting}
formRegister={register('typeId')} formRegister={register('typeId')}
error={!!errors.typeId} error={!!errors.typeId}
id="typeId" id="typeId"
@@ -171,11 +180,19 @@ const BeerForm: FunctionComponent<BeerFormProps> = ({
/> />
</FormSegment> </FormSegment>
<Button type="submit">{`${ {!isSubmitting && (
formType === 'edit' <Button type="submit" isSubmitting={isSubmitting}>{`${
? `Edit ${defaultValues?.name || 'beer post'}` formType === 'edit'
: 'Create beer post' ? `Edit ${defaultValues?.name || 'beer post'}`
} `}</Button> : 'Create beer post'
}`}</Button>
)}
{isSubmitting && (
<Button type="submit" isSubmitting={isSubmitting}>
Submitting
</Button>
)}
</form> </form>
); );
}; };

View File

@@ -3,11 +3,19 @@ import { FunctionComponent } from 'react';
interface FormButtonProps { interface FormButtonProps {
children: string; children: string;
type: 'button' | 'submit' | 'reset'; type: 'button' | 'submit' | 'reset';
isSubmitting?: boolean;
} }
const Button: FunctionComponent<FormButtonProps> = ({ children, type }) => ( const Button: FunctionComponent<FormButtonProps> = ({
children,
type,
isSubmitting = false,
}) => (
// eslint-disable-next-line react/button-has-type // eslint-disable-next-line react/button-has-type
<button type={type} className="btn-primary btn mt-4 w-full rounded-xl"> <button
type={type}
className={`btn btn-primary mt-4 w-full rounded-xl ${isSubmitting ? 'loading' : ''}`}
>
{children} {children}
</button> </button>
); );

View File

@@ -8,6 +8,7 @@ interface FormSelectProps {
error: boolean; error: boolean;
placeholder: string; placeholder: string;
message: string; message: string;
disabled?: boolean;
} }
/** /**
@@ -40,6 +41,7 @@ const FormSelect: FunctionComponent<FormSelectProps> = ({
formRegister, formRegister,
placeholder, placeholder,
message, message,
disabled = false,
}) => ( }) => (
<select <select
id={id} id={id}
@@ -47,6 +49,7 @@ const FormSelect: FunctionComponent<FormSelectProps> = ({
error ? 'select-error' : '' error ? 'select-error' : ''
}`} }`}
placeholder={placeholder} placeholder={placeholder}
disabled={disabled}
{...formRegister} {...formRegister}
> >
<option value="">{message}</option> <option value="">{message}</option>

View File

@@ -27,7 +27,7 @@ const sendCreateBeerPostRequest = async (
const parsedPayload = beerPostQueryResultSchema.safeParse(payload); const parsedPayload = beerPostQueryResultSchema.safeParse(payload);
if (!parsedPayload.success) { if (!parsedPayload.success) {
throw new Error('Invalid API response'); throw new Error('Invalid API response payload');
} }
return parsedPayload.data; return parsedPayload.data;

View File

@@ -26,7 +26,7 @@ export const beerPostQueryResultSchema = z.object({
id: z.string(), id: z.string(),
username: z.string(), username: z.string(),
}), }),
createdAt: z.date(), createdAt: z.coerce.date(),
}); });
export const beerPostQueryResultArraySchema = z.array(beerPostQueryResultSchema); export const beerPostQueryResultArraySchema = z.array(beerPostQueryResultSchema);