-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathRestartStandardGoalForm.js
More file actions
121 lines (112 loc) · 3.12 KB
/
Copy pathRestartStandardGoalForm.js
File metadata and controls
121 lines (112 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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,
};