Skip to content
Merged
84 changes: 10 additions & 74 deletions frontend/src/pages/StandardGoalForm/RestartStandardGoal.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import { GOAL_STATUS } from '@ttahub/common/src/constants';
import { uniqueId } from 'lodash';
import PropTypes from 'prop-types';
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
import { useForm } from 'react-hook-form';
import React, { useContext, useEffect, useRef, useState } from 'react';
import { useHistory, useLocation, useParams } from 'react-router';
import AppLoadingContext from '../../AppLoadingContext';
import { ROUTES } from '../../Constants';
import {
GOAL_FORM_BUTTON_LABELS,
GOAL_FORM_BUTTON_TYPES,
GOAL_FORM_BUTTON_VARIANTS,
} from '../../components/SharedGoalComponents/constants';
import GoalFormUpdateOrRestart from '../../components/SharedGoalComponents/GoalFormUpdateOrRestart';
import { HTTPError } from '../../fetchers';
import { addStandardGoal, getStandardGoal } from '../../fetchers/standardGoals';
import { getStandardGoal } from '../../fetchers/standardGoals';
import useGoalTemplatePrompts from '../../hooks/useGoalTemplatePrompts';
import { GOAL_FORM_FIELDS, mapObjectivesAndRootCauses } from './constants';
import RestartStandardGoalForm from './RestartStandardGoalForm';

export default function RestartStandardGoal({ recipient }) {
const { goalTemplateId, regionId, grantId } = useParams();
Expand All @@ -29,13 +21,6 @@ export default function RestartStandardGoal({ recipient }) {
const [goal, setGoal] = useState(null);
const fetchAttempted = useRef(false);

const hookForm = useForm({
defaultValues: {
[GOAL_FORM_FIELDS.OBJECTIVES]: [],
[GOAL_FORM_FIELDS.ROOT_CAUSES]: null,
},
});

const [goalTemplatePrompts] = useGoalTemplatePrompts(goalTemplateId);

useEffect(() => {
Expand All @@ -49,14 +34,6 @@ export default function RestartStandardGoal({ recipient }) {
throw new HTTPError('Goal not found', 404);
}
setGoal(g);

// We want the user to start fresh with objectives and root causes.
const resetFormData = {
// eslint-disable-next-line max-len
[GOAL_FORM_FIELDS.OBJECTIVES]: [],
};

hookForm.reset(resetFormData);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
Expand All @@ -78,62 +55,21 @@ export default function RestartStandardGoal({ recipient }) {
fetchAttempted.current = true;
fetchStandardGoal();
}
}, [goal, goalTemplateId, goalTemplatePrompts, grantId, history, hookForm, setIsAppLoading]);

const standardGoalFormButtons = useMemo(
() => [
{
id: uniqueId('goal-form-button-'),
type: GOAL_FORM_BUTTON_TYPES.SUBMIT,
variant: GOAL_FORM_BUTTON_VARIANTS.PRIMARY,
label: GOAL_FORM_BUTTON_LABELS.RESTART,
},
{
id: uniqueId('goal-form-button-'),
type: GOAL_FORM_BUTTON_TYPES.LINK,
variant: GOAL_FORM_BUTTON_VARIANTS.OUTLINE,
label: GOAL_FORM_BUTTON_LABELS.CANCEL,
to: backLinkTo,
},
],
[backLinkTo]
);

const onSubmit = async (data) => {
try {
setIsAppLoading(true);

// submit to backend
await addStandardGoal({
goalTemplateId,
grantId,
status: GOAL_STATUS.IN_PROGRESS,
...mapObjectivesAndRootCauses(data),
});

history.push(backLinkTo);
} catch (err) {
// eslint-disable-next-line no-console
console.log(err);
} finally {
setIsAppLoading(false);
}
};
}, [goal, goalTemplateId, goalTemplatePrompts, grantId, history, setIsAppLoading]);

if (!goal) {
return null;
}

return (
<GoalFormUpdateOrRestart
hookForm={hookForm}
onSubmit={onSubmit}
recipient={recipient}
regionId={regionId}
<RestartStandardGoalForm
goal={goal}
goalTemplatePrompts={goalTemplatePrompts}
standardGoalFormButtons={standardGoalFormButtons}
isRestart
recipient={recipient}
regionId={regionId}
goalTemplateId={goalTemplateId}
grantId={grantId}
backLinkTo={backLinkTo}
/>
);
}
Expand Down
121 changes: 121 additions & 0 deletions frontend/src/pages/StandardGoalForm/RestartStandardGoalForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { GOAL_STATUS } from '@ttahub/common/src/constants';
import { uniqueId } from 'lodash';
import PropTypes from 'prop-types';
import React, { useContext, useMemo } from 'react';
import { useForm } from 'react-hook-form';
import { useHistory } from 'react-router';
import AppLoadingContext from '../../AppLoadingContext';
import {
GOAL_FORM_BUTTON_LABELS,
GOAL_FORM_BUTTON_TYPES,
GOAL_FORM_BUTTON_VARIANTS,
} from '../../components/SharedGoalComponents/constants';
import GoalFormUpdateOrRestart from '../../components/SharedGoalComponents/GoalFormUpdateOrRestart';
import { addStandardGoal } from '../../fetchers/standardGoals';
import { GOAL_FORM_FIELDS, mapObjectivesAndRootCauses } from './constants';

export default function RestartStandardGoalForm({
goal,
goalTemplatePrompts,
recipient,
regionId,
goalTemplateId,
grantId,
backLinkTo,
}) {
const history = useHistory();
const { setIsAppLoading } = useContext(AppLoadingContext);

const hookForm = useForm({
defaultValues: {
[GOAL_FORM_FIELDS.OBJECTIVES]: [],
[GOAL_FORM_FIELDS.ROOT_CAUSES]: null,
},
});

const standardGoalFormButtons = useMemo(
() => [
{
id: uniqueId('goal-form-button-'),
type: GOAL_FORM_BUTTON_TYPES.SUBMIT,
variant: GOAL_FORM_BUTTON_VARIANTS.PRIMARY,
label: GOAL_FORM_BUTTON_LABELS.RESTART,
},
{
id: uniqueId('goal-form-button-'),
type: GOAL_FORM_BUTTON_TYPES.LINK,
variant: GOAL_FORM_BUTTON_VARIANTS.OUTLINE,
label: GOAL_FORM_BUTTON_LABELS.CANCEL,
to: backLinkTo,
},
],
[backLinkTo]
);

const onSubmit = async (data) => {
try {
setIsAppLoading(true);

await addStandardGoal({
goalTemplateId,
grantId,
status: GOAL_STATUS.IN_PROGRESS,
...mapObjectivesAndRootCauses(data),
});

history.push(backLinkTo);
} catch (err) {
// eslint-disable-next-line no-console
console.log(err);
} finally {
setIsAppLoading(false);
}
};

return (
<GoalFormUpdateOrRestart
hookForm={hookForm}
onSubmit={onSubmit}
recipient={recipient}
regionId={regionId}
goal={goal}
goalTemplatePrompts={goalTemplatePrompts}
standardGoalFormButtons={standardGoalFormButtons}
isRestart
/>
);
}

RestartStandardGoalForm.propTypes = {
goal: PropTypes.shape({
id: PropTypes.number,
name: PropTypes.string,
grant: PropTypes.shape({
numberWithProgramTypes: PropTypes.string,
}),
objectives: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number,
title: PropTypes.string,
})
),
}).isRequired,
goalTemplatePrompts: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number,
prompt: PropTypes.string,
})
),
recipient: PropTypes.shape({
id: PropTypes.number,
name: PropTypes.string,
}).isRequired,
regionId: PropTypes.string.isRequired,
goalTemplateId: PropTypes.string.isRequired,
grantId: PropTypes.string.isRequired,
backLinkTo: PropTypes.string.isRequired,
};

RestartStandardGoalForm.defaultProps = {
goalTemplatePrompts: null,
};
94 changes: 11 additions & 83 deletions frontend/src/pages/StandardGoalForm/UpdateStandardGoal.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { uniqueId } from 'lodash';
import PropTypes from 'prop-types';
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
import { useForm } from 'react-hook-form';
import React, { useContext, useEffect, useRef, useState } from 'react';
import { useHistory, useLocation, useParams } from 'react-router';
import AppLoadingContext from '../../AppLoadingContext';
import { ROUTES } from '../../Constants';
import {
GOAL_FORM_BUTTON_LABELS,
GOAL_FORM_BUTTON_TYPES,
GOAL_FORM_BUTTON_VARIANTS,
} from '../../components/SharedGoalComponents/constants';
import GoalFormUpdateOrRestart from '../../components/SharedGoalComponents/GoalFormUpdateOrRestart';
import { HTTPError } from '../../fetchers';
import { getStandardGoal, updateStandardGoal } from '../../fetchers/standardGoals';
import { getStandardGoal } from '../../fetchers/standardGoals';
import useGoalTemplatePrompts from '../../hooks/useGoalTemplatePrompts';
import { GOAL_FORM_FIELDS } from './constants';
import UpdateStandardGoalForm from './UpdateStandardGoalForm';

export default function UpdateStandardGoal({ recipient }) {
const { goalTemplateId, regionId, grantId } = useParams();
Expand All @@ -28,41 +20,20 @@ export default function UpdateStandardGoal({ recipient }) {
const [goal, setGoal] = useState(null);
const fetchAttempted = useRef(false);

const hookForm = useForm({
defaultValues: {
[GOAL_FORM_FIELDS.OBJECTIVES]: [],
[GOAL_FORM_FIELDS.ROOT_CAUSES]: null,
},
});

const [goalTemplatePrompts] = useGoalTemplatePrompts(goalTemplateId);

useEffect(() => {
const fetchStandardGoal = async () => {
try {
setIsAppLoading(true);

// we need to get closed only if we are restarting the goal
const g = await getStandardGoal(goalTemplateId, grantId);

setGoal(g);
if (!g) {
throw new HTTPError('Goal not found', 404);
}

const resetFormData = {
// eslint-disable-next-line max-len
[GOAL_FORM_FIELDS.OBJECTIVES]: g.objectives.map((o) => ({
value: o.title,
objectiveId: o.id,
onAR: o.onAR,
status: o.status,
})),
[GOAL_FORM_FIELDS.ROOT_CAUSES]: g.responses.flatMap((responses) =>
responses.response.map((r) => ({ id: r, name: r }))
),
};
hookForm.reset(resetFormData);
setGoal(g);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
Expand All @@ -84,64 +55,21 @@ export default function UpdateStandardGoal({ recipient }) {
fetchAttempted.current = true;
fetchStandardGoal();
}
}, [goal, goalTemplateId, goalTemplatePrompts, grantId, history, hookForm, setIsAppLoading]);

const standardGoalFormButtons = useMemo(
() => [
{
id: uniqueId('goal-form-button-'),
type: GOAL_FORM_BUTTON_TYPES.SUBMIT,
variant: GOAL_FORM_BUTTON_VARIANTS.PRIMARY,
label: GOAL_FORM_BUTTON_LABELS.SAVE,
},
{
id: uniqueId('goal-form-button-'),
type: GOAL_FORM_BUTTON_TYPES.LINK,
variant: GOAL_FORM_BUTTON_VARIANTS.OUTLINE,
label: GOAL_FORM_BUTTON_LABELS.CANCEL,
to: backLinkTo,
},
],
[backLinkTo]
);

const onSubmit = async (data) => {
try {
setIsAppLoading(true);

// submit to backend
await updateStandardGoal({
goalTemplateId,
grantId,
// eslint-disable-next-line max-len
objectives: data.objectives
? data.objectives.map((o) => ({ title: o.value, id: o.objectiveId }))
: [],
rootCauses: data.rootCauses ? data.rootCauses.map((r) => r.id) : null,
});

history.push(backLinkTo);
} catch (err) {
// eslint-disable-next-line no-console
console.log(err);
} finally {
setIsAppLoading(false);
}
};
}, [goal, goalTemplateId, goalTemplatePrompts, grantId, history, setIsAppLoading]);

if (!goal) {
return null;
}

return (
<GoalFormUpdateOrRestart
hookForm={hookForm}
onSubmit={onSubmit}
recipient={recipient}
regionId={regionId}
<UpdateStandardGoalForm
goal={goal}
goalTemplatePrompts={goalTemplatePrompts}
standardGoalFormButtons={standardGoalFormButtons}
recipient={recipient}
regionId={regionId}
goalTemplateId={goalTemplateId}
grantId={grantId}
backLinkTo={backLinkTo}
/>
);
}
Expand Down
Loading
Loading