Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion - Radio Options #3408

Draft
wants to merge 1 commit into
base: integration
Choose a base branch
from
Draft
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
13 changes: 3 additions & 10 deletions packages/webapp/src/components/Animals/DetailCards/Origin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 */}
<Input
Expand Down Expand Up @@ -115,7 +108,7 @@ const Origin = ({ t, currency, originOptions, namePrefix = '' }: OriginProps) =>
/>
</>
);
}, [origin, Object.entries(errors)]);
}, [watchedOrigin, Object.entries(errors)]);

return (
<div className={styles.sectionWrapper}>
Expand All @@ -135,7 +128,7 @@ const Origin = ({ t, currency, originOptions, namePrefix = '' }: OriginProps) =>
row
/>
</div>
{origin && fields}
{watchedOrigin && fields}
</div>
);
};
Expand Down
47 changes: 28 additions & 19 deletions packages/webapp/src/components/Form/RadioGroup/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,28 @@ export default function RadioGroup({
</>
)}
{!!radios &&
radios.map((radioOptions) => (
<Radio
name={name}
key={radioOptions.value}
checked={field.value === radioOptions.value}
onChange={(e) => {
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 (
<Radio
name={name}
key={radioOptions.value?.id || radioOptions.value}
checked={
field.value?.id === radioOptions.value?.id || field.value === radioOptions.value
}
onChange={(e) => {
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}
/>
);
})}
</div>
);
}
Expand All @@ -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,
]),
}),
),
};
7 changes: 2 additions & 5 deletions packages/webapp/src/containers/Animals/AddAnimals/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Animal>[] = [];
const formattedBatches: Partial<AnimalBatch>[] = [];

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));
}
});

Expand Down
11 changes: 8 additions & 3 deletions packages/webapp/src/containers/Animals/AddAnimals/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ export enum DetailsFields {
PRICE = 'price',
}

export type ReactSelectOption<T extends string | number> = {
export interface OriginValue {
id: number;
key: string;
}

export type ReactSelectOption<T extends string | number | OriginValue> = {
label: string;
value: T;
key?: string;
Expand All @@ -92,7 +97,7 @@ export type Option = {
[DetailsFields.TAG_TYPE]: ReactSelectOption<number>;
[DetailsFields.ORGANIC_STATUS]: ReactSelectOption<OrganicStatus>;
[DetailsFields.SEX]: ReactSelectOption<number>;
[DetailsFields.ORIGIN]: ReactSelectOption<number>;
[DetailsFields.ORIGIN]: ReactSelectOption<OriginValue>;
};

export type AnimalDetailsFormFields = {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
}));
}

Expand Down
21 changes: 8 additions & 13 deletions packages/webapp/src/containers/Animals/AddAnimals/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -89,19 +90,16 @@ const convertFormDate = (date?: string): string | undefined => {
return toLocalISOString(date);
};

const formatOrigin = (
data: AnimalDetailsFormFields,
broughtInId?: number,
): Partial<Animal | AnimalBatch> => {
if (!broughtInId && !data[DetailsFields.ORIGIN]) {
const formatOrigin = (data: AnimalDetailsFormFields): Partial<Animal | AnimalBatch> => {
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]),
Expand All @@ -118,7 +116,6 @@ const formatOrigin = (
const formatCommonDetails = (
isAnimal: boolean,
data: AnimalDetailsFormFields,
broughtInId?: number,
): Partial<Animal | AnimalBatch> => {
return {
// General
Expand All @@ -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],
Expand All @@ -142,10 +139,9 @@ const formatCommonDetails = (

export const formatAnimalDetailsToDBStructure = (
data: AnimalDetailsFormFields,
broughtInId?: number,
): Partial<Animal> => {
return {
...formatCommonDetails(true, data, broughtInId),
...formatCommonDetails(true, data),

// Other
weaning_date: convertFormDate(data[DetailsFields.WEANING_DATE]),
Expand All @@ -160,9 +156,8 @@ export const formatAnimalDetailsToDBStructure = (

export const formatBatchDetailsToDBStructure = (
data: AnimalDetailsFormFields,
broughtInId?: number,
): Partial<AnimalBatch> => {
return formatCommonDetails(false, data, broughtInId);
return formatCommonDetails(false, data);
};

export const getSexMap = (
Expand Down
2 changes: 1 addition & 1 deletion packages/webapp/src/store/api/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export interface AnimalIdentifierColor {

export interface AnimalOrigin {
id: number;
key: string;
key: 'BROUGHT_IN' | 'BORN_AT_FARM';
}

export interface AnimalUse {
Expand Down
4 changes: 2 additions & 2 deletions packages/webapp/src/stories/Animals/Details/mockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export const organicStatusOptions: ReactSelectOption<OrganicStatus>[] = [
];

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<AnimalDetailsFormFields> = {
Expand Down
Loading