-
Notifications
You must be signed in to change notification settings - Fork 2
feat(CentralIdentity): add campus admin roles mgmt to user view #736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
client/src/components/controlpanel/CentralIdentity/CampusAdminRolesSection.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| import { useState } from "react"; | ||
| import { Header, Table, Button, Icon, Dropdown } from "semantic-ui-react"; | ||
| import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; | ||
| import useGlobalError from "../../error/ErrorHooks"; | ||
| import { useNotifications } from "../../../context/NotificationContext"; | ||
| import { Organization } from "../../../types"; | ||
| import api from "../../../api"; | ||
|
|
||
| interface Props { | ||
| uuid: string; | ||
| } | ||
|
|
||
| type CampusAdminRole = { | ||
| org: Pick<Organization, "orgID" | "name" | "shortName">; | ||
| role: string; | ||
| roleInternal: string; | ||
| }; | ||
|
|
||
| const CampusAdminRolesSection: React.FC<Props> = ({ uuid }) => { | ||
| const queryClient = useQueryClient(); | ||
| const { handleGlobalError } = useGlobalError(); | ||
| const { addNotification } = useNotifications(); | ||
| const [selectedOrg, setSelectedOrg] = useState<string>(""); | ||
|
|
||
| const { data: adminRoles = [], isFetching: rolesLoading } = useQuery<CampusAdminRole[]>({ | ||
| queryKey: ["user-campus-admin-roles", uuid], | ||
| queryFn: async () => { | ||
| try { | ||
| const res = await api.getUserRoles(uuid); | ||
| if (res.data.err) throw new Error(res.data.errMsg); | ||
| return res.data.user.roles.filter((r) => r.roleInternal === "campusadmin"); | ||
| } catch (err) { | ||
| handleGlobalError(err); | ||
| return []; | ||
| } | ||
| }, | ||
| enabled: !!uuid, | ||
| staleTime: 1000 * 60 * 5, | ||
| refetchOnWindowFocus: false, | ||
| }); | ||
|
|
||
| const { data: allOrgs = [], isFetching: orgsLoading } = useQuery<Organization[]>({ | ||
| queryKey: ["all-organizations"], | ||
| queryFn: async () => { | ||
| try { | ||
| const res = await api.getAllOrganizations(); | ||
| if (res.data.err) throw new Error(res.data.errMsg); | ||
| return res.data.orgs; | ||
| } catch (err) { | ||
| handleGlobalError(err); | ||
| return []; | ||
| } | ||
| }, | ||
| staleTime: 1000 * 60 * 10, | ||
| refetchOnWindowFocus: false, | ||
| }); | ||
|
|
||
| const removeMutation = useMutation({ | ||
| mutationFn: async (orgID: string) => { | ||
| const res = await api.updateUserRole(uuid, orgID, "member"); | ||
| if (res.data.err) throw new Error(res.data.errMsg); | ||
| return res; | ||
| }, | ||
| onSuccess: () => { | ||
| addNotification({ message: "Campus admin role removed (demoted to member).", type: "success" }); | ||
| queryClient.invalidateQueries({ queryKey: ["user-campus-admin-roles", uuid] }); | ||
| }, | ||
| onError: (err) => handleGlobalError(err), | ||
| }); | ||
|
|
||
| const grantMutation = useMutation({ | ||
| mutationFn: async (orgID: string) => { | ||
| const res = await api.updateUserRole(uuid, orgID, "campusadmin"); | ||
| if (res.data.err) throw new Error(res.data.errMsg); | ||
| return res; | ||
| }, | ||
| onSuccess: () => { | ||
| addNotification({ message: "Campus admin role granted.", type: "success" }); | ||
| setSelectedOrg(""); | ||
| queryClient.invalidateQueries({ queryKey: ["user-campus-admin-roles", uuid] }); | ||
| }, | ||
|
jakeaturner marked this conversation as resolved.
|
||
| onError: (err) => handleGlobalError(err), | ||
| }); | ||
|
|
||
| const adminOrgIDs = new Set(adminRoles.map((r) => r.org.orgID)); | ||
| const grantableOrgs = allOrgs.filter((o) => !adminOrgIDs.has(o.orgID) && o.orgID !== "libretexts"); | ||
| const orgOptions = grantableOrgs.map((o) => ({ | ||
| key: o.orgID, | ||
| text: o.name, | ||
| value: o.orgID, | ||
| })); | ||
|
jakeaturner marked this conversation as resolved.
|
||
|
|
||
| return ( | ||
| <div className="flex flex-col rounded-md p-4 shadow-md bg-white h-fit space-y-1.5"> | ||
| <div className="flex justify-between items-center mb-4 border-b border-slate-300 pb-2"> | ||
| <Header as="h3" className="!m-0"> | ||
| Campus Admin Roles | ||
| </Header> | ||
| </div> | ||
| <Table compact celled> | ||
| <Table.Header> | ||
| <Table.Row> | ||
| <Table.HeaderCell>Organization</Table.HeaderCell> | ||
| <Table.HeaderCell>Actions</Table.HeaderCell> | ||
| </Table.Row> | ||
| </Table.Header> | ||
| <Table.Body> | ||
| {adminRoles.length > 0 ? ( | ||
| adminRoles.map((r) => ( | ||
| <Table.Row key={r.org.orgID}> | ||
| <Table.Cell>{r.org.name}</Table.Cell> | ||
| <Table.Cell> | ||
| <Button | ||
| icon | ||
| color="red" | ||
| size="tiny" | ||
| loading={removeMutation.isPending} | ||
| title="Remove campus admin role (demote to member)" | ||
| onClick={() => removeMutation.mutate(r.org.orgID)} | ||
| > | ||
| <Icon name="user delete" /> | ||
| </Button> | ||
| </Table.Cell> | ||
| </Table.Row> | ||
| )) | ||
| ) : ( | ||
| <Table.Row> | ||
| <Table.Cell colSpan={2} textAlign="center"> | ||
| {rolesLoading ? ( | ||
| <em>Loading...</em> | ||
| ) : ( | ||
| <em>No campus admin roles found.</em> | ||
| )} | ||
| </Table.Cell> | ||
| </Table.Row> | ||
| )} | ||
| </Table.Body> | ||
| </Table> | ||
| {!orgsLoading && grantableOrgs.length > 0 && ( | ||
| <div className="flex items-center gap-2 pt-2"> | ||
| <Dropdown | ||
| placeholder="Select organization to grant..." | ||
| selection | ||
| search | ||
| options={orgOptions} | ||
| value={selectedOrg} | ||
| onChange={(_, { value }) => setSelectedOrg(value as string)} | ||
| className="flex-1" | ||
| /> | ||
| <Button | ||
| color="green" | ||
| size="small" | ||
| disabled={!selectedOrg} | ||
| loading={grantMutation.isPending} | ||
| onClick={() => grantMutation.mutate(selectedOrg)} | ||
| > | ||
| <Icon name="plus" /> Grant | ||
| </Button> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default CampusAdminRolesSection; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.