Skip to content

Commit

Permalink
Merge pull request #3564 from LiteFarmOrg/LF-4482-create-task-from-an…
Browse files Browse the repository at this point in the history
…imal-inventory

LF-4482 support creating task from animal inventory (nice to have)
  • Loading branch information
Duncan-Brain authored Dec 5, 2024
2 parents f7fa752 + b473dbd commit 137f8f7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export const PureTaskTypeSelection = ({
persistedFormData,
useHookFormPersist,
onContinue,
onError,
taskTypes,
customTasks,
isAdmin,
Expand All @@ -81,6 +80,7 @@ export const PureTaskTypeSelection = ({
const selected_task_type = watch(TASK_TYPE_ID);

const isMakingCropTask = !!location?.state?.management_plan_id;
const isMakingAnimalTask = !!location?.state?.animal_ids;

const onSelectTask = (task_type_id) => {
setValue(TASK_TYPE_ID, task_type_id);
Expand Down Expand Up @@ -108,6 +108,25 @@ export const PureTaskTypeSelection = ({
}
return onSelectTask(taskType.task_type_id);
};

const shouldDisplayTaskType = (taskType) => {
const supportedTaskTypes = getSupportedTaskTypesSet(isAdmin);
const { farm_id, task_translation_key } = taskType;

if (farm_id === null && supportedTaskTypes.has(task_translation_key)) {
// If trying to make a task through the crop management plan 'Add Task' link -- exclude animal tasks from selection for now
if (isMakingCropTask) {
return !ANIMAL_TASKS.includes(task_translation_key);
}
// If trying to make a task through the animal inventory 'Create a task' action -- only include animal tasks in selection
if (isMakingAnimalTask) {
return ANIMAL_TASKS.includes(task_translation_key);
}
return true;
}
return false;
};

return (
<>
<Form>
Expand All @@ -124,26 +143,14 @@ export const PureTaskTypeSelection = ({

<div style={{ paddingBottom: '20px' }} className={styles.matrixContainer}>
{taskTypes
?.filter(({ farm_id, task_translation_key }) => {
const supportedTaskTypes = getSupportedTaskTypesSet(isAdmin);
// If trying to make a task through the crop management plan 'Add Task' link -- exclude animal tasks from selection for now
const isNotAnimalTaskWhileCreatingCropTask = !(
ANIMAL_TASKS.includes(task_translation_key) && isMakingCropTask
);
const shouldDisplayTaskType =
farm_id === null &&
supportedTaskTypes.has(task_translation_key) &&
isNotAnimalTaskWhileCreatingCropTask;

return shouldDisplayTaskType;
})
?.filter(shouldDisplayTaskType)
.sort((firstTaskType, secondTaskType) =>
t(`task:${firstTaskType.task_translation_key}`).localeCompare(
t(`task:${secondTaskType.task_translation_key}`),
),
)
.map((taskType) => {
const { task_translation_key, task_type_id, farm_id } = taskType;
const { task_translation_key, task_type_id } = taskType;
return (
<div
data-cy="task-selection"
Expand All @@ -167,7 +174,7 @@ export const PureTaskTypeSelection = ({
?.sort((firstTaskType, secondTaskType) =>
firstTaskType.task_name.localeCompare(secondTaskType.task_name),
)
.map(({ task_translation_key, task_type_id, task_name }) => {
.map(({ task_type_id, task_name }) => {
return (
<div
onClick={() => {
Expand Down
14 changes: 4 additions & 10 deletions packages/webapp/src/components/Task/TaskAnimalInventory/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,14 @@ export default function PureTaskAnimalInventory({
}) {
const { t } = useTranslation();
const ANIMAL_IDS = 'animalIds';
const preSelectedIds = persistedFormData.animalIds || history.location?.state?.animal_ids;

const {
register,
handleSubmit,
getValues,
watch,
setValue,
formState: { isValid },
} = useForm({
const { register, handleSubmit, getValues, watch, setValue } = useForm({
mode: 'onChange',
shouldUnregister: false,
defaultValues: {
...persistedFormData,
[ANIMAL_IDS]: persistedFormData.animalIds || [],
[ANIMAL_IDS]: preSelectedIds || [],
},
});

Expand Down Expand Up @@ -107,7 +101,7 @@ export default function PureTaskAnimalInventory({
onSelect={onSelect}
view={View.TASK}
history={history}
preSelectedIds={persistedFormData?.animalIds}
preSelectedIds={preSelectedIds}
showLinks={false}
/>
</Form>
Expand Down
9 changes: 6 additions & 3 deletions packages/webapp/src/containers/Animals/Inventory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import clsx from 'clsx';
import AnimalsBetaSpotlight from './AnimalsBetaSpotlight';
import { sumObjectValues } from '../../../util';
import Icon from '../../../components/Icons';
import { onAddTask } from '../../Task/onAddTask';

const HEIGHTS = {
filterAndSearch: 64,
Expand Down Expand Up @@ -420,9 +421,11 @@ export default function AnimalInventory({
};

const iconActions: iconAction[] = [
{ label: t(`common:ADD_TO_GROUP`), iconName: 'ADD_ANIMAL', onClick: () => ({}) },
{ label: t(`common:CREATE_A_TASK`), iconName: 'TASK_CREATION', onClick: () => ({}) },
{ label: t(`common:CLONE`), iconName: 'CLONE', onClick: () => ({}) },
{
label: t(`common:CREATE_A_TASK`),
iconName: 'TASK_CREATION',
onClick: () => onAddTask(dispatch, history, { animal_ids: selectedInventoryIds })(),
},
{
label: t(`ANIMAL.REMOVE_ANIMAL`),
iconName: 'REMOVE_ANIMAL',
Expand Down

0 comments on commit 137f8f7

Please sign in to comment.