-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataEntryReview.tsx
200 lines (186 loc) · 6.81 KB
/
DataEntryReview.tsx
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Recidiviz - a data platform for criminal justice reform
// Copyright (C) 2024 Recidiviz, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =============================================================================
import { Modal } from "@justice-counts/common/components/Modal";
import { showToast } from "@justice-counts/common/components/Toast";
import { observer } from "mobx-react-lite";
import React, { useEffect, useState } from "react";
import { useLocation, useNavigate, useParams } from "react-router-dom";
import { trackReportPublished } from "../../analytics";
import { NotFound } from "../../pages/NotFound";
import { useStore } from "../../stores";
import { printReportTitle } from "../../utils";
import { REPORT_LOWERCASE, REPORTS_LOWERCASE } from "../Global/constants";
import { Loading } from "../Loading";
import {
createPublishSuccessModalButtons,
PublishReviewPropsFromDatapoints,
ReviewHeaderActionButton,
ReviewMetric,
ReviewMetrics,
} from "../ReviewMetrics";
import { VIEW_PUBLISHED_DATA_DESCRIPTION } from "./constants";
import { useCheckMetricForErrors } from "./hooks";
import { ReviewWrapper } from "./ReportDataEntry.styles";
const DataEntryReview = () => {
const params = useParams();
const reportID = Number(params.id);
const agencyId = Number(params.agencyId);
const navigate = useNavigate();
const { state } = useLocation();
const { metricDisplayNames } =
(state as { metricDisplayNames: string[] }) || {};
const { reportStore, formStore, userStore } = useStore();
const checkMetricForErrors = useCheckMetricForErrors(reportID);
const [isSuccessModalOpen, setIsSuccessModalOpen] = useState(false);
const [isPublishInProgress, setIsPublishInProgress] = useState(false);
const [loadingDatapoints, setLoadingDatapoints] = useState(true);
const [publishReviewProps, setPublishReviewProps] =
useState<PublishReviewPropsFromDatapoints>();
const datapointsByMetric = publishReviewProps?.datapointsByMetric;
const metricsToDisplay = publishReviewProps?.metricsToDisplay;
const hasPublishReviewProps = metricsToDisplay && datapointsByMetric;
const publishReport = async () => {
const finalMetricsToPublish =
formStore.reportUpdatedValuesForBackend(reportID);
setIsPublishInProgress(true);
const response = (await reportStore.updateReport(
reportID,
finalMetricsToPublish,
"PUBLISHED"
)) as Response;
if (response.status === 200) {
setIsSuccessModalOpen(true);
setIsPublishInProgress(false);
const agencyID = reportStore.reportOverviews[reportID]?.agency_id;
const agency = userStore.userAgenciesById[agencyID];
trackReportPublished(reportID, finalMetricsToPublish, agency);
} else {
showToast({
message: `Something went wrong publishing the ${printReportTitle(
reportStore.reportOverviews[reportID].month,
reportStore.reportOverviews[reportID].year,
reportStore.reportOverviews[reportID].frequency
)} ${REPORT_LOWERCASE}!`,
});
}
};
useEffect(() => {
const initialize = async () => {
const reviewProps = await reportStore.getPublishReviewPropsFromDatapoints(
[reportID],
String(agencyId)
);
if (reviewProps instanceof Error) {
return setLoadingDatapoints(false);
}
if (reviewProps) setPublishReviewProps(reviewProps);
setLoadingDatapoints(false);
};
// When a user refreshes in the review page, navigate them back to the data entry form
if (!reportStore.reportMetrics[reportID]) {
return navigate(`../records/${reportID}`);
}
initialize();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
if (
reportStore.reportOverviews[reportID] &&
reportStore.reportOverviews[reportID].agency_id !== agencyId
) {
return <NotFound />;
}
// review component props
const metrics = hasPublishReviewProps
? metricsToDisplay
.reduce((acc, metric) => {
const reviewMetric = {
datapoints: datapointsByMetric[metric.key],
display_name: metric.displayName,
key: metric.key,
metricHasError: checkMetricForErrors(metric.key),
metricHasValidInput: Boolean(
formStore.metricsValues?.[reportID]?.[metric.key]?.value
),
};
return [...acc, reviewMetric];
}, [] as ReviewMetric[])
// Sort this list of metrics so it matches the order of the metrics list on the data entry page
.sort(
(a, b) =>
metricDisplayNames &&
metricDisplayNames.indexOf(a.display_name) -
metricDisplayNames.indexOf(b.display_name)
)
: [];
const record = reportStore.reportOverviews[reportID];
const title = "Review & Publish";
const description = (
<>
Here’s a breakdown of data from the {REPORT_LOWERCASE}. Take a moment to
review these changes, then publish when ready. If you believe there is an
error, please contact the Justice Counts team via{" "}
<a href="mailto:[email protected]">
</a>
.
</>
);
const buttons: ReviewHeaderActionButton[] = [
{
name: "Back to data entry",
onClick: () => navigate(`../records/${reportID}`),
borderColor: "lightgrey",
},
{
name: "Exit without Publishing",
onClick: () => navigate(`/agency/${agencyId}/${REPORTS_LOWERCASE}`),
borderColor: "lightgrey",
},
{
name: "Publish",
onClick: publishReport,
buttonColor: "green",
isPublishButton: true,
isPublishInProgress,
},
];
return (
<ReviewWrapper>
{isSuccessModalOpen && (
<Modal
title="Data published!"
description={VIEW_PUBLISHED_DATA_DESCRIPTION}
buttons={createPublishSuccessModalButtons(agencyId, navigate)}
modalType="success"
centerText
/>
)}
{loadingDatapoints ? (
<Loading />
) : (
<ReviewMetrics
title={title}
description={description}
buttons={buttons}
metrics={metrics}
records={[record]}
/>
)}
</ReviewWrapper>
);
};
export default observer(DataEntryReview);