This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathControl.jsx
81 lines (74 loc) · 2.47 KB
/
Control.jsx
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
import React from "react";
import { useContext, useEffect, useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { toast } from "react-toastify";
import AlertToast from "../atoms/AlertToast";
import { config } from "../config";
import RequestService from "../services/RequestService";
import ControlTemplate from "../templates/ControlTemplate";
import ErrorMessage from "../molecules/ErrorMessage";
import GlobalState from "../GlobalState";
import LoadingIndicator from "../atoms/LoadingIndicator";
const ERROR_MESSAGE = "Error loading project control";
export default function Control() {
const navigate = useNavigate();
const { id, controlId } = useParams();
const [, setState] = useContext(GlobalState);
const [pageData, setPageData] = useState();
const [hasError, setHasError] = useState(false);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
setIsLoading(true);
RequestService.get(
`${config.backendUrl}/projects/${id}/controls/${controlId}/`,
(response) => {
setState((state) => ({ ...state, project: response.data.project })); // TODO: This can be removed or updated accordingly when Breadcrumbs is refactored
setPageData(response.data);
setIsLoading(false);
},
(err) => {
setHasError(true);
setIsLoading(false);
}
);
}, [controlId, id, setState]);
function postControlUpdate(postVariables) {
const nextPageId = pageData.catalog_data.next_id;
RequestService.patch(
`${config.backendUrl}/projects/${id}/controls/${controlId}/`,
JSON.stringify(postVariables),
(response) => {
toast(
AlertToast(
"success",
`Control ${controlId.toUpperCase()} has been saved.`
)
);
const nextLink = `/projects/${id}/controls/${nextPageId}`;
navigate(nextLink);
},
(err) => {
toast(AlertToast("error", "Something went wrong, try again."));
}
);
}
if (isLoading) {
return <LoadingIndicator />;
}
if (hasError) {
return <ErrorMessage message={ERROR_MESSAGE} />;
}
if (pageData) {
let controlData = pageData.catalog_data;
controlData.id = pageData.control.id;
controlData.status = pageData.status;
return (
<ControlTemplate
project={pageData.project}
control={controlData}
componentData={pageData.component_data}
submitCallback={postControlUpdate}
/>
);
}
}