Skip to content

Commit

Permalink
Merge pull request #3308 from LiteFarmOrg/LF-4221_2/Fix_completion_flow
Browse files Browse the repository at this point in the history
LF-4221: Fix soil amendment task completion flow
  • Loading branch information
kathyavini authored Jul 18, 2024
2 parents 1f403fe + fbe3622 commit 87762c1
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 57 deletions.
34 changes: 32 additions & 2 deletions packages/webapp/src/components/Task/TaskComplete/StepOne.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import { PurePlantingTask } from '../PlantingTask';
import PureIrrigationTask from '../PureIrrigationTask';
import { formatTaskReadOnlyDefaultValues } from '../../../util/task';

const soilAmendmentContinueDisabled = (needsChange, isValid) => {
if (!needsChange) {
return false;
}
return !isValid;
};

export default function PureCompleteStepOne({
persistedFormData,
onContinue,
Expand Down Expand Up @@ -51,14 +58,37 @@ export default function PureCompleteStepOne({
const changesRequired = watch(CHANGES_NEEDED);
const taskType = selectedTaskType?.task_translation_key;

const continueDisabled =
taskType === 'SOIL_AMENDMENT_TASK'
? soilAmendmentContinueDisabled(getValues(CHANGES_NEEDED), isValid)
: !isValid;

const onSubmit = (event) => {
event.preventDefault();

if (
taskType === 'SOIL_AMENDMENT_TASK' &&
soilAmendmentContinueDisabled(getValues(CHANGES_NEEDED)) === false
) {
onContinue();
} else {
handleSubmit(onContinue)();
}
};

return (
<Form
buttonGroup={
<Button data-cy="beforeComplete-submit" type={'submit'} disabled={!isValid} fullLength>
<Button
data-cy="beforeComplete-submit"
type={'submit'}
disabled={continueDisabled}
fullLength
>
{t('common:CONTINUE')}
</Button>
}
onSubmit={handleSubmit(onContinue)}
onSubmit={onSubmit}
>
<MultiStepPageTitle
style={{ marginBottom: '24px' }}
Expand Down
118 changes: 66 additions & 52 deletions packages/webapp/src/components/Task/TaskComplete/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,71 @@ export default function PureTaskComplete({
const disabled = !isValid || (!happiness && !prefer_not_to_say);

const isIrrigationLocation = useIsTaskType('IRRIGATION_TASK');
const onSubmit = (event) => {
event.preventDefault();

let task_type_name = persistedFormData?.taskType.task_translation_key.toLowerCase();

const format = (formData) => {
let completeDate = '';
switch (formData[DATE_CHOICE]) {
case TODAY_DUE_DATE:
completeDate = date_today;
break;
case ANOTHER_DUE_DATE:
completeDate = formData[ANOTHER_DATE];
break;
case ORIGINAL_DUE_DATE:
default:
completeDate = date_due;
break;
}

let data = {
taskData: {
duration: duration,
happiness: prefer_not_to_say ? null : happiness,
completion_notes: notes,
complete_date: completeDate,
},
task_translation_key: persistedFormData?.taskType.task_translation_key,
isCustomTaskType: !!persistedFormData?.taskType.farm_id,
};
const isFieldWork = task_type_name === 'field_work_task';
const isOtherFieldWork =
isFieldWork && persistedFormData?.field_work_task?.field_work_task_type.value === 'OTHER';
if (persistedFormData?.need_changes && !isOtherFieldWork) {
data.taskData[task_type_name] = getObjectInnerValues(persistedFormData[task_type_name]);
} else if (isOtherFieldWork) {
data.taskData[task_type_name] = { ...persistedFormData[task_type_name] };
}
//TODO: replace with useIsTaskType
if (task_type_name === 'harvest_task') {
data.harvest_uses = persistedFormData?.harvest_uses;
data.taskData[task_type_name] = {
...persistedFormData?.harvest_task,
actual_quantity: persistedFormData?.actual_quantity,
actual_quantity_unit: persistedFormData?.actual_quantity_unit.value,
};
}
if (isIrrigationLocation && persistedFormData.locations?.length) {
data.location_id = persistedFormData.locations[0].location_id;
}
if (TASKTYPE_PRODUCT_MAP[data.task_translation_key]) {
const taskProductKey = TASKTYPE_PRODUCT_MAP[data.task_translation_key];
data[taskProductKey] = persistedFormData?.[taskProductKey];
}

return data;
};

if (task_type_name === 'soil_amendment_task' && getValues('need_changes') === false) {
const formData = getValues();
onSave(format(formData));
} else {
handleSubmit((data) => onSave(format(data)))();
}
};

return (
<Form
Expand All @@ -80,58 +145,7 @@ export default function PureTaskComplete({
{t('common:SAVE')}
</Button>
}
onSubmit={handleSubmit((formData) => {
let completeDate = '';
switch (formData[DATE_CHOICE]) {
case TODAY_DUE_DATE:
completeDate = date_today;
break;
case ANOTHER_DUE_DATE:
completeDate = formData[ANOTHER_DATE];
break;
case ORIGINAL_DUE_DATE:
default:
completeDate = date_due;
break;
}

let data = {
taskData: {
duration: duration,
happiness: prefer_not_to_say ? null : happiness,
completion_notes: notes,
complete_date: completeDate,
},
task_translation_key: persistedFormData?.taskType.task_translation_key,
isCustomTaskType: !!persistedFormData?.taskType.farm_id,
};
let task_type_name = persistedFormData?.taskType.task_translation_key.toLowerCase();
const isFieldWork = task_type_name === 'field_work_task';
const isOtherFieldWork =
isFieldWork && persistedFormData?.field_work_task?.field_work_task_type.value === 'OTHER';
if (persistedFormData?.need_changes && !isOtherFieldWork) {
data.taskData[task_type_name] = getObjectInnerValues(persistedFormData[task_type_name]);
} else if (isOtherFieldWork) {
data.taskData[task_type_name] = { ...persistedFormData[task_type_name] };
}
//TODO: replace with useIsTaskType
if (task_type_name === 'harvest_task') {
data.harvest_uses = persistedFormData?.harvest_uses;
data.taskData[task_type_name] = {
...persistedFormData?.harvest_task,
actual_quantity: persistedFormData?.actual_quantity,
actual_quantity_unit: persistedFormData?.actual_quantity_unit.value,
};
}
if (isIrrigationLocation && persistedFormData.locations?.length) {
data.location_id = persistedFormData.locations[0].location_id;
}
if (TASKTYPE_PRODUCT_MAP[data.task_translation_key]) {
const taskProductKey = TASKTYPE_PRODUCT_MAP[data.task_translation_key];
data[taskProductKey] = persistedFormData?.[taskProductKey];
}
onSave(data);
})}
onSubmit={onSubmit}
>
<MultiStepPageTitle
style={{ marginBottom: '24px' }}
Expand Down
9 changes: 6 additions & 3 deletions packages/webapp/src/util/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type DBSoilAmendmentTaskProduct = {
weight_unit?: string;
volume?: number;
volume_unit?: string;
percent_of_location_amended: number;
percent_of_location_amended?: number;
total_area_amended: number;
application_rate_weight?: number;
application_rate_weight_unit?: string;
Expand Down Expand Up @@ -86,7 +86,9 @@ type FormTask = {

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

export const formatSoilAmendmentTaskToFormStructure = (task: DBTask | FormTask): FormTask => {
Expand All @@ -98,13 +100,14 @@ export const formatSoilAmendmentTaskToFormStructure = (task: DBTask | FormTask):

const formattedTaskProducts = task.soil_amendment_task_products.map(
(dbTaskProduct: DBSoilAmendmentTaskProduct): FormSoilAmendmentTaskProduct => {
const { purpose_relationships, ...rest } = dbTaskProduct;
const { purpose_relationships, percent_of_location_amended, ...rest } = dbTaskProduct;
const isWeight = !!(rest.weight || rest.weight === 0);

const formattedTaskProduct = {
...rest,
purposes: [],
is_weight: isWeight,
percent_of_location_amended: percent_of_location_amended ?? 100,
} as FormSoilAmendmentTaskProduct;

dbTaskProduct.purpose_relationships.forEach(({ purpose_id, other_purpose }) => {
Expand Down

0 comments on commit 87762c1

Please sign in to comment.