Skip to content

Commit ce0bb4e

Browse files
authored
fix super user toggle (#2675)
1 parent 08d22e8 commit ce0bb4e

3 files changed

Lines changed: 75 additions & 6 deletions

File tree

src/components/SuperAdmin/MetricsData.jsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FormattedDate } from "react-intl";
2+
import SuperUserToggle from "./SuperUserToggle";
23

34
const OSM_USER_LINK = `${window.env.REACT_APP_OSM_SERVER}/user/`;
45

@@ -159,7 +160,7 @@ export const PROJECT_COLUMNS = [
159160
},
160161
];
161162

162-
export const USER_COLUMNS = [
163+
export const getUserColumns = (userChanges, setUserChanges) => [
163164
{
164165
Header: "Id",
165166
accessor: "id",
@@ -206,6 +207,13 @@ export const USER_COLUMNS = [
206207
Header: "Role",
207208
accessor: "superUser",
208209
width: 200,
209-
Cell: ({ value }) => <div>{value ? "Super User" : "Basic User"}</div>,
210+
Cell: ({ row, value }) => (
211+
<SuperUserToggle
212+
initialValue={value}
213+
userId={row.original?.id}
214+
userChanges={userChanges}
215+
setUserChanges={setUserChanges}
216+
/>
217+
),
210218
},
211219
];

src/components/SuperAdmin/MetricsTable.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import { useMemo } from "react";
1+
import { useMemo, useState } from "react";
22
import { injectIntl } from "react-intl";
33
import { usePagination, useResizeColumns, useSortBy, useTable } from "react-table";
44
import BusySpinner from "../BusySpinner/BusySpinner";
55
import WithSortedChallenges from "../HOCs/WithSortedChallenges/WithSortedChallenges";
66
import PaginationControl from "../PaginationControl/PaginationControl";
77
import { TableWrapper, renderTableHeader } from "../TableShared/EnhancedTable";
88
import { cellStyles, rowStyles, tableStyles } from "../TableShared/TableStyles";
9-
import { CHALLENGE_COLUMNS, PROJECT_COLUMNS, USER_COLUMNS } from "./MetricsData";
9+
import { CHALLENGE_COLUMNS, PROJECT_COLUMNS, getUserColumns } from "./MetricsData";
1010
import WithMetricsSearchResults from "./WithMetricsSearchResults";
1111
import WithSortedProjects from "./WithSortedProjects";
1212
import WithSortedUsers from "./WithSortedUsers";
1313

1414
const MetricsTable = (props) => {
15+
const [userChanges, setUserChanges] = useState({});
16+
1517
const data = useMemo(() => {
1618
if (props.currentTab === "challenges") {
1719
return props.challenges.map((c) => ({
@@ -58,10 +60,10 @@ const MetricsTable = (props) => {
5860
} else if (props.currentTab === "projects") {
5961
return PROJECT_COLUMNS;
6062
} else if (props.currentTab === "users") {
61-
return USER_COLUMNS;
63+
return getUserColumns(userChanges, setUserChanges);
6264
}
6365
return [];
64-
}, [props.currentTab]);
66+
}, [props.currentTab, userChanges]);
6567

6668
const {
6769
getTableProps,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { useEffect, useState } from "react";
2+
import Endpoint from "../../services/Server/Endpoint";
3+
import { defaultRoutes as api } from "../../services/Server/Server";
4+
import WithCurrentUser from "../HOCs/WithCurrentUser/WithCurrentUser";
5+
6+
const SuperUserToggle = (props) => {
7+
const [selection, setSelection] = useState(props.initialValue ? "super" : "basic");
8+
9+
useEffect(() => {
10+
const localState = props.userChanges[props.userId];
11+
setSelection(localState !== undefined ? localState : props.initialValue ? "super" : "basic");
12+
}, [props.userId, props.initialValue, props.userChanges]);
13+
14+
const updateValue = async (e) => {
15+
const value = e.target.value;
16+
setSelection(value);
17+
18+
try {
19+
if (value === "super") {
20+
await new Endpoint(api.superUser.addSuperUserGrant, {
21+
variables: { userId: props.userId },
22+
}).execute();
23+
} else {
24+
await new Endpoint(api.superUser.deleteSuperUserGrant, {
25+
variables: { userId: props.userId },
26+
}).execute();
27+
}
28+
29+
props.setUserChanges?.({
30+
...props.userChanges,
31+
[props.userId]: value,
32+
});
33+
} catch (error) {
34+
// Revert on error
35+
const localState = props.userChanges[props.userId];
36+
setSelection(localState !== undefined ? localState : props.initialValue ? "super" : "basic");
37+
console.error(error);
38+
}
39+
};
40+
41+
if (props.user?.id !== props.userId) {
42+
return (
43+
<div>
44+
<select
45+
className={selection === "super" ? "mr-bg-green" : "mr-bg-blue"}
46+
value={selection}
47+
onChange={updateValue}
48+
>
49+
<option value="basic">Basic User</option>
50+
<option value="super">Super User</option>
51+
</select>
52+
</div>
53+
);
54+
}
55+
56+
return <div>Super User</div>;
57+
};
58+
59+
export default WithCurrentUser(SuperUserToggle);

0 commit comments

Comments
 (0)