Skip to content

Commit

Permalink
LF-4410: No reset on limit, show error
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbrid committed Dec 4, 2024
1 parent d4d8765 commit 83d148a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
3 changes: 3 additions & 0 deletions packages/webapp/public/locales/de/animal.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@
"MILK": "Milch",
"OTHER": "Andere",
"RECREATIONAL_OR_CULTURAL_USE": "Kulturelle oder Freizeitnutzung"
},
"COUNT": {
"MAX": "MISSING"
}
}
3 changes: 3 additions & 0 deletions packages/webapp/public/locales/en/animal.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@
"RECREATIONAL_OR_CULTURAL_USE": "Recreational or Cultural Use",
"COMPANIONSHIP": "Companionship",
"OTHER": "Other"
},
"COUNT": {
"MAX": "You can only add up to {{max}} animals at a time"
}
}
3 changes: 3 additions & 0 deletions packages/webapp/public/locales/es/animal.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@
"RECREATIONAL_OR_CULTURAL_USE": "MISSING",
"COMPANIONSHIP": "MISSING",
"OTHER": "MISSING"
},
"COUNT": {
"MAX": "MISSING"
}
}
3 changes: 3 additions & 0 deletions packages/webapp/public/locales/fr/animal.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@
"RECREATIONAL_OR_CULTURAL_USE": "MISSING",
"COMPANIONSHIP": "MISSING",
"OTHER": "MISSING"
},
"COUNT": {
"MAX": "MISSING"
}
}
3 changes: 3 additions & 0 deletions packages/webapp/public/locales/pt/animal.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@
"RECREATIONAL_OR_CULTURAL_USE": "MISSING",
"COMPANIONSHIP": "MISSING",
"OTHER": "MISSING"
},
"COUNT": {
"MAX": "MISSING"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export default function AddAnimalsFormCard({
getValues,
setValue,
resetField,
setError,
clearErrors,
formState: { errors },
} = useFormContext();

Expand All @@ -84,6 +86,22 @@ export default function AddAnimalsFormCard({
}
}, []);

const countMaxValidation = () => ({
value: ANIMAL_COUNT_LIMIT,
message: t('animal:COUNT.MAX', { max: ANIMAL_COUNT_LIMIT }),
});

useEffect(() => {
if (shouldCreateIndividualProfiles && watchAnimalCount > ANIMAL_COUNT_LIMIT) {
setError(`${namePrefix}${BasicsFields.COUNT}`, {
type: 'manual',
message: t('animal:COUNT.MAX', { max: ANIMAL_COUNT_LIMIT }),
});
} else {
clearErrors(`${namePrefix}${BasicsFields.COUNT}`);
}
}, [watchAnimalCount, shouldCreateIndividualProfiles, setError, clearErrors]);

const filteredBreeds = breedOptions.filter(({ type }) => type === watchAnimalType?.value);

return (
Expand Down Expand Up @@ -114,17 +132,16 @@ export default function AddAnimalsFormCard({
<NumberInput
name={`${namePrefix}${BasicsFields.COUNT}`}
control={control}
max={shouldCreateIndividualProfiles ? ANIMAL_COUNT_LIMIT : undefined}
label={t('common:COUNT')}
className={styles.countInput}
allowDecimal={false}
value={watchAnimalCount}
showStepper
rules={{
required: {
value: true,
message: t('common:REQUIRED'),
},
max: shouldCreateIndividualProfiles ? countMaxValidation() : undefined,
min: hookFormMinValidation(1),
}}
onChange={() => trigger(`${namePrefix}${BasicsFields.COUNT}`)}
Expand Down Expand Up @@ -154,11 +171,6 @@ export default function AddAnimalsFormCard({
hookFormRegister={register(`${namePrefix}${BasicsFields.CREATE_INDIVIDUAL_PROFILES}`)}
onChange={(e) => {
onIndividualProfilesCheck?.((e.target as HTMLInputElement).checked);
if ((e.target as HTMLInputElement).checked && watchAnimalCount > ANIMAL_COUNT_LIMIT) {
setValue(`${namePrefix}${BasicsFields.COUNT}`, ANIMAL_COUNT_LIMIT, {
shouldValidate: true,
});
}
}}
/>
{shouldCreateIndividualProfiles ? (
Expand Down
4 changes: 0 additions & 4 deletions packages/webapp/src/components/Form/NumberInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ export type NumberInputProps<T extends FieldValues> = UseControllerProps<T> &
showStepper?: boolean;

className?: string;

value?: number;
};

export default function NumberInput<T extends FieldValues>({
Expand All @@ -59,7 +57,6 @@ export default function NumberInput<T extends FieldValues>({
className,
onChange,
onBlur,
value,
...props
}: NumberInputProps<T>) {
const { field, fieldState, formState } = useController({ name, control, rules, defaultValue });
Expand Down Expand Up @@ -90,7 +87,6 @@ export default function NumberInput<T extends FieldValues>({
className={className}
error={fieldState.error?.message}
onResetIconClick={reset}
value={value}
leftSection={currencySymbol}
rightSection={
<>
Expand Down

0 comments on commit 83d148a

Please sign in to comment.