-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReportDataEntry.tsx
158 lines (144 loc) · 5.4 KB
/
ReportDataEntry.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
// 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 { showToast } from "@justice-counts/common/components/Toast";
import { Report } from "@justice-counts/common/types";
import { observer } from "mobx-react-lite";
import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { trackReportUnpublished } from "../../analytics";
import { NotFound } from "../../pages/NotFound";
import { useStore } from "../../stores";
import { printReportTitle } from "../../utils";
import { PageWrapper } from "../Forms";
import { REPORT_LOWERCASE } from "../Global/constants";
import { Loading } from "../Loading";
import DataEntryForm from "./DataEntryForm";
import PublishDataPanel from "./PublishDataPanel";
import {
FieldDescriptionProps,
ReportDataEntryWrapper,
} from "./ReportDataEntry.styles";
import ReportSummaryPanel from "./ReportSummaryPanel";
const ReportDataEntry = () => {
const [loadingError, setLoadingError] = useState<string | undefined>(
undefined
);
const [activeMetric, setActiveMetric] = useState<string>("");
const [fieldDescription, setFieldDescription] = useState<
FieldDescriptionProps | undefined
>(undefined);
const { formStore, reportStore, userStore } = useStore();
const { agencyId, id } = useParams();
const reportID = Number(id);
const reportOverview = reportStore.reportOverviews[reportID] as Report;
const reportMetrics = reportStore.reportMetrics[reportID];
const convertReportToDraft = async () => {
if (reportOverview.status === "PUBLISHED") {
const response = (await reportStore.updateReport(
reportID,
[],
"DRAFT"
)) as Response;
if (response.status === 200) {
showToast({
message: `The ${printReportTitle(
reportStore.reportOverviews[reportID].month,
reportStore.reportOverviews[reportID].year,
reportStore.reportOverviews[reportID].frequency
)} ${REPORT_LOWERCASE} has been unpublished and editing is enabled.`,
check: true,
timeout: 4000,
});
const agencyID = reportStore.reportOverviews[reportID]?.agency_id;
const agency = userStore.userAgenciesById[agencyID];
trackReportUnpublished(reportID, agency);
} else {
showToast({
message: `Something went wrong unpublishing the ${printReportTitle(
reportStore.reportOverviews[reportID].month,
reportStore.reportOverviews[reportID].year,
reportStore.reportOverviews[reportID].frequency
)} ${REPORT_LOWERCASE}!`,
});
}
}
};
useEffect(() => {
const initialize = async () => {
const result = await reportStore.getReport(reportID);
if (result instanceof Error) {
setLoadingError(result.message);
}
};
initialize();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
/** Runs validation of previously saved inputs on load */
if (reportMetrics) {
formStore.validatePreviouslySavedInputs(reportID);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [reportMetrics]);
const updateActiveMetric = (metricKey: string) => setActiveMetric(metricKey);
const updateFieldDescription = (title?: string, description?: string) => {
if (title && description) {
setFieldDescription({ title, description });
} else {
setFieldDescription(undefined);
}
};
useEffect(() => {
const firstEnabledMetric = reportMetrics?.find((metric) => metric.enabled);
if (reportMetrics && firstEnabledMetric)
updateActiveMetric(firstEnabledMetric.key); // open to the first enabled metric by default
}, [reportMetrics, reportID]);
if (reportStore.loadingReportData) {
return (
<PageWrapper>
<Loading />
</PageWrapper>
);
}
if (loadingError || !reportStore.reportOverviews[reportID]) {
return <PageWrapper>Error: {loadingError}</PageWrapper>;
}
if (reportStore.reportOverviews[reportID].agency_id !== Number(agencyId)) {
return <NotFound />;
}
return (
<ReportDataEntryWrapper>
<ReportSummaryPanel
reportID={reportID}
activeMetric={activeMetric}
fieldDescription={fieldDescription}
/>
<DataEntryForm
reportID={reportID}
updateActiveMetric={updateActiveMetric}
updateFieldDescription={updateFieldDescription}
convertReportToDraft={convertReportToDraft}
/>
<PublishDataPanel
reportID={reportID}
activeMetric={activeMetric}
fieldDescription={fieldDescription}
/>
</ReportDataEntryWrapper>
);
};
export default observer(ReportDataEntry);