Skip to content

Commit

Permalink
Merge pull request #3554 from LiteFarmOrg/LF-4528/Hide_Created_in_Err…
Browse files Browse the repository at this point in the history
…or_option_for_animals_with_completed_Tasks

LF-4528: Hide created in error option for animals with completed tasks
  • Loading branch information
kathyavini authored Dec 4, 2024
2 parents fc62aaa + 0d60991 commit ee6c16a
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/api/src/controllers/animalBatchController.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const animalBatchController = {
group_ids: true,
sex_detail: true,
animal_batch_use_relationships: true,
tasks: true,
});
return res.status(200).send(
rows.map(({ animal_union_batch, group_ids, ...rest }) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/controllers/animalController.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const animalController = {
animal_union_batch: true,
group_ids: true,
animal_use_relationships: true,
tasks: true,
});
return res.status(200).send(
rows.map(({ animal_union_batch, group_ids, ...rest }) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/models/animalBatchModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class AnimalBatchModel extends baseModel {
},
to: 'task.task_id',
},
modify: (query) => query.select('task.task_id').where('deleted', false),
},
default_type: {
modelClass: DefaultAnimalTypeModel,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/models/animalModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class Animal extends baseModel {
},
to: 'task.task_id',
},
modify: (query) => query.select('task.task_id').where('deleted', false),
},
default_type: {
modelClass: DefaultAnimalTypeModel,
Expand Down
2 changes: 2 additions & 0 deletions packages/api/tests/animal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,14 @@ describe('Animal Tests', () => {
internal_identifier: res.body[0].internal_identifier,
group_ids: [],
animal_use_relationships: [],
tasks: [],
}).toMatchObject(res.body[0]);
expect({
...secondAnimal,
internal_identifier: res.body[1].internal_identifier,
group_ids: [],
animal_use_relationships: [],
tasks: [],
}).toMatchObject(res.body[1]);
}
});
Expand Down
2 changes: 2 additions & 0 deletions packages/api/tests/animal_batch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,14 @@ describe('Animal Batch Tests', () => {
internal_identifier: 1,
group_ids: [],
animal_batch_use_relationships: [],
tasks: [],
}).toMatchObject(res.body[0]);
expect({
...secondAnimalBatch,
internal_identifier: 2,
group_ids: [],
animal_batch_use_relationships: [],
tasks: [],
}).toMatchObject(res.body[1]);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>.
*/
import { useEffect, useMemo } from 'react';
import { useMemo } from 'react';
import Drawer from '../../Drawer';
import Button from '../../Form/Button';
import ReactSelect from '../../Form/ReactSelect';
Expand All @@ -24,7 +24,6 @@ import Input, { getInputErrors } from '../../Form/Input';
import { ReactComponent as WarningIcon } from '../../../assets/images/warning.svg';
import { ReactComponent as CheckIcon } from '../../../assets/images/check-circle.svg';
import { useTranslation } from 'react-i18next';
import { ClassNamesConfig } from 'react-select';
import { getLocalDateInYYYYDDMM } from '../../../util/date';
import { useGetAnimalRemovalReasonsQuery } from '../../../store/api/apiSlice';
import type { AnimalRemovalReasonKeys } from '../../../store/api/types';
Expand Down Expand Up @@ -54,6 +53,7 @@ type RemoveAnimalsModalProps = {
onClose: () => void;
onConfirm: SubmitHandler<FormFields>;
showSuccessMessage: boolean;
hideDeleteOption?: boolean;
};

export default function RemoveAnimalsModal(props: RemoveAnimalsModalProps) {
Expand All @@ -62,7 +62,6 @@ export default function RemoveAnimalsModal(props: RemoveAnimalsModalProps) {
handleSubmit,
control,
watch,
reset,
formState: { errors, isValid },
} = useForm<FormFields>({
mode: 'onChange',
Expand Down Expand Up @@ -110,10 +109,14 @@ export default function RemoveAnimalsModal(props: RemoveAnimalsModalProps) {
label: t('common:OTHER'),
value: RemovalReasons.OTHER,
},
{
label: t('REMOVE_ANIMALS.CREATED_IN_ERROR'),
value: RemovalReasons.CREATED_IN_ERROR,
},
...(props.hideDeleteOption
? []
: [
{
label: t('REMOVE_ANIMALS.CREATED_IN_ERROR'),
value: RemovalReasons.CREATED_IN_ERROR,
},
]),
];

const selectedOption = watch(REASON);
Expand Down
7 changes: 3 additions & 4 deletions packages/webapp/src/containers/Animals/Inventory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,8 @@ export default function AnimalInventory({
[updateSelectedTypeIds],
);

const { onConfirmRemoveAnimals, removalModalOpen, setRemovalModalOpen } = useAnimalOrBatchRemoval(
selectedInventoryIds,
setSelectedInventoryIds,
);
const { onConfirmRemoveAnimals, removalModalOpen, setRemovalModalOpen, hasFinalizedTasks } =
useAnimalOrBatchRemoval(selectedInventoryIds, inventory, setSelectedInventoryIds);

const animalsColumns = useMemo(
() => [
Expand Down Expand Up @@ -463,6 +461,7 @@ export default function AnimalInventory({
onClose={() => setRemovalModalOpen(false)}
onConfirm={onConfirmRemoveAnimals}
showSuccessMessage={false}
hideDeleteOption={hasFinalizedTasks}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type AnimalInventory = {
custom_breed_id: number | null;
default_breed_id: number | null;
location_id?: string | null;
tasks: Animal['tasks'];
};

const { t } = i18n;
Expand Down Expand Up @@ -172,6 +173,7 @@ const formatAnimalsData = (
custom_breed_id: animal.custom_breed_id,
default_breed_id: animal.default_breed_id,
location_id: animal.location_id,
tasks: animal.tasks,
};
});
};
Expand Down Expand Up @@ -211,6 +213,7 @@ const formatAnimalBatchesData = (
custom_breed_id: batch.custom_breed_id,
default_breed_id: batch.default_breed_id,
location_id: batch.location_id,
tasks: batch.tasks,
};
});
};
Expand Down
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 { Dispatch, SetStateAction, useState } from 'react';
import { Dispatch, SetStateAction, useState, useCallback, useMemo } from 'react';
import {
useDeleteAnimalBatchesMutation,
useDeleteAnimalsMutation,
Expand All @@ -26,15 +26,20 @@ import { CREATED_IN_ERROR_ID, FormFields } from '../../../components/Animals/Rem
import useMutations from '../../../hooks/api/useMutations';
import { enqueueErrorSnackbar, enqueueSuccessSnackbar } from '../../Snackbar/snackbarSlice';
import { AnimalOrBatchKeys } from '../types';
import { useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import { useTranslation } from 'react-i18next';
import { completedTasksSelector, abandonedTasksSelector } from '../../taskSlice';
import { Animal } from '../../../store/api/types';

const useAnimalOrBatchRemoval = (
selectedInventoryIds: string[],
animalTasksWithInventoryIds: { id: string; tasks: Animal['tasks'] }[],
setSelectedInventoryIds?: Dispatch<SetStateAction<string[]>>,
) => {
const dispatch = useDispatch();
const { t } = useTranslation(['message']);
const completedTasks = useSelector(completedTasksSelector) || [];
const abandonedTasks = useSelector(abandonedTasksSelector) || [];

const { mutations } = useMutations([
{ label: 'deleteAnimals', hook: useDeleteAnimalsMutation },
Expand Down Expand Up @@ -164,7 +169,35 @@ const useAnimalOrBatchRemoval = (
}
};

return { onConfirmRemoveAnimals, removalModalOpen, setRemovalModalOpen };
const getFinalizedTaskIdsSet = useCallback(() => {
return new Set([...completedTasks, ...abandonedTasks].map(({ task_id }) => task_id));
}, [completedTasks, abandonedTasks]);

const hasFinalizedTasks = useMemo(() => {
if (!removalModalOpen) {
return false;
}

const selectedInventoryIdsSet = new Set(selectedInventoryIds);
const finalizedTaskIdsSet = getFinalizedTaskIdsSet();

let inventoryFoundCount = 0;
for (let { id, tasks } of animalTasksWithInventoryIds) {
if (selectedInventoryIdsSet.has(id)) {
inventoryFoundCount++;

if (tasks.some(({ task_id }) => finalizedTaskIdsSet.has(task_id))) {
return true;
}
}
// Stop iterating once all selected inventory IDs have been processed.
if (inventoryFoundCount === selectedInventoryIdsSet.size) {
return false;
}
}
}, [removalModalOpen, completedTasks, abandonedTasks, selectedInventoryIds]);

return { onConfirmRemoveAnimals, removalModalOpen, setRemovalModalOpen, hasFinalizedTasks };
};

export default useAnimalOrBatchRemoval;
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,11 @@ function SingleAnimalView({ isCompactSideMenu, history, match, location }: AddAn
return generateInventoryId(animalOrBatch, (selectedAnimal || selectedBatch)!);
};

const { onConfirmRemoveAnimals, removalModalOpen, setRemovalModalOpen } = useAnimalOrBatchRemoval(
[getInventoryId()],
);
const { onConfirmRemoveAnimals, removalModalOpen, setRemovalModalOpen, hasFinalizedTasks } =
useAnimalOrBatchRemoval(
[getInventoryId()],
[{ ...(selectedAnimal || selectedBatch)!, id: getInventoryId() }],
);

const onConfirmRemoval = async (formData: FormFields) => {
const result = await onConfirmRemoveAnimals(formData);
Expand Down Expand Up @@ -241,6 +243,7 @@ function SingleAnimalView({ isCompactSideMenu, history, match, location }: AddAn
onClose={() => setRemovalModalOpen(false)}
onConfirm={onConfirmRemoval}
showSuccessMessage={false}
hideDeleteOption={hasFinalizedTasks}
/>
</FixedHeaderContainer>
</div>
Expand Down
2 changes: 2 additions & 0 deletions packages/webapp/src/store/api/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface Animal {
removal_explanation: string | null;
removal_date: string | null;
location_id: string | null;
tasks: { task_id: number }[];
type_name?: string; // request only
breed_name?: string; // request only
}
Expand Down Expand Up @@ -94,6 +95,7 @@ export interface AnimalBatch {
removal_explanation: string | null;
removal_date: string | null;
location_id: string | null;
tasks: { task_id: number }[];
type_name?: string; // request only
breed_name?: string; // request only
}
Expand Down
2 changes: 2 additions & 0 deletions packages/webapp/src/stories/Animals/mockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const mockAnimal1: Animal = {
internal_identifier: 12,
group_ids: [],
location_id: 'xxxxx',
tasks: [],
};

export const mockAnimal2: Animal = {
Expand Down Expand Up @@ -120,4 +121,5 @@ export const mockBatch1: AnimalBatch = {
{ sex_id: 2, count: 40 },
],
location_id: 'xxxxx',
tasks: [],
};

0 comments on commit ee6c16a

Please sign in to comment.