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

Commit

Permalink
feat: Bb 276 implement calculate util (#287)
Browse files Browse the repository at this point in the history
* Add CalculationTable Interface

* Move SubmissionValues to crops

It's only being used there, makes sense to have it only there

* Add Calculate Util

* Call Calculate from Fertilizer Next button

* Update Crops validationSchema max values

* camelCase it

* Lint

* Console log calculation

* Implement CropRemoval Calculation

* Removed debug console.logs

Crop removal was done but console was printing wrong results... A page refresh fixed this issue...

* Fixed crop removal calculation when removing from field

It should simply add the coeficient, not add the Yield * coeficient

* Cleanup

* Lint

* Update Husky to check build before push
  • Loading branch information
GDamaso authored Jul 24, 2024
1 parent e98ac39 commit 18d6c32
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 66 deletions.
1 change: 1 addition & 0 deletions backend/.husky/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cd frontend
npm run test
npm run format
npm run lint
npm run build

cd ../backend
npm run test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC } from 'react';
import FarmDetailsInterface from '@Interface/FarmDetailsInterface';
import { CropsDetailsInterface } from '@Interface/CropsDetailsInterface';
import CropsDetailsInterface from '@Interface/CropsDetailsInterface';
import FertilizerInterface from '@Interface/FertilizerInterface';
import {
StyledH3HeaderContainer,
Expand Down
26 changes: 23 additions & 3 deletions frontend/src/Commons/Forms/InputModules/Crops/CropsInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
StyledAreaContainer,
} from '@Commons/FormStyles.styles';
import initialFarmDetails from '@Constants/InitialFarmDetails';
import { CropsDetailsInterface, SubmissionCropsInterface } from '@Interface/CropsDetailsInterface';
import CropsDetailsInterface from '@Interface/CropsDetailsInterface';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
StyledNewFieldButtonContainer,
Expand All @@ -48,8 +48,6 @@ import {
import CropsButtonGroup from './CropsButtonGroup';
import { StyledDivider } from '../ListComponent.styles';

const initialValues: SubmissionCropsInterface = initialFarmDetails.Fields[0].Crops[0];

const checkHasCrops = (Fields: FieldDetailInterface[]) => {
let hasCrop = false;
Fields.forEach((field) => {
Expand All @@ -60,6 +58,28 @@ const checkHasCrops = (Fields: FieldDetailInterface[]) => {
return hasCrop;
};

/**
* @summary Interface for the main data file
* @description This interface will be for Submission, it works the same as the CropsDetailInterface
* but this is only for the submission of formik.
* @author @Kcaparas
*/

interface SubmissionCropsInterface {
id: number;
cropId: string;
yield: number;
plantAgeYears: string;
numberOfPlantsPerAcre: number;
distanceBtwnPlants: string;
distanceBtwnRows: string;
willPlantsBePruned: boolean | undefined;
whereWillPruningsGo: string;
willSawdustBeApplied: boolean | undefined;
}

const initialValues: SubmissionCropsInterface = initialFarmDetails.Fields[0].Crops[0];

const CropsInfoComponent: FC<InputModuleProps> = ({
farmDetails,
updateFarmDetails,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC } from 'react';
import FieldDetailInterface from '@Interface/FieldDetailsInterface';
import { CropsDetailsInterface } from '@Interface/CropsDetailsInterface';
import CropsDetailsInterface from '@Interface/CropsDetailsInterface';
import {
StyledListContainer,
StyledListItem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const FertilizersInfo: React.FC<InputModuleProps> = ({
fertilizersDetails,
handleFormState,
updateFertDetails,
farmDetails,
}) => {
const initialFieldValues = initialFarmDetails.Fields[0].Nutrients[0];
const [isAddButtonClicked, setAddButtonClicked] = useState<boolean>(false);
Expand Down Expand Up @@ -135,6 +136,7 @@ const FertilizersInfo: React.FC<InputModuleProps> = ({
<FertilizersButtonComponent
addNewFertilizer={addNewFertilizer}
handleFormState={handleFormState}
farmDetails={farmDetails}
/>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@ import Button from '@Commons/Button/Button';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faPlus } from '@fortawesome/free-solid-svg-icons';
import { COMPLETED } from '@Constants/ModuleStatus';
import Calculate from '@Utils/Calculate/Calculate';
import {
StyledButtonGroupContainer,
StyledButtonContainer,
StyledAddCancelButtonContainer,
StyledNewFieldButtonContainer,
StyledNewFieldButtonController,
} from '@Commons/Button/FieldButtonGroup.styles';
import FarmDetailsInterface from '@Interface/FarmDetailsInterface';

type ButtonComponentProps = {
addNewFertilizer: () => void;
submitFertDetails?: () => void;
handleFormState(cmd: string, toggle?: boolean, status?: string): void;
farmDetails: FarmDetailsInterface;
};

const FertilizersButtonComponent: FC<ButtonComponentProps> = ({
addNewFertilizer,
handleFormState,
submitFertDetails,
farmDetails,
}) => (
<StyledButtonGroupContainer>
<StyledNewFieldButtonContainer>
Expand Down Expand Up @@ -60,6 +64,7 @@ const FertilizersButtonComponent: FC<ButtonComponentProps> = ({
handleClick={() => {
if (submitFertDetails) submitFertDetails();
handleFormState(CmdOptions.FORWARDS, undefined, COMPLETED);
Calculate(farmDetails.Fields[0]);
}}
/>
</StyledButtonContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const FieldsAndSoilComponent: FC<InputModuleProps> = ({
{ id: 'true', label: 'Yes', value: true },
{ id: 'false', label: 'No', value: false },
];

const initialValues: FieldDetailInterface = initialFarmDetails.Fields[0];

const validationSchema = Yup.object().shape({
Expand All @@ -78,10 +79,10 @@ const FieldsAndSoilComponent: FC<InputModuleProps> = ({
otherwise: (schema) => schema.notRequired(),
}),
sampleDate: Yup.string().required(),
valNO3H: Yup.number().min(0).max(7).required(),
ValP: Yup.number().min(0).max(7).required(),
valK: Yup.number().min(0).max(7).required(),
valPH: Yup.number().min(0).max(7).required(),
valNO3H: Yup.number().min(0).max(1000).required(),
ValP: Yup.number().min(0).max(1000).required(),
valK: Yup.number().min(0).max(1000).required(),
valPH: Yup.number().min(0).max(1000).required(),
})
.required(),
otherwise: (schema) => schema.notRequired(),
Expand Down Expand Up @@ -109,6 +110,17 @@ const FieldsAndSoilComponent: FC<InputModuleProps> = ({
const addFieldData = (values: FieldDetailInterface): void => {
setTimeout(() => {
const farmInfo: FarmDetailsInterface = { ...farmDetails };
const noSoilTestVal = {
...values.SoilTest,
ValP: Infinity,
valK: Infinity,
valPH: 4.0,
};

const noLeafTestVal = {
leafTissueP: Infinity,
leafTissueK: Infinity,
};

const newField: FieldDetailInterface = {
Id: fieldIndex,
Expand Down Expand Up @@ -163,6 +175,9 @@ const FieldsAndSoilComponent: FC<InputModuleProps> = ({
],
};

if (values.HasSoilTest === false) newField.SoilTest = noSoilTestVal;
if (values.HasLeafTest === false) newField.LeafTest = noLeafTestVal;

farmInfo.Fields.push(newField);

// splice or pop to remove Crops after getting pushed to array
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/Interface/AgronomicBalanceInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface AgronomicBalanceInterface {
N: number;
P: number;
K: number;
}

export default AgronomicBalanceInterface;
57 changes: 57 additions & 0 deletions frontend/src/Interface/CalculationTableInterface.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
interface NutrientRange {
min?: number;
max?: number;
addition: number;
}

interface SoilTestRange {
min?: number;
max?: number;
leafTissueRanges?: NutrientRange[];
}

interface Params {
sawdust?: string;
Yield?: string;
soilTest?: string;
leafTissue?: string;
Pruned?: string;
Removed?: string;
}

export interface CalcLogic {
sawdustAddition?: number;
yieldRanges?: NutrientRange[];
soilTestRanges?: SoilTestRange[];
N?: number;
P?: number;
K?: number;
fruitRemovalFactor?: number;
pruningRemovalFactor?: number;
}

interface Calculation {
description: string;
params: Params;
logic: CalcLogic;
}

interface AgronomicBalance {
nitrogenCalculation: Calculation;
phosphorusCalculation: Calculation;
potassiumCalculation: Calculation;
}

export interface CropRemovalCoeficient {
phosphorusRemoval: Calculation;
potassiumRemoval: Calculation;
}

interface CalculationTable {
cropType: string;
crop: string;
agronomicBalance: AgronomicBalance;
cropRemovalBalance: CropRemovalCoeficient;
}

export default CalculationTable;
6 changes: 6 additions & 0 deletions frontend/src/Interface/CropRemovalBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface CropRemovalBalanceInterface {
P: number;
K: number;
}

export default CropRemovalBalanceInterface;
22 changes: 1 addition & 21 deletions frontend/src/Interface/CropsDetailsInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,4 @@ interface CropsDetailsInterface {
willSawdustBeApplied: boolean | undefined;
}

/**
* @summary Interface for the main data file
* @description This interface will be for Submission, it works the same as the CropsDetailInterface
* but this is only for the submission of formik.
* @author @Kcaparas
*/

interface SubmissionCropsInterface {
id: number;
cropId: string;
yield: number;
plantAgeYears: string;
numberOfPlantsPerAcre: number;
distanceBtwnPlants: string;
distanceBtwnRows: string;
willPlantsBePruned: boolean | undefined;
whereWillPruningsGo: string;
willSawdustBeApplied: boolean | undefined;
}

export type { CropsDetailsInterface, SubmissionCropsInterface };
export default CropsDetailsInterface;
2 changes: 1 addition & 1 deletion frontend/src/Interface/FieldDetailsInterface.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CropsDetailsInterface } from './CropsDetailsInterface';
import CropsDetailsInterface from './CropsDetailsInterface';
import LeafTestInterface from './LeafTestInterface';
import FertilizerInterface from './FertilizerInterface';
import SoilTestInterface from './SoilTestInterface';
Expand Down
Loading

0 comments on commit 18d6c32

Please sign in to comment.