Debian Codegraph @

jsx/src/components/GroupEdit/GroupEdit.jsx

raw
1 import React, { useEffect, useState } from "react";
2 import { useSelector, useDispatch } from "react-redux";
3 import { Link, useNavigate, useLocation } from "react-router-dom";
4 import PropTypes from "prop-types";
5 import { Button, Card } from "react-bootstrap";
6 import GroupSelect from "../GroupSelect/GroupSelect";
7 import DynamicTable from "../DynamicTable/DynamicTable";
8 import { MainContainer } from "../../util/layout";
9
10 const GroupEdit = (props) => {
11 const [selected, setSelected] = useState([]),
12 [changed, setChanged] = useState(false),
13 [errorAlert, setErrorAlert] = useState(null),
14 navigate = useNavigate(),
15 location = useLocation(),
16 limit = useSelector((state) => state.limit);
17
18 const dispatch = useDispatch();
19 const hasDuplicates = (a) => a.filter((e, i) => a.indexOf(e) != i).length > 0;
20 const dispatchPageUpdate = (data, page) => {
21 dispatch({
22 type: "GROUPS_PAGE",
23 value: {
24 data: data,
25 page: page,
26 },
27 });
28 };
29
30 const {
31 addToGroup,
32 updateProp,
33 removeFromGroup,
34 deleteGroup,
35 updateGroups,
36 validateUser,
37 } = props;
38
39 useEffect(() => {
40 if (!location.state) {
41 navigate("/groups");
42 }
43 }, [location]);
44
45 const { group_data } = location.state || {};
46 if (!group_data) return <div></div>;
47 const [propobject, setProp] = useState(group_data.properties);
48 const [propkeys, setPropKeys] = useState([]);
49 const [propvalues, setPropValues] = useState([]);
50
51 return (
52 <MainContainer errorAlert={errorAlert} setErrorAlert={setErrorAlert}>
53 <h1>Editing Group {group_data.name}</h1>
54 <Card>
55 <Card.Header>
56 <h2>Manage group members</h2>
57 </Card.Header>
58 <Card.Body>
59 <GroupSelect
60 users={group_data.users}
61 validateUser={validateUser}
62 onChange={(selection) => {
63 setSelected(selection);
64 setChanged(true);
65 }}
66 />
67 </Card.Body>
68 <Card.Header>
69 <h2>Manage group properties</h2>
70 </Card.Header>
71 <Card.Body>
72 <DynamicTable
73 current_propobject={group_data.properties}
74 setProp={setProp}
75 setPropKeys={setPropKeys}
76 setPropValues={setPropValues}
77
78 //Add keys
79 />
80 <div>
81 <span id="error"></span>
82 </div>
83 </Card.Body>
84 <Card.Footer>
85 <Link to="/groups">
86 <Button variant="light" id="return">
87 Back
88 </Button>
89 </Link>
90 <span> </span>
91 <Button
92 id="submit"
93 data-testid="submit"
94 variant="primary"
95 onClick={() => {
96 // check for changes
97 let new_users = selected.filter(
98 (e) => !group_data.users.includes(e),
99 );
100 let removed_users = group_data.users.filter(
101 (e) => !selected.includes(e),
102 );
103 let promiseQueue = [];
104 if (new_users.length > 0)
105 promiseQueue.push(addToGroup(new_users, group_data.name));
106 if (removed_users.length > 0)
107 promiseQueue.push(
108 removeFromGroup(removed_users, group_data.name),
109 );
110
111 if (hasDuplicates(propkeys) == true) {
112 setErrorAlert(`Duplicate keys found!`);
113 } else {
114 propkeys.forEach((key, i) => (propobject[key] = propvalues[i]));
115 }
116 if (
117 propobject != group_data.properties &&
118 hasDuplicates(propkeys) == false
119 ) {
120 promiseQueue.push(updateProp(propobject, group_data.name));
121 setErrorAlert(null);
122 }
123 Promise.all(promiseQueue)
124 .then((data) => {
125 // ensure status of all requests are < 300
126 let allPassed =
127 data.map((e) => e.status).filter((e) => e >= 300).length ==
128 0;
129
130 allPassed
131 ? updateGroups(0, limit).then((data) =>
132 dispatchPageUpdate(data, 0),
133 )
134 : setErrorAlert(`Failed to edit group.`);
135 })
136 .catch(() => {
137 setErrorAlert(`Failed to edit group.`);
138 });
139 }}
140 >
141 Apply
142 </Button>
143 <Button
144 id="delete-group"
145 data-testid="delete-group"
146 variant="danger"
147 className="float-end"
148 onClick={() => {
149 var groupName = group_data.name;
150 deleteGroup(groupName)
151 // TODO add error if res not ok
152 .then((data) => {
153 data.status < 300
154 ? updateGroups(0, limit)
155 .then((data) => dispatchPageUpdate(data, 0))
156 .then(() => navigate("/groups"))
157 : setErrorAlert(`Failed to delete group.`);
158 })
159 .catch(() => setErrorAlert(`Failed to delete group.`));
160 }}
161 >
162 Delete Group
163 </Button>
164 <div>
165 <span id="error"></span>
166 </div>
167 </Card.Footer>
168 </Card>
169 </MainContainer>
170 );
171 };
172
173 GroupEdit.propTypes = {
174 addToGroup: PropTypes.func,
175 removeFromGroup: PropTypes.func,
176 deleteGroup: PropTypes.func,
177 updateGroups: PropTypes.func,
178 validateUser: PropTypes.func,
179 };
180
181 export default GroupEdit;