Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Update to allow optional crops
Browse files Browse the repository at this point in the history
  • Loading branch information
GDamaso committed Aug 14, 2024
1 parent aa36aa6 commit 65339a3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
17 changes: 9 additions & 8 deletions frontend/src/Commons/Forms/InputModules/Crops/CropsInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import CropsButtonGroup from './CropsButtonGroup';
const checkHasCrops = (Fields: FieldDetailInterface[]) => {
let hasCrop = false;
Fields.forEach((field) => {
if (field.Crops.length > 0) {
if (field.Crops && field.Crops.length > 0) {
hasCrop = true;
}
});
Expand Down Expand Up @@ -158,9 +158,9 @@ const CropsInfoComponent: FC<InputModuleProps> = ({
);
};

const addCrop = (values: SubmissionCropsInterface, fieldIdx: number, cropIdx: number): void => {
const addCrop = (values: SubmissionCropsInterface, fieldIdx: number): void => {
const newCrop: CropsDetailsInterface = {
id: cropIdx,
id: farmDetails.Fields[fieldIdx].Crops?.length ?? 0,
cropId: values.cropId,
yield: values.yield,
plantAgeYears: values.plantAgeYears,
Expand All @@ -175,7 +175,8 @@ const CropsInfoComponent: FC<InputModuleProps> = ({
};

setTimeout(() => {
farmDetails.Fields[fieldIdx].Crops.push(newCrop);
console.log(newCrop);
farmDetails.Fields[fieldIdx].Crops?.push(newCrop);
setFarmDetails(farmDetails);
showFormHandler(fieldIdx);
setInitialFieldValues(initialValues);
Expand All @@ -190,9 +191,9 @@ const CropsInfoComponent: FC<InputModuleProps> = ({
(f) => f.FieldName === field.FieldName && f.Area === field.Area,
);

const cropIdx = updatedFarmDetails.Fields[fieldIdx].Crops.findIndex((c) => c === crop);
const cropIdx = updatedFarmDetails.Fields[fieldIdx].Crops?.findIndex((c) => c === crop);

updatedFarmDetails.Fields[fieldIdx].Crops.splice(cropIdx, 1);
updatedFarmDetails.Fields[fieldIdx].Crops?.splice(cropIdx ?? 0, 1);
setFarmDetails(updatedFarmDetails);
};

Expand All @@ -217,7 +218,7 @@ const CropsInfoComponent: FC<InputModuleProps> = ({
initialValues={cropInitialValues}
validationSchema={validationSchema}
onSubmit={(values, { resetForm }) => {
addCrop(values, index, field.Crops.length);
addCrop(values, index);
resetForm();
}}
validate={(values) => {
Expand Down Expand Up @@ -346,7 +347,7 @@ const CropsInfoComponent: FC<InputModuleProps> = ({
}
</Formik>

{!hasFieldBeenSelected[index] && field.Crops.length < 2 && (
{!hasFieldBeenSelected[index] && (field.Crops?.length ?? 0) < 2 && (
<StyledNewFieldButtonContainer formCrops>
<StyledNewFieldButtonController>
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const CropsList: FC<CropsListComponentProps> = ({ farmDetails, field, removeCrop
<p key={field.FieldName}>{field.FieldName}</p>
</StyledListItem>
<StyledCropsGroup>
{field.Crops.map((crop: CropsDetailsInterface, idx: number) => (
{field.Crops?.map((crop: CropsDetailsInterface, idx: number) => (
<StyledListItem
desktopWidth="160px"
mobileWidth="121px"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ const FieldsAndSoilComponent: FC<InputModuleProps> = ({
const [isSubmitted, setSubmitted] = useState<boolean>(farmDetails.Fields.length > 0);
// Would trigger when new field button is clicked.
const [isFieldAdded, setFieldAdd] = useState<boolean>(false);
const [initialValues, setInitialValues] = useState<FieldDetailInterface>(
initialFarmDetails.Fields[0],
);

const radioOptions = [
{ id: 'true', label: 'Yes', value: true },
{ id: 'false', label: 'No', value: false },
];

const initialValues: FieldDetailInterface = initialFarmDetails.Fields[0];
const textAreaMaxLength: number = 200;
const validationSchema = Yup.object().shape({
FieldName: Yup.string().max(24).required('Required'),
Expand Down Expand Up @@ -118,6 +120,8 @@ const FieldsAndSoilComponent: FC<InputModuleProps> = ({
(f) => f.FieldName === field.FieldName && f.Area === field.Area,
);

setInitialValues(field);
removeField(field);
setFieldIndex(idx);
setFieldAdd(true);
handleFormState(FIELDS_AND_SOIL, undefined, ACTIVE);
Expand Down Expand Up @@ -172,8 +176,8 @@ const FieldsAndSoilComponent: FC<InputModuleProps> = ({
leafTissueP: values.LeafTest.leafTissueP,
leafTissueK: values.LeafTest.leafTissueK,
},
Crops: farmDetails.Fields[fieldIndex]?.Crops ?? [],
};
console.log(newField);

if (values.HasSoilTest === false) newField.SoilTest = noSoilTestVal;
if (values.HasLeafTest === false) newField.LeafTest = noLeafTestVal;
Expand All @@ -195,6 +199,7 @@ const FieldsAndSoilComponent: FC<InputModuleProps> = ({
// farmInfo.Fields[fieldIndex].Nutrients.nutrientFertilizers.splice(0, 1);
// }

setInitialValues(initialValues);
setFarmDetails(farmInfo);
setFieldIndex((prevIndex) => prevIndex + 1);
setSubmitted(true);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/Utils/convertToNMP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const convertToNMP = (
prevDetails: NmpInterface,
): NmpInterface => {
const newFields: NmpFieldInterface[] = newDetails.Fields.map((field: FieldDetailInterface) => {
const newCrops: NmpCropInterface[] = field.Crops.map((crop: CropsDetailsInterface) => ({
const newCrops: NmpCropInterface[] = field.Crops?.map((crop: CropsDetailsInterface) => ({
...templateCropNMP,
id: crop.id,
cropId: crop.cropId,
Expand Down

0 comments on commit 65339a3

Please sign in to comment.