Skip to content

Commit

Permalink
Merge pull request #3562 from LiteFarmOrg/LF-4541-add-animal-inventor…
Browse files Browse the repository at this point in the history
…y-to-custom-task

Lf 4541 add animal inventory to custom task
  • Loading branch information
SayakaOno authored Dec 6, 2024
2 parents f42af14 + 8bec6c9 commit 97a2ca9
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/de/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1875,7 +1875,7 @@
"QUANTITY_CANNOT_EXCEED": "Die genutzte Erntemenge darf nicht größer sein als die zu verteilende Menge",
"RATE_THIS_TASK": "Bewerten Sie diese Aufgabe",
"REMOVE_HARVEST_USE": "Entfernen",
"SELECT_ANIMALS_TO_MOVE": "MISSING",
"SELECT_ANIMALS": "MISSING",
"SELECT_DATE": "Wählen Sie das Datum der Aufgabe",
"SELECT_TASK_LOCATIONS": "Wählen Sie die Aufgabenstellung(en)",
"SELECT_WILD_CROP": "Diese Aufgabe zielt auf eine Wildpflanze ab",
Expand Down
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,7 @@
"QUANTITY_CANNOT_EXCEED": "Harvest quantity used cannot exceed amount to allocate",
"RATE_THIS_TASK": "Rate this task",
"REMOVE_HARVEST_USE": "Remove",
"SELECT_ANIMALS_TO_MOVE": "Select animals to move",
"SELECT_ANIMALS": "Select animals",
"SELECT_DATE": "Select the task date",
"SELECT_TASK_LOCATIONS": "Select the task location(s)",
"SELECT_WILD_CROP": "This task targets a wild crop",
Expand Down
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/es/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2022,7 +2022,7 @@
"QUANTITY_CANNOT_EXCEED": "La cantidad de cosecha utilizada no puede exceder la cantidad a asignar",
"RATE_THIS_TASK": "Califica esta tarea",
"REMOVE_HARVEST_USE": "Quitar",
"SELECT_ANIMALS_TO_MOVE": "MISSING",
"SELECT_ANIMALS": "MISSING",
"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",
Expand Down
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,7 @@
"QUANTITY_CANNOT_EXCEED": "La quantité de récolte utilisée ne peut pas dépasser la quantité à allouer",
"RATE_THIS_TASK": "Évaluez cette tâche",
"REMOVE_HARVEST_USE": "Supprimer",
"SELECT_ANIMALS_TO_MOVE": "MISSING",
"SELECT_ANIMALS": "MISSING",
"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",
Expand Down
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/hi/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1872,7 +1872,7 @@
"QUANTITY_CANNOT_EXCEED": "फसल उपयोग की मात्रा आवंटित राशि से अधिक नहीं हो सकती",
"RATE_THIS_TASK": "इस कार्य को रेट करें",
"REMOVE_HARVEST_USE": "हटाएं",
"SELECT_ANIMALS_TO_MOVE": "MISSING",
"SELECT_ANIMALS": "MISSING",
"SELECT_DATE": "कार्य तिथि चुनें",
"SELECT_TASK_LOCATIONS": "कार्य स्थान(ओं) का चयन करें",
"SELECT_WILD_CROP": "यह कार्य एक जंगली फसल को लक्षित करता है",
Expand Down
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/pa/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1872,7 +1872,7 @@
"QUANTITY_CANNOT_EXCEED": "ਵਰਤੇ ਗਏ ਵਾਢੀ ਦੀ ਮਾਤਰਾ ਨਿਰਧਾਰਤ ਕੀਤੀ ਰਕਮ ਤੋਂ ਵੱਧ ਨਹੀਂ ਹੋ ਸਕਦੀ",
"RATE_THIS_TASK": "ਇਸ ਕੰਮ ਨੂੰ ਦਰਜਾ ਦਿਓ",
"REMOVE_HARVEST_USE": "ਹਟਾਓ",
"SELECT_ANIMALS_TO_MOVE": "MISSING",
"SELECT_ANIMALS": "MISSING",
"SELECT_DATE": "ਕੰਮ ਦੀ ਮਿਤੀ ਚੁਣੋ",
"SELECT_TASK_LOCATIONS": "ਕੰਮ ਦੇ ਟਿਕਾਣੇ ਚੁਣੋ",
"SELECT_WILD_CROP": "ਇਹ ਕੰਮ ਜੰਗਲੀ ਫਸਲ ਨੂੰ ਨਿਸ਼ਾਨਾ ਬਣਾਉਂਦਾ ਹੈ",
Expand Down
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/pt/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,7 @@
"QUANTITY_CANNOT_EXCEED": "A quantidade de colheita usada não pode exceder a quantidade a ser alocada",
"RATE_THIS_TASK": "Avalie esta tarefa",
"REMOVE_HARVEST_USE": "Remover",
"SELECT_ANIMALS_TO_MOVE": "MISSING",
"SELECT_ANIMALS": "MISSING",
"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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const PureTaskAssignment = ({
handleSubmit,
getValues,
additionalContent,
progress = 86,
}) => {
const { t } = useTranslation();
const wageOverride = watch(WAGE_OVERRIDE);
Expand Down Expand Up @@ -109,7 +110,7 @@ const PureTaskAssignment = ({
onCancel={historyCancel}
title={t('ADD_TASK.ADD_A_TASK')}
cancelModalTitle={t('ADD_TASK.CANCEL')}
value={86}
value={progress}
/>

<AssignTask
Expand Down
7 changes: 4 additions & 3 deletions packages/webapp/src/components/Task/PureTaskCrops/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const PureTaskCrops = ({
isMulti = true,
isRequired,
defaultManagementPlanId,
progress = 57,
history,
location,
}) => {
Expand Down Expand Up @@ -111,8 +112,8 @@ const PureTaskCrops = ({
.map((management_plan) => management_plan.management_plan_id)
.filter((management_plan_id) => managementPlanIds.includes(management_plan_id))
: getValues(MANAGEMENT_PLANS)?.length
? [getValues(MANAGEMENT_PLANS)?.[0]?.management_plan_id]
: [],
? [getValues(MANAGEMENT_PLANS)?.[0]?.management_plan_id]
: [],
);

const onSelectManagementPlan = (management_plan_id) => {
Expand Down Expand Up @@ -249,7 +250,7 @@ const PureTaskCrops = ({
onCancel={historyCancel}
title={t('ADD_TASK.ADD_A_TASK')}
cancelModalTitle={t('ADD_TASK.CANCEL')}
value={57}
value={progress}
/>

<Main style={{ paddingBottom: '20px' }}>{t('ADD_TASK.AFFECT_PLANS')}</Main>
Expand Down
6 changes: 4 additions & 2 deletions packages/webapp/src/components/Task/PureTaskDetails/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import PureIrrigationTask from '../PureIrrigationTask';
import PureSoilAmendmentTask from '../SoilAmendmentTask';
import PureMovementTask from '../MovementTask';
import { defaultValues as soilAmendmentProductDefaultValues } from '../AddSoilAmendmentProducts';
import { getProgress } from '../../../containers/Task/util';

export default function PureTaskDetails({
handleGoBack,
Expand All @@ -32,10 +33,11 @@ export default function PureTaskDetails({
}) {
const { t } = useTranslation();
const taskType = selectedTaskType.task_translation_key;
const taskName = selectedTaskType.task_name;
const isCustomType = !!selectedTaskType.farm_id;
const isHarvest = isTaskType(selectedTaskType, 'HARVEST_TASK');
const isIrrigationTask = isTaskType(selectedTaskType, 'IRRIGATION_TASK');
const isCustomTask = isTaskType(selectedTaskType, 'CUSTOM_TASK');
const progress = isCustomTask ? getProgress('CUSTOM_TASK', 'task_details') : isHarvest ? 67 : 71;

const defaults = {
CLEANING_TASK: { cleaning_task: { agent_used: false } },
Expand Down Expand Up @@ -134,7 +136,7 @@ export default function PureTaskDetails({
onCancel={historyCancel}
title={t('ADD_TASK.ADD_A_TASK')}
cancelModalTitle={t('ADD_TASK.CANCEL')}
value={isHarvest ? 67 : 71}
value={progress}
/>

<Main
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default function PureTaskAnimalInventory({
history,
isDesktop,
isRequired = true,
progress = 43,
}) {
const { t } = useTranslation();
const preSelectedIds = persistedFormData.animalIds || history.location?.state?.animal_ids;
Expand All @@ -46,7 +47,6 @@ export default function PureTaskAnimalInventory({
},
});

const progress = 43;
const { historyCancel } = useHookFormPersist(getValues);
const animalIds = watch(ANIMAL_IDS);
const disabled = isRequired && !animalIds?.length;
Expand Down Expand Up @@ -95,7 +95,7 @@ export default function PureTaskAnimalInventory({
maxHeight: '24px',
}}
>
{t('TASK.SELECT_ANIMALS_TO_MOVE')}
{t('TASK.SELECT_ANIMALS')}
</Main>
<input type="hidden" {...register(ANIMAL_IDS)} />
<AnimalInventory
Expand Down
3 changes: 1 addition & 2 deletions packages/webapp/src/components/Task/TaskDate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function PureTaskDate({
onGoBack,
persistedFormData,
useHookFormPersist,
progress = 28,
}) {
const { t } = useTranslation();

Expand All @@ -28,8 +29,6 @@ export default function PureTaskDate({
defaultValues: { ...persistedFormData },
});

const progress = 28;

const { historyCancel } = useHookFormPersist(getValues);

const DUE_DATE = 'due_date';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export default function PureTaskLocations({
targetsWildCrop,
isAnimalTask = false,
optionalLocation = false,
progress = 43,
}) {
const { t } = useTranslation();
const progress = 43;

const defaultLocations = useMemo(() => {
const locationIdsSet = new Set(locations.map(({ location_id }) => location_id));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 LiteFarm.org
* This file is part of LiteFarm.
*
* LiteFarm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LiteFarm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>.
*/
import { useGetAnimalsQuery, useGetAnimalBatchesQuery } from '../../../store/api/apiSlice';
import useQueries from '../../../hooks/api/useQueries';

const useAnimalsExist = () => {
const { data, isLoading } = useQueries([
{ label: 'animals', hook: useGetAnimalsQuery },
{ label: 'animalBatches', hook: useGetAnimalBatchesQuery },
]);

const animalsExistOnFarm = !isLoading && (data.animals.length || data.animalBatches.length);

return { animalsExistOnFarm };
};

export default useAnimalsExist;
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ import PureTaskAnimalInventory from '../../../components/Task/TaskAnimalInventor
import { HookFormPersistProvider } from '../../hooks/useHookFormPersist/HookFormPersistProvider';
import { useTheme } from '@mui/styles';
import { useMediaQuery } from '@mui/material';
import { useIsTaskType } from '../useIsTaskType';
import { getProgress } from '../util';

function TaskAnimalInventory({ history, location }) {
const isCustomTask = useIsTaskType('CUSTOM_TASK');
const progress = isCustomTask ? getProgress('CUSTOM_TASK', 'task_animal_selection') : undefined;

const onGoBack = () => {
history.back();
};

const onContinue = () => {
history.push('/add_task/task_locations', location?.state);
isCustomTask
? history.push('/add_task/task_details', location?.state)
: history.push('/add_task/task_locations', location?.state);
};

const theme = useTheme();
const isDesktop = useMediaQuery(theme.breakpoints.up('lg'));

Expand All @@ -36,6 +44,8 @@ function TaskAnimalInventory({ history, location }) {
onContinue={onContinue}
history={history}
isDesktop={isDesktop}
isRequired={!isCustomTask}
progress={progress}
/>
</HookFormPersistProvider>
);
Expand Down
6 changes: 5 additions & 1 deletion packages/webapp/src/containers/Task/TaskAssignment/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
ASSIGNEE,
ALREADY_COMPLETED,
} from '../../../components/Task/AssignTask/constants';
import { getProgress } from '../util';
import { useIsTaskType } from '../useIsTaskType';

export default function TaskManagement({ history, match, location }) {
const userFarms = useSelector(userFarmEntitiesSelector);
Expand All @@ -31,7 +33,8 @@ export default function TaskManagement({ history, match, location }) {
const persistedFormData = useSelector(hookFormPersistSelector);
const [isFarmWorker] = useState(userFarm.role_id === 3);
const worker = users[userFarm.user_id];

const isCustomTask = useIsTaskType('CUSTOM_TASK');
const progress = isCustomTask ? getProgress('CUSTOM_TASK', 'task_assignment') : undefined;
const [showCannotCreateModal, setShowCannotCreateModal] = useState(false);

const defaultAssignee = useMemo(() => {
Expand Down Expand Up @@ -184,6 +187,7 @@ export default function TaskManagement({ history, match, location }) {
override={override}
{...taskAssignForm}
additionalContent={taskCompleted}
progress={progress}
/>
</HookFormPersistProvider>
{showCannotCreateModal && (
Expand Down
25 changes: 25 additions & 0 deletions packages/webapp/src/containers/Task/TaskCrops/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ import {
} from './useManagementPlanTilesByLocationIds';
import { cropLocationsSelector } from '../../locationSlice';
import { useIsTaskType } from '../useIsTaskType';
import { getProgress } from '../util';
import useAnimalsExist from '../../Animals/Inventory/useAnimalsExist';

export default function ManagementPlanSelector({ history, match, location }) {
const isTransplantTask = useIsTaskType('TRANSPLANT_TASK');
const isCustomTask = useIsTaskType('CUSTOM_TASK');
if (isTransplantTask)
return (
<TransplantManagementPlansSelector history={history} match={match} location={location} />
);
if (isCustomTask)
return (
<CustomTaskManagementPlansSelector history={history} match={match} location={location} />
);
return <TaskCrops history={history} match={match} location={location} />;
}

Expand All @@ -35,13 +42,30 @@ function TransplantManagementPlansSelector({ history, match, location }) {
);
}

function CustomTaskManagementPlansSelector({ history, match, location }) {
const { animalsExistOnFarm } = useAnimalsExist();
const onContinuePath = animalsExistOnFarm ? '/add_task/task_animal_selection' : undefined;
const progress = getProgress('CUSTOM_TASK', 'task_crops');

return (
<TaskCrops
onContinuePath={onContinuePath}
history={history}
match={match}
location={location}
progress={progress}
/>
);
}

function TaskCrops({
history,
match,
goBackPath = '/add_task/task_locations',
onContinuePath = '/add_task/task_details',
locations,
location,
progress,
}) {
const persistedPaths = [goBackPath, onContinuePath];
const handleGoBack = () => {
Expand Down Expand Up @@ -76,6 +100,7 @@ function TaskCrops({
isRequired={isRequired}
wildManagementPlanTiles={showWildCrops ? wildManagementPlanTiles : undefined}
defaultManagementPlanId={location?.state?.management_plan_id ?? null}
progress={progress}
history={history}
location={location}
/>
Expand Down
6 changes: 5 additions & 1 deletion packages/webapp/src/containers/Task/TaskDate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { HookFormPersistProvider } from '../../hooks/useHookFormPersist/HookForm
import { useIsTaskType } from '../useIsTaskType';
import { useSelector } from 'react-redux';
import { tasksByManagementPlanIdSelector } from '../../taskSlice';
import { getProgress } from '../util';

function TaskDate({ history, match, location }) {
const onGoBack = () => {
history.back();
};
const isTransplantTask = useIsTaskType('TRANSPLANT_TASK');
const isMovementTask = useIsTaskType('MOVEMENT_TASK');
const isCustomTask = useIsTaskType('CUSTOM_TASK');

const tasks = location.state.management_plan_id
? useSelector(tasksByManagementPlanIdSelector(location.state.management_plan_id))
Expand Down Expand Up @@ -73,9 +75,11 @@ function TaskDate({ history, match, location }) {
history.push(getNextStepPath(), location?.state);
};

const progress = isCustomTask ? getProgress('CUSTOM_TASK', 'task_date') : undefined;

return (
<HookFormPersistProvider>
<PureTaskDate onGoBack={onGoBack} onContinue={onContinue} />
<PureTaskDate onGoBack={onGoBack} onContinue={onContinue} progress={progress} />
</HookFormPersistProvider>
);
}
Expand Down
Loading

0 comments on commit 97a2ca9

Please sign in to comment.