|
1 |
| -import React from 'react'; |
| 1 | +import React, { useEffect, useState } from 'react'; |
2 | 2 | import PropTypes from 'prop-types';
|
| 3 | +import { |
| 4 | + Form, Label, Fieldset, Textarea, Alert, Button, Accordion, |
| 5 | +} from '@trussworks/react-uswds'; |
| 6 | +import { useForm } from 'react-hook-form'; |
3 | 7 | import { Helmet } from 'react-helmet';
|
4 |
| -import { Button } from '@trussworks/react-uswds'; |
5 |
| - |
6 |
| -const ReviewSubmit = ({ allComplete, onSubmit }) => ( |
7 |
| - <> |
8 |
| - <Helmet> |
9 |
| - <title>Review and submit</title> |
10 |
| - </Helmet> |
11 |
| - <div> |
12 |
| - Review and submit |
13 |
| - <br /> |
14 |
| - <Button disabled={!allComplete} onClick={onSubmit}>Submit</Button> |
15 |
| - </div> |
16 |
| - </> |
17 |
| -); |
| 8 | + |
| 9 | +import { fetchApprovers } from '../../../fetchers/activityReports'; |
| 10 | +import MultiSelect from '../../../components/MultiSelect'; |
| 11 | +import Container from '../../../components/Container'; |
| 12 | + |
| 13 | +const defaultValues = { |
| 14 | + approvingManagers: null, |
| 15 | + additionalNotes: null, |
| 16 | +}; |
| 17 | + |
| 18 | +const ReviewSubmit = ({ |
| 19 | + initialData, allComplete, onSubmit, submitted, reviewItems, |
| 20 | +}) => { |
| 21 | + const [loading, updateLoading] = useState(true); |
| 22 | + const [possibleApprovers, updatePossibleApprovers] = useState([]); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + updateLoading(true); |
| 26 | + const fetch = async () => { |
| 27 | + const approvers = await fetchApprovers(); |
| 28 | + updatePossibleApprovers(approvers); |
| 29 | + updateLoading(false); |
| 30 | + }; |
| 31 | + fetch(); |
| 32 | + }, []); |
| 33 | + |
| 34 | + const { |
| 35 | + handleSubmit, register, formState, control, |
| 36 | + } = useForm({ |
| 37 | + mode: 'onChange', |
| 38 | + defaultValues: { ...defaultValues, ...initialData }, |
| 39 | + }); |
| 40 | + |
| 41 | + const onFormSubmit = (data) => { |
| 42 | + onSubmit(data); |
| 43 | + }; |
| 44 | + |
| 45 | + const { |
| 46 | + isValid, |
| 47 | + } = formState; |
| 48 | + |
| 49 | + const valid = allComplete && isValid; |
| 50 | + |
| 51 | + return ( |
| 52 | + <> |
| 53 | + <Helmet> |
| 54 | + <title>Review and submit</title> |
| 55 | + </Helmet> |
| 56 | + <Accordion bordered={false} items={reviewItems} /> |
| 57 | + <Container skipTopPadding className="margin-top-2 padding-top-2"> |
| 58 | + <h3>Submit Report</h3> |
| 59 | + {submitted |
| 60 | + && ( |
| 61 | + <Alert noIcon className="margin-y-4" type="success"> |
| 62 | + <b>Success</b> |
| 63 | + <br /> |
| 64 | + This report was successfully submitted for approval |
| 65 | + </Alert> |
| 66 | + )} |
| 67 | + {!allComplete |
| 68 | + && ( |
| 69 | + <Alert noIcon className="margin-y-4" type="error"> |
| 70 | + <b>Incomplete report</b> |
| 71 | + <br /> |
| 72 | + This report cannot be submitted until all sections are complete |
| 73 | + </Alert> |
| 74 | + )} |
| 75 | + <Form className="smart-hub--form-large" onSubmit={handleSubmit(onFormSubmit)}> |
| 76 | + <Fieldset className="smart-hub--report-legend smart-hub--form-section" legend="Additional Notes"> |
| 77 | + <Label htmlFor="additionalNotes">Additional notes for this activity (optional)</Label> |
| 78 | + <Textarea inputRef={register} id="additionalNotes" name="additionalNotes" /> |
| 79 | + </Fieldset> |
| 80 | + <Fieldset className="smart-hub--report-legend smart-hub--form-section" legend="Review and submit report"> |
| 81 | + <p className="margin-top-4"> |
| 82 | + Submitting this form for approval means that you will no longer be in draft mode. |
| 83 | + Please review all information in each section before submitting to your manager for |
| 84 | + approval. |
| 85 | + </p> |
| 86 | + <MultiSelect |
| 87 | + label="Manager - you may choose more than one." |
| 88 | + name="approvingManagers" |
| 89 | + options={possibleApprovers.map((user) => ({ |
| 90 | + label: user.name, |
| 91 | + value: user.id, |
| 92 | + }))} |
| 93 | + control={control} |
| 94 | + disabled={loading} |
| 95 | + /> |
| 96 | + </Fieldset> |
| 97 | + <Button type="submit" disabled={!valid}>Submit report for approval</Button> |
| 98 | + </Form> |
| 99 | + </Container> |
| 100 | + </> |
| 101 | + ); |
| 102 | +}; |
18 | 103 |
|
19 | 104 | ReviewSubmit.propTypes = {
|
| 105 | + initialData: PropTypes.shape({}), |
20 | 106 | allComplete: PropTypes.bool.isRequired,
|
21 | 107 | onSubmit: PropTypes.func.isRequired,
|
| 108 | + submitted: PropTypes.bool.isRequired, |
| 109 | + reviewItems: PropTypes.arrayOf( |
| 110 | + PropTypes.shape({ |
| 111 | + id: PropTypes.string.isRequired, |
| 112 | + title: PropTypes.string.isRequired, |
| 113 | + content: PropTypes.node.isRequired, |
| 114 | + }), |
| 115 | + ).isRequired, |
| 116 | +}; |
| 117 | + |
| 118 | +ReviewSubmit.defaultProps = { |
| 119 | + initialData: {}, |
22 | 120 | };
|
23 | 121 |
|
24 | 122 | export default ReviewSubmit;
|
0 commit comments