Skip to content

Commit 6cea504

Browse files
committed
Clean up code, switch to auth
1 parent 1461cde commit 6cea504

File tree

5 files changed

+60
-112
lines changed

5 files changed

+60
-112
lines changed

admin-api-frontend/src/components/AddRolesForm/index.tsx

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,33 @@
11
import React, { useState } from "react";
22
import { Input, Button } from "@nextui-org/react";
3-
import axios from "axios";
3+
import useFetchWithMsal from "../../hooks/useFetchWithMsal.jsx";
4+
import { loginRequest } from "../../authConfig.js";
45

56
const AddRolesForm = () => {
67
const BASE_URL = process.env.REACT_APP_BASE_URL;
78

89
const [netID, setNetID] = useState<string>("");
910
const [roles, setRoles] = useState<string>("");
10-
const [rolesSplit, setRolesSplit] = useState<string[]>([]);
1111

12+
const { execute } = useFetchWithMsal(loginRequest);
1213
const handleAddRolesThroughUpdate = async () => {
1314
try {
14-
const response = await axios.put(
15-
`${BASE_URL}/default/api/v1/update_user`,
16-
null,
17-
{
18-
params: {
19-
netid: netID,
20-
newPerms: permissions,
21-
newRoles: roles,
22-
},
23-
}
24-
);
25-
console.log(response.data);
15+
execute(
16+
"PUT",
17+
`${BASE_URL}/default/api/v1/update_user?netid=${netID}&newRoles=${roles}&newPerms=`,
18+
null
19+
).then((response) => {
20+
console.log(response);
21+
});
2622
} catch (error) {
2723
console.log(error);
2824
}
2925
};
3026

31-
const handleNetIDChange = (event) => {
32-
setNetID(event.target.value);
33-
};
34-
35-
const handleRolesChange = (event) => {
36-
setRoles(event.target.value);
37-
setRolesSplit(event.target.value.split(","));
38-
};
39-
4027
const handleSubmit = (event) => {
4128
if (netID !== "" && roles !== "") {
42-
console.log(netID);
43-
console.log(rolesSplit);
44-
console.log(permissionsSplit);
45-
// Do something with the netID, roles and permissions
46-
4729
event.preventDefault();
4830
handleAddRolesThroughUpdate();
49-
setRolesSplit([]);
5031
setNetID("");
5132
setRoles("");
5233
}
@@ -61,14 +42,14 @@ const AddRolesForm = () => {
6142
<Input
6243
placeholder="NetID"
6344
value={netID}
64-
onChange={handleNetIDChange}
45+
onChange={(event) => setNetID(event.target.value)}
6546
/>
6647
</div>
6748
<div className="mb-2">
6849
<Input
6950
placeholder="Roles"
7051
value={roles}
71-
onChange={handleRolesChange}
52+
onChange={(event) => setRoles(event.target.value)}
7253
/>
7354
</div>
7455
<Button type="submit">Submit</Button>

admin-api-frontend/src/components/CreateUserForm/index.tsx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useState } from "react";
22
import { Input, Button } from "@nextui-org/react";
3-
import axios from "axios";
3+
import useFetchWithMsal from "../../hooks/useFetchWithMsal.jsx";
4+
import { loginRequest } from "../../authConfig.js";
45

56
const CreateUserForm = () => {
67
const BASE_URL = process.env.REACT_APP_BASE_URL;
@@ -9,30 +10,24 @@ const CreateUserForm = () => {
910
const [roles, setRoles] = useState<string>("");
1011
const [permissions, setPermissions] = useState<string>("");
1112

13+
const { execute } = useFetchWithMsal(loginRequest);
1214
const handleCreateUser = async () => {
1315
try {
14-
const response = await axios.put(
15-
`${BASE_URL}/default/api/v1/create_user`,
16-
null,
17-
{
18-
params: {
19-
netid: netID,
20-
permStr: permissions,
21-
roleStr: roles,
22-
},
23-
}
24-
);
25-
console.log(response.data);
16+
execute(
17+
"PUT",
18+
`${BASE_URL}/default/api/v1/create_user?netid=${netID}&permStr=${permissions}&roleStr=${roles}`,
19+
null
20+
).then((response) => {
21+
console.log(response);
22+
});
2623
} catch (error) {
2724
console.log(error);
2825
}
2926
};
3027

3128
const handleSubmit = (event) => {
3229
if (netID !== "" && roles !== "") {
33-
// This handles the API call
3430
handleCreateUser();
35-
3631
event.preventDefault();
3732
setNetID("");
3833
setRoles("");

admin-api-frontend/src/components/DeleteUserForm/index.tsx

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,30 @@
11
import React, { useState } from "react";
22
import { Input, Button } from "@nextui-org/react";
3-
import axios from "axios";
3+
import useFetchWithMsal from "../../hooks/useFetchWithMsal.jsx";
4+
import { loginRequest } from "../../authConfig.js";
45

56
const DeleteUserForm = () => {
67
const BASE_URL = process.env.REACT_APP_BASE_URL;
78

89
const [netID, setNetID] = useState<string>("");
910

11+
const { execute } = useFetchWithMsal(loginRequest);
1012
const handleDeleteUser = async () => {
1113
try {
12-
const response = await axios.delete(
13-
`${BASE_URL}/default/api/v1/delete_user`,
14-
{
15-
params: {
16-
netid: netID,
17-
},
18-
}
19-
);
20-
console.log(response.data);
14+
execute(
15+
"DELETE",
16+
`${BASE_URL}/default/api/v1/delete_user?netid=${netID}`,
17+
null
18+
).then((response) => {
19+
console.log(response);
20+
});
2121
} catch (error) {
2222
console.log(error);
2323
}
2424
};
2525

26-
const handleNetIDChange = (event) => {
27-
setNetID(event.target.value);
28-
};
29-
3026
const handleSubmit = (event) => {
3127
if (netID !== "") {
32-
console.log(netID);
33-
// Do something with the netID
3428
handleDeleteUser();
3529
event.preventDefault();
3630
setNetID("");
@@ -46,7 +40,7 @@ const DeleteUserForm = () => {
4640
<Input
4741
placeholder="NetID"
4842
value={netID}
49-
onChange={handleNetIDChange}
43+
onChange={(event) => setNetID(event.target.value)}
5044
/>
5145
</div>
5246
<Button type="submit">Submit</Button>

admin-api-frontend/src/components/GetUserInfoForm/index.tsx

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
import React, { useState } from "react";
22
import { Input, Button } from "@nextui-org/react";
3-
import axios from "axios";
3+
import useFetchWithMsal from "../../hooks/useFetchWithMsal.jsx";
4+
import { loginRequest } from "../../authConfig.js";
45

56
const GetUserInfoForm = () => {
67
const BASE_URL = process.env.REACT_APP_BASE_URL;
78

89
const [netID, setNetID] = useState<string>("");
910

11+
const { execute } = useFetchWithMsal(loginRequest);
12+
1013
const handleGetUser = async () => {
1114
try {
12-
const response = await axios.get(
13-
`${BASE_URL}/default/api/v1/get_user`,
14-
{
15-
params: {
16-
netid: netID,
17-
},
18-
}
19-
);
20-
console.log(response.data);
15+
execute(
16+
"GET",
17+
`${BASE_URL}/default/api/v1/get_user?netid=${netID}`,
18+
null
19+
).then((response) => {
20+
console.log(response);
21+
});
2122
} catch (error) {
2223
console.log(error);
2324
}
2425
};
2526

26-
const handleNetIDChange = (event) => {
27-
setNetID(event.target.value);
28-
};
29-
3027
const handleSubmit = (event) => {
3128
if (netID !== "") {
32-
console.log(netID);
33-
// Do something with the netID
34-
3529
event.preventDefault();
3630
handleGetUser();
3731
setNetID("");
@@ -47,7 +41,7 @@ const GetUserInfoForm = () => {
4741
<Input
4842
placeholder="NetID"
4943
value={netID}
50-
onChange={handleNetIDChange}
44+
onChange={(event) => setNetID(event.target.value)}
5145
/>
5246
</div>
5347
<Button type="submit">Submit</Button>

admin-api-frontend/src/components/RemoveRolesForm/index.tsx

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,36 @@
11
import React, { useState } from "react";
22
import { Input, Button } from "@nextui-org/react";
3-
import axios from "axios";
3+
import useFetchWithMsal from "../../hooks/useFetchWithMsal.jsx";
4+
import { loginRequest } from "../../authConfig.js";
45

56
const RemoveRolesForm = () => {
67
const BASE_URL = process.env.REACT_APP_BASE_URL;
78

89
const [netID, setNetID] = useState<string>("");
910
const [roles, setRoles] = useState<string>("");
10-
const [rolesSplit, setRolesSplit] = useState<string[]>([]);
1111

12+
const { execute } = useFetchWithMsal(loginRequest);
13+
14+
// This does not actually remove the roles, it just adds. We need to make a
15+
// New lambda function for this endpoint
1216
const handleRemoveRolesThroughUpdate = async () => {
1317
try {
14-
const response = await axios.put(
15-
`${BASE_URL}/default/api/v1/update_user`,
16-
null,
17-
{
18-
params: {
19-
netid: netID,
20-
newPerms: permissions,
21-
newRoles: roles,
22-
},
23-
}
24-
);
25-
console.log(response.data);
18+
execute(
19+
"PUT",
20+
`${BASE_URL}/default/api/v1/update_user?netid=${netID}&newRoles=${roles}`,
21+
null
22+
).then((response) => {
23+
console.log(response);
24+
});
2625
} catch (error) {
2726
console.log(error);
2827
}
2928
};
3029

31-
const handleNetIDChange = (event) => {
32-
setNetID(event.target.value);
33-
};
34-
35-
const handleRolesChange = (event) => {
36-
setRoles(event.target.value);
37-
setRolesSplit(event.target.value.split(","));
38-
};
39-
4030
const handleSubmit = (event) => {
4131
if (netID !== "" && roles !== "") {
42-
console.log(netID);
43-
console.log(rolesSplit);
44-
console.log(permissionsSplit);
45-
// Do something with the netID, roles and permissions
46-
4732
event.preventDefault();
4833
handleRemoveRolesThroughUpdate();
49-
setRolesSplit([]);
5034
setNetID("");
5135
setRoles("");
5236
}
@@ -61,14 +45,14 @@ const RemoveRolesForm = () => {
6145
<Input
6246
placeholder="NetID"
6347
value={netID}
64-
onChange={handleNetIDChange}
48+
onChange={(event) => setNetID(event.target.value)}
6549
/>
6650
</div>
6751
<div className="mb-2">
6852
<Input
6953
placeholder="Roles"
7054
value={roles}
71-
onChange={handleRolesChange}
55+
onChange={(event) => setRoles(event.target.value)}
7256
/>
7357
</div>
7458
<Button type="submit">Submit</Button>

0 commit comments

Comments
 (0)