Skip to content

Commit

Permalink
Merge pull request #3301 from LiteFarmOrg/LF_4221_2/duncanbranch
Browse files Browse the repository at this point in the history
LF-4221_2 - fixing bugs
  • Loading branch information
kathyavini authored Jul 18, 2024
2 parents c5fead3 + 7269329 commit c720de1
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/webapp/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,7 @@
"SELECT_DATE": "Select the task date",
"SELECT_TASK_LOCATIONS": "Select the task location(s)",
"SELECT_WILD_CROP": "This task targets a wild crop",
"SOIL_AMENDMENT_LOCATION": "Select the soil amendment location(s)",
"STATUS": {
"ABANDONED": "Abandoned",
"COMPLETED": "Completed",
Expand Down
1 change: 1 addition & 0 deletions packages/webapp/public/locales/es/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,7 @@
"SELECT_DATE": "Seleccione la fecha de la tarea",
"SELECT_TASK_LOCATIONS": "Seleccione la(s) ubicación(es) de la tarea",
"SELECT_WILD_CROP": "Esta tarea se dirige a un cultivo silvestre",
"SOIL_AMENDMENT_LOCATION": "MISSING",
"STATUS": {
"ABANDONED": "Abandonada",
"COMPLETED": "Completada",
Expand Down
1 change: 1 addition & 0 deletions packages/webapp/public/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,7 @@
"SELECT_DATE": "Sélectionnez la date de la tâche",
"SELECT_TASK_LOCATIONS": "Sélectionnez le(s) emplacement(s) de la tâche",
"SELECT_WILD_CROP": "Cette tâche cible une culture sauvage",
"SOIL_AMENDMENT_LOCATION": "MISSING",
"STATUS": {
"ABANDONED": "Abandonnée",
"COMPLETED": "Terminée",
Expand Down
1 change: 1 addition & 0 deletions packages/webapp/public/locales/pt/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,7 @@
"SELECT_DATE": "Selecionar a data da tarefa",
"SELECT_TASK_LOCATIONS": "Selecionar o(s) local(is) da tarefa",
"SELECT_WILD_CROP": "Esta tarefa é para um cultivo silvestre",
"SOIL_AMENDMENT_LOCATION": "MISSING",
"STATUS": {
"ABANDONED": "Abandonada",
"COMPLETED": "Completa",
Expand Down
25 changes: 25 additions & 0 deletions packages/webapp/src/containers/Task/TaskLocations/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default function TaskLocationsSwitch({ history, match, location }) {
const isHarvestLocation = useIsTaskType('HARVEST_TASK');
const isIrrigationLocation = useIsTaskType('IRRIGATION_TASK');
const isTransplantLocation = useIsTaskType('TRANSPLANT_TASK');
const isSoilAmendmentLocation = useIsTaskType('SOIL_AMENDMENT_TASK');

if (isHarvestLocation) {
return <TaskActiveAndPlannedCropLocations history={history} location={location} />;
Expand All @@ -40,6 +41,10 @@ export default function TaskLocationsSwitch({ history, match, location }) {
return <TaskIrrigationLocations history={history} location={location} />;
}

if (isSoilAmendmentLocation) {
return <TaskSoilAmendmentLocations history={history} location={location} />;
}

return <TaskAllLocations history={history} location={location} />;
}

Expand Down Expand Up @@ -111,6 +116,26 @@ function TaskIrrigationLocations({ history, location }) {
);
}

//This goes to all crop locations, multiSelect, not wildCrops with pins
function TaskSoilAmendmentLocations({ history, location }) {
const { t } = useTranslation();
const cropLocations = useSelector(cropLocationsSelector);
const onContinue = () => {
history.push('/add_task/task_crops', location.state);
};

return (
<TaskLocations
locations={cropLocations}
history={history}
isMulti={true}
title={t('TASK.SOIL_AMENDMENT_LOCATION')}
onContinue={onContinue}
location={location}
/>
);
}

function TaskAllLocations({ history, location }) {
const dispatch = useDispatch();
const locations = useSelector(locationsSelector);
Expand Down
20 changes: 18 additions & 2 deletions packages/webapp/src/containers/Task/saga.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ import {
createCompleteTaskUrl,
} from '../../util/siteMapConstants';
import { setPersistedPaths, setFormData } from '../hooks/useHookFormPersist/hookFormPersistSlice';
import { formatSoilAmendmentProductToDBStructure, getRemovedTaskProductIds } from '../../util/task';
import {
formatSoilAmendmentTaskToDBStructure,
formatSoilAmendmentProductToDBStructure,
getRemovedTaskProductIds,
} from '../../util/task';
import { TASKTYPE_PRODUCT_MAP } from './constants';
import { api } from '../../store/api/apiSlice';

Expand Down Expand Up @@ -526,10 +530,13 @@ const getSoilAmendmentTaskBody = (
data,
endpoint,
managementPlanWithCurrentLocationEntities,
{ purposes },
{ purposes, methods },
) => {
return {
...getPostTaskBody(data, endpoint, managementPlanWithCurrentLocationEntities),
soil_amendment_task: formatSoilAmendmentTaskToDBStructure(data.soil_amendment_task, {
methods,
}),
soil_amendment_task_products: formatSoilAmendmentProductToDBStructure(
data.soil_amendment_task_products,
{ purposes },
Expand Down Expand Up @@ -591,6 +598,8 @@ export function* createTaskSaga({ payload }) {
api.endpoints.getSoilAmendmentPurposes.select()(state),
);
taskTypeSpecificData.purposes = purposes.data;
const methods = yield select((state) => api.endpoints.getSoilAmendmentMethods.select()(state));
taskTypeSpecificData.methods = methods.data;
}
const header = getHeader(user_id, farm_id);
const isCustomTask = !!task_farm_id;
Expand Down Expand Up @@ -773,12 +782,17 @@ const getCompleteIrrigationTaskBody = (task_translation_key) => (data) => {
};

const getCompleteSoilAmendmentTaskBody = (data, taskTypeSpecificData) => {
const soilAmendmentTask = formatSoilAmendmentTaskToDBStructure(
data.soil_amendment_task,
taskTypeSpecificData,
);
const soilAmendmentTaskProducts = formatSoilAmendmentProductToDBStructure(
data.soil_amendment_task_products,
taskTypeSpecificData,
);
return {
...data.taskData,
soil_amendment_task: soilAmendmentTask,
soil_amendment_task_products: soilAmendmentTaskProducts,
};
};
Expand Down Expand Up @@ -814,6 +828,8 @@ export function* completeTaskSaga({ payload: { task_id, data, returnPath } }) {
api.endpoints.getSoilAmendmentPurposes.select()(state),
);
taskTypeSpecificData.purposes = purposes.data;
const methods = yield select((state) => api.endpoints.getSoilAmendmentMethods.select()(state));
taskTypeSpecificData.methods = methods.data;
}
const taskData = taskTypeGetCompleteTaskBodyFunctionMap[task_translation_key]
? taskTypeGetCompleteTaskBodyFunctionMap[task_translation_key](data, taskTypeSpecificData)
Expand Down
59 changes: 49 additions & 10 deletions packages/webapp/src/util/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>.
*/

import { SoilAmendmentPurpose } from '../store/api/types';
import { SoilAmendmentMethod, SoilAmendmentPurpose } from '../store/api/types';
import { getUnitOptionMap } from './convert-units/getUnitOptionMap';

interface UnitOption {
Expand Down Expand Up @@ -61,27 +61,37 @@ type FormSoilAmendmentTaskProduct = {
};

type DBSoilAmendmentTask = {
soil_amendment_task_products: DBSoilAmendmentTaskProduct[];
furrow_hole_depth?: number;
furrow_hole_depth_unit?: string;
other_application_method?: string;
[key: string]: any;
};

type FormSoilAmendmentTask = {
furrow_hole_depth?: number;
furrow_hole_depth_unit?: UnitOption;
other_application_method?: string;
[key: string]: any;
};

type DBTask = {
soil_amendment_task_products: DBSoilAmendmentTaskProduct[];
[key: string]: any;
};

type FormTask = {
soil_amendment_task_products: FormSoilAmendmentTaskProduct[];
[key: string]: any;
};

// Type guard
function isFormSoilAmendmentTask(
task: DBSoilAmendmentTask | FormSoilAmendmentTask,
): task is FormSoilAmendmentTask {
function isFormSoilAmendmentTask(task: DBTask | FormTask): task is FormTask {
return 'purposes' in task.soil_amendment_task_products[0];
}

export const formatSoilAmendmentTaskToFormStructure = (
task: DBSoilAmendmentTask | FormSoilAmendmentTask,
): FormSoilAmendmentTask => {
export const formatSoilAmendmentTaskToFormStructure = (task: DBTask | FormTask): FormTask => {
if (isFormSoilAmendmentTask(task)) {
return task as FormSoilAmendmentTask;
return task as FormTask;
}

const taskClone = structuredClone(task);
Expand Down Expand Up @@ -177,12 +187,41 @@ export const formatSoilAmendmentProductToDBStructure = (
});
};

export const formatSoilAmendmentTaskToDBStructure = (
soilAmendmentTask: FormSoilAmendmentTask,
{ methods }: { methods: SoilAmendmentMethod[] },
): DBSoilAmendmentTask => {
const {
method_id,
furrow_hole_depth,
furrow_hole_depth_unit,
other_application_method,
...rest
} = soilAmendmentTask;
const furrowHoleId = methods?.find(({ key }) => key === 'FURROW_HOLE')?.id;
const otherMethodId = methods?.find(({ key }) => key === 'OTHER')?.id;
if (!furrowHoleId) {
throw Error('id for FURROW_HOLE method does not exist');
}
if (!otherMethodId) {
throw Error('id for OTHER method does not exist');
}
return {
...rest,
method_id,
furrow_hole_depth: method_id === furrowHoleId ? furrow_hole_depth : undefined,
furrow_hole_depth_unit: method_id === furrowHoleId ? furrow_hole_depth_unit?.value : undefined,
other_application_method:
soilAmendmentTask.method_id === otherMethodId ? other_application_method : undefined,
};
};

export const formatTaskReadOnlyDefaultValues = (task: {
taskType?: { task_translation_key: string };
[key: string]: any;
}) => {
if (task.taskType?.task_translation_key === 'SOIL_AMENDMENT_TASK') {
return formatSoilAmendmentTaskToFormStructure(task as DBSoilAmendmentTask);
return formatSoilAmendmentTaskToFormStructure(task as DBTask);
}

return structuredClone(task);
Expand Down

0 comments on commit c720de1

Please sign in to comment.