Skip to content

Commit

Permalink
Merge pull request #3294 from LiteFarmOrg/LF-4221_2/Update_soil_amend…
Browse files Browse the repository at this point in the history
…ment_task_flows_using_new_AddProducts_component

LF-4221 (2): Update soil amendment task flows using new add products component
  • Loading branch information
Duncan-Brain authored Jul 17, 2024
2 parents 7bb4d4b + cbc2b1c commit 2293327
Show file tree
Hide file tree
Showing 17 changed files with 437 additions and 66 deletions.
2 changes: 0 additions & 2 deletions packages/api/src/routes/taskRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from '../middleware/validation/assignTask.js';
import taskController from '../controllers/taskController.js';
import { createOrPatchProduct } from '../middleware/validation/product.js';
import checkParamAgainstBody from '../middleware/acl/checkParamAgainstBody.js';
import {
checkAbandonTask,
checkCompleteTask,
Expand Down Expand Up @@ -173,7 +172,6 @@ router.patch(
modelMapping['soil_amendment_task'],
hasFarmAccess({ params: 'task_id' }),
checkScope(['edit:task']),
checkParamAgainstBody('task_id'),
checkCompleteTask('soil_amendment_task'),
taskController.completeTask('soil_amendment_task'),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ const SoilAmendmentProductCard = ({

return (
<div className={styles.productCard}>
{onRemove && <SmallButton onClick={onRemove} className={styles.removeButton} />}
{!isReadOnly && onRemove && (
<SmallButton onClick={onRemove} className={styles.removeButton} />
)}
<div>
<Controller
control={control}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,13 @@ const QuantityApplicationRate = ({
const APPLICATION_RATE_VOLUME = `${namePrefix}.${TASK_PRODUCT_FIELD_NAMES.APPLICATION_RATE_VOLUME}`;
const APPLICATION_RATE_WEIGHT_UNIT = `${namePrefix}.${TASK_PRODUCT_FIELD_NAMES.APPLICATION_RATE_WEIGHT_UNIT}`;
const APPLICATION_RATE_VOLUME_UNIT = `${namePrefix}.${TASK_PRODUCT_FIELD_NAMES.APPLICATION_RATE_VOLUME_UNIT}`;
const IS_WEIGHT = `${namePrefix}.${TASK_PRODUCT_FIELD_NAMES.IS_WEIGHT}`;

const isWeight = getValues(IS_WEIGHT);

const [isExpanded, setIsExpanded] = useState(false);
const toggleExpanded = () => setIsExpanded((prev) => !prev);

const [isWeight, setIsWeight] = useState(() => {
const volumeValue = getValues(VOLUME);
return isNaN(Number(volumeValue));
});

const toggleMeasure = () => {
setIsWeight((prev) => !prev);
};

let total_area, total_area_unit, type;
const locationCount = locations.length;
if (locationCount === 1) {
Expand Down Expand Up @@ -145,7 +139,7 @@ const QuantityApplicationRate = ({
leftLabel={formatLabel('ADD_TASK.SOIL_AMENDMENT_VIEW.WEIGHT', isWeight)}
label={formatLabel('ADD_TASK.SOIL_AMENDMENT_VIEW.VOLUME', !isWeight)}
isToggleVariant
onChange={toggleMeasure}
onChange={(e) => setValue(IS_WEIGHT, e.target.checked ? false : true)}
checked={!isWeight}
disabled={isReadOnly}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface ProductFields {
product_id: ProductId;
}

const FIELD_NAME = 'soil_amendment_task.soil_amendment_task_products';
const FIELD_NAME = 'soil_amendment_task_products';

const defaultValues = {
[TASK_PRODUCT_FIELD_NAMES.PRODUCT_ID]: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const TASK_PRODUCT_FIELD_NAMES = {
APPLICATION_RATE_WEIGHT_UNIT: 'application_rate_weight_unit',
APPLICATION_RATE_VOLUME: 'application_rate_volume',
APPLICATION_RATE_VOLUME_UNIT: 'application_rate_volume_unit',
IS_WEIGHT: 'is_weight',
} as const;

export const PRODUCT_FIELD_NAMES = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export default function PureTaskDetails({

const defaults = {
CLEANING_TASK: { cleaning_task: { agent_used: false } },
SOIL_AMENDMENT_TASK: { soil_amendment_task: { soil_amendment_task_products: [{}] } }, // TODO
SOIL_AMENDMENT_TASK: {
soil_amendment_task_products: [{}],
},
};

const harvest_tasks = useMemo(() => {
Expand Down
8 changes: 4 additions & 4 deletions packages/webapp/src/components/Task/TaskComplete/StepOne.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import PureCleaningTask from '../CleaningTask';
import PureSoilAmendmentTask from '../SoilAmendmentTask';
import PureFieldWorkTask from '../FieldWorkTask';
import PurePestControlTask from '../PestControlTask';
import { cloneObject } from '../../../util';
import { PurePlantingTask } from '../PlantingTask';
import PureIrrigationTask from '../PureIrrigationTask';
import { formatTaskReadOnlyDefaultValues } from '../../../util/task';

export default function PureCompleteStepOne({
persistedFormData,
Expand All @@ -26,9 +26,9 @@ export default function PureCompleteStepOne({
useHookFormPersist,
}) {
const { t } = useTranslation();
const defaultsToUse = persistedFormData.need_changes
? cloneObject(persistedFormData)
: cloneObject(selectedTask);
const defaultsToUse = formatTaskReadOnlyDefaultValues(
persistedFormData.need_changes ? persistedFormData : selectedTask,
);
const {
register,
handleSubmit,
Expand Down
5 changes: 5 additions & 0 deletions packages/webapp/src/components/Task/TaskComplete/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getDateInputFormat } from '../../../util/moment';
import { isNotInFuture } from '../../Form/Input/utils';
import { useIsTaskType } from '../../../containers/Task/useIsTaskType';
import { ORIGINAL_DUE_DATE, TODAY_DUE_DATE, ANOTHER_DUE_DATE } from '../AbandonTask/constants';
import { TASKTYPE_PRODUCT_MAP } from '../../../containers/Task/constants';

export default function PureTaskComplete({
onSave,
Expand Down Expand Up @@ -125,6 +126,10 @@ export default function PureTaskComplete({
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);
})}
>
Expand Down
4 changes: 2 additions & 2 deletions packages/webapp/src/components/Task/TaskReadOnly/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { useForm } from 'react-hook-form';
import TimeSlider from '../../Form/Slider/TimeSlider';
import Rating from '../../Rating';
import Checkbox from '../../Form/Checkbox';
import { cloneObject } from '../../../util';
import PureCleaningTask from '../CleaningTask';
import PureFieldWorkTask from '../FieldWorkTask';
import PureSoilAmendmentTask from '../SoilAmendmentTask';
Expand All @@ -54,6 +53,7 @@ import PureIrrigationTask from '../PureIrrigationTask';
import DeleteBox from './DeleteBox';
import { userFarmSelector } from '../../../containers/userFarmSlice';
import { certifierSurveySelector } from '../../../containers/OrganicCertifierSurvey/slice';
import { formatTaskReadOnlyDefaultValues } from '../../../util/task';

export default function PureTaskReadOnly({
onGoBack,
Expand Down Expand Up @@ -120,7 +120,7 @@ export default function PureTaskReadOnly({
} = useForm({
mode: 'onChange',
shouldUnregister: false,
defaultValues: cloneObject(task),
defaultValues: formatTaskReadOnlyDefaultValues(task),
});

const taskAfterCompleteComponents = {
Expand Down
4 changes: 4 additions & 0 deletions packages/webapp/src/containers/Task/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ export const TASK_TYPES = {
HARVEST: 'harvest_tasks',
IRRIGATION: 'irrigation_task',
};

export const TASKTYPE_PRODUCT_MAP = {
SOIL_AMENDMENT_TASK: 'soil_amendment_task_products',
};
Loading

0 comments on commit 2293327

Please sign in to comment.