diff --git a/packages/webapp/src/components/Animals/DetailCards/Origin.tsx b/packages/webapp/src/components/Animals/DetailCards/Origin.tsx index 6c54a5237c..0e6714000d 100644 --- a/packages/webapp/src/components/Animals/DetailCards/Origin.tsx +++ b/packages/webapp/src/components/Animals/DetailCards/Origin.tsx @@ -41,15 +41,8 @@ const Origin = ({ t, currency, originOptions, namePrefix = '' }: OriginProps) => const watchedOrigin = watch(`${namePrefix}${DetailsFields.ORIGIN}`); - const getOriginEnum = (watchedOrigin: number): AnimalOrigins => { - const originOption = originOptions.find((option) => option.value === watchedOrigin); - return AnimalOrigins[originOption?.key as keyof typeof AnimalOrigins]; - }; - - const origin = !watchedOrigin ? undefined : getOriginEnum(watchedOrigin); - const fields = useMemo(() => { - return origin === AnimalOrigins.BROUGHT_IN ? ( + return watchedOrigin?.key === AnimalOrigins.BROUGHT_IN ? ( <> {/* @ts-ignore */} /> ); - }, [origin, Object.entries(errors)]); + }, [watchedOrigin, Object.entries(errors)]); return (
@@ -135,7 +128,7 @@ const Origin = ({ t, currency, originOptions, namePrefix = '' }: OriginProps) => row />
- {origin && fields} + {watchedOrigin && fields} ); }; diff --git a/packages/webapp/src/components/Form/RadioGroup/index.jsx b/packages/webapp/src/components/Form/RadioGroup/index.jsx index 9807d95d52..ab4d6ac0f0 100644 --- a/packages/webapp/src/components/Form/RadioGroup/index.jsx +++ b/packages/webapp/src/components/Form/RadioGroup/index.jsx @@ -92,24 +92,28 @@ export default function RadioGroup({ )} {!!radios && - radios.map((radioOptions) => ( - { - field?.onChange?.(radioOptions.value); - onChange?.({ target: { value: radioOptions.value } }); - }} - onBlur={(e) => { - field?.onBlur?.(radioOptions.value); - onBlur?.({ target: { value: radioOptions.value } }); - }} - value={field.value} - {...props} - {...radioOptions} - /> - ))} + radios.map((radioOptions) => { + return ( + { + field?.onChange?.(radioOptions.value); + onChange?.({ target: { value: radioOptions.value } }); + }} + onBlur={(e) => { + field?.onBlur?.(radioOptions.value); + onBlur?.({ target: { value: radioOptions.value } }); + }} + value={field.value} + {...props} + {...radioOptions} + /> + ); + })} ); } @@ -130,7 +134,12 @@ RadioGroup.propTypes = { label: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), pill: PropTypes.string, defaultChecked: PropTypes.bool, - value: PropTypes.oneOfType([PropTypes.string, PropTypes.bool, PropTypes.number]), + value: PropTypes.oneOfType([ + PropTypes.string, + PropTypes.bool, + PropTypes.number, + PropTypes.object, + ]), }), ), }; diff --git a/packages/webapp/src/containers/Animals/AddAnimals/index.tsx b/packages/webapp/src/containers/Animals/AddAnimals/index.tsx index 2286673ea0..9f55bfd74a 100644 --- a/packages/webapp/src/containers/Animals/AddAnimals/index.tsx +++ b/packages/webapp/src/containers/Animals/AddAnimals/index.tsx @@ -51,24 +51,21 @@ function AddAnimals({ isCompactSideMenu, history }: AddAnimalsProps) { const [addAnimals] = useAddAnimalsMutation(); const [addAnimalBatches] = useAddAnimalBatchesMutation(); - const { data: orgins = [] } = useGetAnimalOriginsQuery(); - const onSave = async ( data: any, onGoForward: () => void, setFormResultData: (data: any) => void, ) => { const details = data[STEPS.DETAILS] as AnimalDetailsFormFields[]; - const broughtInId = orgins.find((origin) => origin.key === 'BROUGHT_IN')?.id; const formattedAnimals: Partial[] = []; const formattedBatches: Partial[] = []; details.forEach((animalOrBatch) => { if (animalOrBatch.animal_or_batch === AnimalOrBatchKeys.ANIMAL) { - formattedAnimals.push(formatAnimalDetailsToDBStructure(animalOrBatch, broughtInId)); + formattedAnimals.push(formatAnimalDetailsToDBStructure(animalOrBatch)); } else { - formattedBatches.push(formatBatchDetailsToDBStructure(animalOrBatch, broughtInId)); + formattedBatches.push(formatBatchDetailsToDBStructure(animalOrBatch)); } }); diff --git a/packages/webapp/src/containers/Animals/AddAnimals/types.ts b/packages/webapp/src/containers/Animals/AddAnimals/types.ts index d074444223..570d060b62 100644 --- a/packages/webapp/src/containers/Animals/AddAnimals/types.ts +++ b/packages/webapp/src/containers/Animals/AddAnimals/types.ts @@ -78,7 +78,12 @@ export enum DetailsFields { PRICE = 'price', } -export type ReactSelectOption = { +export interface OriginValue { + id: number; + key: string; +} + +export type ReactSelectOption = { label: string; value: T; key?: string; @@ -92,7 +97,7 @@ export type Option = { [DetailsFields.TAG_TYPE]: ReactSelectOption; [DetailsFields.ORGANIC_STATUS]: ReactSelectOption; [DetailsFields.SEX]: ReactSelectOption; - [DetailsFields.ORIGIN]: ReactSelectOption; + [DetailsFields.ORIGIN]: ReactSelectOption; }; export type AnimalDetailsFormFields = { @@ -116,7 +121,7 @@ export type AnimalDetailsFormFields = { [DetailsFields.ORGANIC_STATUS]?: Option[DetailsFields.ORGANIC_STATUS]; [DetailsFields.OTHER_DETAILS]?: string; [DetailsFields.ANIMAL_IMAGE]?: any; - [DetailsFields.ORIGIN]?: number; + [DetailsFields.ORIGIN]?: OriginValue; [DetailsFields.DAM]?: string; [DetailsFields.SIRE]?: string; [DetailsFields.BROUGHT_IN_DATE]?: string; diff --git a/packages/webapp/src/containers/Animals/AddAnimals/useAnimalOptions.ts b/packages/webapp/src/containers/Animals/AddAnimals/useAnimalOptions.ts index 788ebb48e6..4f35be4509 100644 --- a/packages/webapp/src/containers/Animals/AddAnimals/useAnimalOptions.ts +++ b/packages/webapp/src/containers/Animals/AddAnimals/useAnimalOptions.ts @@ -26,7 +26,7 @@ import { } from '../../../store/api/apiSlice'; import { useTranslation } from 'react-i18next'; import { generateUniqueAnimalId } from '../../../util/animal'; -import { ANIMAL_ID_PREFIX } from '../types'; +import { ANIMAL_ID_PREFIX, AnimalOrigins } from '../types'; type OptionType = | 'default_types' @@ -149,9 +149,8 @@ export const useAnimalOptions = (...optionTypes: OptionType[]) => { if (optionTypes.includes('origin')) { options.originOptions = orgins.map(({ id, key }) => ({ - value: id, + value: { id, key: AnimalOrigins[key] }, label: t(`animal:ORIGIN.${key}`), - key: key, })); } diff --git a/packages/webapp/src/containers/Animals/AddAnimals/utils.ts b/packages/webapp/src/containers/Animals/AddAnimals/utils.ts index 5705521da4..a666c224f9 100644 --- a/packages/webapp/src/containers/Animals/AddAnimals/utils.ts +++ b/packages/webapp/src/containers/Animals/AddAnimals/utils.ts @@ -30,6 +30,7 @@ import { BatchSummary, } from '../../../components/Animals/AddAnimalsSummaryCard/types'; import { chooseAnimalBreedLabel, chooseAnimalTypeLabel } from '../Inventory/useAnimalInventory'; +import { AnimalOrigins } from '../types'; const formatFormTypeOrBreed = ( typeOrBreed: 'type' | 'breed', @@ -89,19 +90,16 @@ const convertFormDate = (date?: string): string | undefined => { return toLocalISOString(date); }; -const formatOrigin = ( - data: AnimalDetailsFormFields, - broughtInId?: number, -): Partial => { - if (!broughtInId && !data[DetailsFields.ORIGIN]) { +const formatOrigin = (data: AnimalDetailsFormFields): Partial => { + if (!data[DetailsFields.ORIGIN]) { return { birth_date: convertFormDate(data[DetailsFields.DATE_OF_BIRTH]) }; } - const isBroughtIn = broughtInId === data[DetailsFields.ORIGIN]; + const isBroughtIn = data[DetailsFields.ORIGIN]?.key === AnimalOrigins.BROUGHT_IN; return { birth_date: convertFormDate(data[DetailsFields.DATE_OF_BIRTH]), - origin_id: data[DetailsFields.ORIGIN], + origin_id: data[DetailsFields.ORIGIN]?.id, ...(isBroughtIn ? { brought_in_date: convertFormDate(data[DetailsFields.BROUGHT_IN_DATE]), @@ -118,7 +116,6 @@ const formatOrigin = ( const formatCommonDetails = ( isAnimal: boolean, data: AnimalDetailsFormFields, - broughtInId?: number, ): Partial => { return { // General @@ -133,7 +130,7 @@ const formatCommonDetails = ( photo_url: data[DetailsFields.ANIMAL_IMAGE], // Origin - ...formatOrigin(data, broughtInId), + ...formatOrigin(data), // Unique (animal) | General (batch) name: data[DetailsFields.NAME], @@ -142,10 +139,9 @@ const formatCommonDetails = ( export const formatAnimalDetailsToDBStructure = ( data: AnimalDetailsFormFields, - broughtInId?: number, ): Partial => { return { - ...formatCommonDetails(true, data, broughtInId), + ...formatCommonDetails(true, data), // Other weaning_date: convertFormDate(data[DetailsFields.WEANING_DATE]), @@ -160,9 +156,8 @@ export const formatAnimalDetailsToDBStructure = ( export const formatBatchDetailsToDBStructure = ( data: AnimalDetailsFormFields, - broughtInId?: number, ): Partial => { - return formatCommonDetails(false, data, broughtInId); + return formatCommonDetails(false, data); }; export const getSexMap = ( diff --git a/packages/webapp/src/store/api/types/index.ts b/packages/webapp/src/store/api/types/index.ts index 0a85cf626e..8103791b46 100644 --- a/packages/webapp/src/store/api/types/index.ts +++ b/packages/webapp/src/store/api/types/index.ts @@ -130,7 +130,7 @@ export interface AnimalIdentifierColor { export interface AnimalOrigin { id: number; - key: string; + key: 'BROUGHT_IN' | 'BORN_AT_FARM'; } export interface AnimalUse { diff --git a/packages/webapp/src/stories/Animals/Details/mockData.ts b/packages/webapp/src/stories/Animals/Details/mockData.ts index a47bfd2dfb..445c842a68 100644 --- a/packages/webapp/src/stories/Animals/Details/mockData.ts +++ b/packages/webapp/src/stories/Animals/Details/mockData.ts @@ -62,8 +62,8 @@ export const organicStatusOptions: ReactSelectOption[] = [ ]; export const originOptions = [ - { value: 1, label: 'Brought in', key: 'BROUGHT_IN' }, - { value: 2, label: 'Born at the farm', key: 'BORN_AT_FARM' }, + { value: { id: 1, key: 'BROUGHT_IN' }, label: 'Brought in' }, + { value: { id: 2, key: 'BORN_AT_FARM' }, label: 'Born at the farm' }, ]; export const defaultValues: Partial = {