Skip to content

Commit

Permalink
Merge pull request #1169 from CruGlobal/8385-preserve-previous-task-name
Browse files Browse the repository at this point in the history
[MDPX-8385] Preserve previous task name
  • Loading branch information
canac authored Nov 4, 2024
2 parents 975d3ae + 8b8c90e commit 9c904af
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ describe('TaskModalCompleteForm', () => {
expect(openTaskModal).toHaveBeenCalledWith({
view: TaskModalEnum.Add,
defaultValues: {
subject: task.subject,
activityType: ActivityTypeEnum.PartnerCareThank,
contactIds: ['contact-1', 'contact-2'],
userId: 'user-1',
Expand Down Expand Up @@ -411,6 +412,7 @@ describe('TaskModalCompleteForm', () => {
expect(openTaskModal).toHaveBeenCalledWith({
view: 'add',
defaultValues: {
subject: task.subject,
activityType: ActivityTypeEnum.PartnerCareThank,
contactIds: ['contact-1'],
userId: 'user-1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { useUpdateContactStatusMutation } from '../TaskModal.generated';
import {
extractSuggestedTags,
getDatabaseValueFromResult,
getDefaultTaskName,
} from '../TaskModalHelper';
import { possibleNextActions } from '../possibleNextActions';
import { possiblePartnerStatus } from '../possiblePartnerStatus';
Expand Down Expand Up @@ -219,9 +220,17 @@ const TaskModalCompleteForm = ({
}
onClose();
if (attributes.nextAction) {
const defaultSubject = getDefaultTaskName(
task.activityType ?? null,
activityTypes,
);

openTaskModal({
view: TaskModalEnum.Add,
defaultValues: {
// If the user changed the task subject from the default, use their customized task
// subject in the new task
subject: task.subject === defaultSubject ? undefined : task.subject,
activityType: attributes.nextAction,
// TODO: Use fragments to ensure all required fields are loaded
contactIds: task.contacts.nodes.map((contact) => contact.id),
Expand Down
11 changes: 1 addition & 10 deletions src/components/Task/Modal/Form/LogForm/TaskModalLogForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { SnackbarProvider } from 'notistack';
import TestRouter from '__tests__/util/TestRouter';
import { GqlMockedProvider } from '__tests__/util/graphqlMocking';
import { AssigneeOptionsQuery } from 'src/components/Contacts/ContactDetails/ContactDetailsTab/Other/EditContactOtherModal/EditContactOther.generated';
import { GetUserQuery } from 'src/components/User/GetUser.generated';
import { ActivityTypeEnum } from 'src/graphql/types.generated';
import useTaskModal from 'src/hooks/useTaskModal';
import { dispatch } from 'src/lib/analytics';
Expand Down Expand Up @@ -283,10 +282,7 @@ describe('TaskModalLogForm', () => {
const { findByRole, getByRole } = render(
<LocalizationProvider dateAdapter={AdapterLuxon}>
<SnackbarProvider>
<GqlMockedProvider<{
AssigneeOptions: AssigneeOptionsQuery;
GetUser: GetUserQuery;
}>
<GqlMockedProvider<{ AssigneeOptions: AssigneeOptionsQuery }>
mocks={{
AssigneeOptions: {
accountListUsers: {
Expand All @@ -297,11 +293,6 @@ describe('TaskModalLogForm', () => {
],
},
},
GetUser: {
user: {
id: 'user-1',
},
},
}}
onCall={mutationSpy}
>
Expand Down
17 changes: 14 additions & 3 deletions src/components/Task/Modal/Form/LogForm/TaskModalLogForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
import {
extractSuggestedTags,
getDatabaseValueFromResult,
getDefaultTaskName,
handleTaskActionChange,
handleTaskPhaseChange,
} from '../TaskModalHelper';
Expand Down Expand Up @@ -121,7 +122,6 @@ const TaskModalLogForm = ({
const {
phaseData,
setPhaseId,
constants,
taskPhases,
activityTypes,
activitiesByPhase,
Expand Down Expand Up @@ -246,9 +246,20 @@ const TaskModalLogForm = ({
enqueueSnackbar(t('Task(s) logged successfully'), { variant: 'success' });
onClose();
if (attributes.nextAction) {
const defaultSubject = getDefaultTaskName(
attributes.activityType,
activityTypes,
);

openTaskModal({
view: TaskModalEnum.Add,
defaultValues: {
// If the user changed the task subject from the default, use their customized task
// subject in the new task
subject:
attributes.subject === defaultSubject
? undefined
: attributes.subject,
activityType: attributes.nextAction,
// TODO: Use fragments to ensure all required fields are loaded
contactIds: attributes.contactIds,
Expand Down Expand Up @@ -365,7 +376,7 @@ const TaskModalLogForm = ({
activities,
focusActivity,
activityType,
constants,
activityTypes,
setFieldTouched,
});
}}
Expand All @@ -388,7 +399,7 @@ const TaskModalLogForm = ({
setFieldValue,
setFieldTouched,
setActionSelected,
constants,
activityTypes,
});
}}
inputRef={activityRef}
Expand Down
68 changes: 51 additions & 17 deletions src/components/Task/Modal/Form/TaskModalForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { render, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { DateTime, Settings } from 'luxon';
import { DateTime } from 'luxon';
import { SnackbarProvider } from 'notistack';
import { GqlMockedProvider } from '__tests__/util/graphqlMocking';
import LoadConstantsMock from 'src/components/Constants/LoadConstantsMock';
import { AssigneeOptionsQuery } from 'src/components/Contacts/ContactDetails/ContactDetailsTab/Other/EditContactOtherModal/EditContactOther.generated';
import { GetUserQuery } from 'src/components/User/GetUser.generated';
import { ActivityTypeEnum, PhaseEnum } from 'src/graphql/types.generated';
import useTaskModal from 'src/hooks/useTaskModal';
import theme from 'src/theme';
Expand Down Expand Up @@ -91,12 +90,6 @@ describe('TaskModalForm', () => {
},
};

beforeEach(() => {
// Create a stable time so that the "now" in the component will match "now" in the mocks
const now = Date.now();
Settings.now = () => now;
});

it('Modal should close', async () => {
const { getByText } = render(<Components />);
userEvent.click(getByText('Cancel'));
Expand Down Expand Up @@ -343,10 +336,7 @@ describe('TaskModalForm', () => {
const { getByRole } = render(
<LocalizationProvider dateAdapter={AdapterLuxon}>
<SnackbarProvider>
<GqlMockedProvider<{
AssigneeOptions: AssigneeOptionsQuery;
GetUser: GetUserQuery;
}>
<GqlMockedProvider<{ AssigneeOptions: AssigneeOptionsQuery }>
mocks={{
AssigneeOptions: {
accountListUsers: {
Expand All @@ -357,11 +347,6 @@ describe('TaskModalForm', () => {
],
},
},
GetUser: {
user: {
id: 'user-1',
},
},
}}
onCall={mutationSpy}
>
Expand All @@ -380,6 +365,55 @@ describe('TaskModalForm', () => {
);
});

it('defaults the subject to the defaultValues subject', () => {
const { getByRole } = render(
<LocalizationProvider dateAdapter={AdapterLuxon}>
<SnackbarProvider>
<GqlMockedProvider>
<TaskModalForm
defaultValues={{
taskPhase: PhaseEnum.PartnerCare,
activityType: ActivityTypeEnum.PartnerCareTextMessage,
subject: 'Do something',
}}
accountListId={accountListId}
onClose={onClose}
task={null}
/>
</GqlMockedProvider>
</SnackbarProvider>
</LocalizationProvider>,
);

expect(getByRole('textbox', { name: 'Subject' })).toHaveValue(
'Do something',
);
});

it('defaults the subject to the name based on phase and action', () => {
const { getByRole } = render(
<LocalizationProvider dateAdapter={AdapterLuxon}>
<SnackbarProvider>
<GqlMockedProvider>
<TaskModalForm
defaultValues={{
taskPhase: PhaseEnum.PartnerCare,
activityType: ActivityTypeEnum.PartnerCareTextMessage,
}}
accountListId={accountListId}
onClose={onClose}
task={null}
/>
</GqlMockedProvider>
</SnackbarProvider>
</LocalizationProvider>,
);

expect(getByRole('textbox', { name: 'Subject' })).toHaveValue(
'Text Message Partner For Cultivation',
);
});

it('renders fields for completed task', async () => {
const { getByRole, findByRole, queryByText } = render(
<ThemeProvider theme={theme}>
Expand Down
40 changes: 17 additions & 23 deletions src/components/Task/Modal/Form/TaskModalForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import {
ExtractSuggestedTags,
extractSuggestedTags,
getDatabaseValueFromResult,
getDefaultTaskName,
handleTaskActionChange,
handleTaskPhaseChange,
} from './TaskModalHelper';
Expand All @@ -89,32 +90,26 @@ const getTaskDetails = (
activityTypes: Map<ActivityTypeEnum, ActivityData>,
phaseTags: string[],
) => {
let taskPhase: PhaseEnum | null;
let taskSubject: string | undefined;
let filteredTags: ExtractSuggestedTags | undefined;
let additionalTags: ExtractSuggestedTags['additionalTags'] | undefined;

const activityType =
(task ? task.activityType : defaultValues?.activityType) ?? null;
const initialTaskPhase = task ? task.taskPhase : defaultValues?.taskPhase;
// If the task/defaultValues has no phase specified, calculate the phase from the activity type,
// if present
const activityTypePhase =
activityType && activityTypes.get(activityType)?.phaseId;
const taskPhase = initialTaskPhase ?? activityTypePhase ?? null;

if (task) {
taskPhase = task?.taskPhase ?? null;
if (!taskPhase) {
taskPhase = task?.activityType
? activityTypes.get(task.activityType)?.phaseId || null
: null;
}
//go through tags and move some to selectedSuggestedTags and others to additionalTags
// go through tags and move some to selectedSuggestedTags and others to additionalTags
filteredTags = extractSuggestedTags(task.tagList, phaseTags);
additionalTags = filteredTags?.additionalTags;
} else {
taskPhase = defaultValues?.taskPhase || null;
taskSubject = defaultValues?.subject;
if (defaultValues?.activityType && activityTypes) {
const activityData = defaultValues.activityType
? activityTypes.get(defaultValues.activityType)
: undefined;
if (activityData) {
taskPhase = activityData.phaseId;
taskSubject = activityData.title;
}
}
taskSubject =
defaultValues?.subject ?? getDefaultTaskName(activityType, activityTypes);
}
return {
taskPhase,
Expand Down Expand Up @@ -184,7 +179,6 @@ const TaskModalForm = ({
const {
phaseData,
setPhaseId,
constants,
taskPhases,
activityTypes,
activitiesByPhase,
Expand Down Expand Up @@ -254,7 +248,7 @@ const TaskModalForm = ({
taskPhase: taskPhase as PhaseEnum,
activityType: (defaultValues?.activityType ?? null) as ActivityTypeEnum,
location: '',
subject: taskSubject ?? '',
subject: defaultValues?.subject ?? taskSubject ?? '',
startAt: DateTime.local(),
completedAt: null,
displayResult: null,
Expand Down Expand Up @@ -466,7 +460,7 @@ const TaskModalForm = ({
activities,
focusActivity,
activityType,
constants,
activityTypes,
setFieldTouched,
});
}}
Expand All @@ -492,7 +486,7 @@ const TaskModalForm = ({
setFieldValue,
setFieldTouched,
setActionSelected,
constants,
activityTypes,
});
}}
inputRef={activityRef}
Expand Down
Loading

0 comments on commit 9c904af

Please sign in to comment.