|
| 1 | +import React, {useCallback, useEffect, useState} from "react"; |
| 2 | +import I18n from "../locale/I18n"; |
| 3 | +import "../components/Entities.scss"; |
| 4 | +import {Chip, ChipType, Loader, Tooltip} from "@surfnet/sds"; |
| 5 | +import {Entities} from "../components/Entities"; |
| 6 | +import {applicationManagers, institutionAdmins, removeApplicationManager, removeInstitutionAdmin} from "../api"; |
| 7 | +import UserIcon from "@surfnet/sds/icons/functional-icons/id-2.svg"; |
| 8 | +import "./InstitutionAdmins.scss"; |
| 9 | +import {useNavigate} from "react-router-dom"; |
| 10 | +import {useAppStore} from "../stores/AppStore"; |
| 11 | +import {dateFromEpoch, shortDateFromEpoch} from "../utils/Date"; |
| 12 | +import {AUTHORITIES} from "../utils/UserRole"; |
| 13 | +import {chipTypeForUserRole} from "../utils/Authority"; |
| 14 | +import TrashIcon from "@surfnet/sds/icons/functional-icons/bin.svg"; |
| 15 | +import ConfirmationDialog from "../components/ConfirmationDialog"; |
| 16 | +import {isEmpty} from "../utils/Utils"; |
| 17 | + |
| 18 | +export const InstitutionAdmins = () => { |
| 19 | + |
| 20 | + const {user: currentUser, setFlash} = useAppStore(state => state); |
| 21 | + |
| 22 | + const [confirmation, setConfirmation] = useState({}); |
| 23 | + const [confirmationOpen, setConfirmationOpen] = useState(false); |
| 24 | + const [admins, setAdmins] = useState([]); |
| 25 | + const [loading, setLoading] = useState(true); |
| 26 | + const navigate = useNavigate(); |
| 27 | + |
| 28 | + const loadAdmins = useCallback(() => { |
| 29 | + Promise.all([applicationManagers(), institutionAdmins(true)]) |
| 30 | + .then(res => { |
| 31 | + setAdmins(res[0].concat(res[1])); |
| 32 | + }) |
| 33 | + .finally(() => { |
| 34 | + setLoading(false); |
| 35 | + }); |
| 36 | + }, []); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + if (!currentUser.institutionAdmin || !currentUser.organizationGUID) { |
| 40 | + navigate("/home"); |
| 41 | + return; |
| 42 | + } |
| 43 | + loadAdmins(); |
| 44 | + }, [currentUser.institutionAdmin, currentUser.organizationGUID, loadAdmins, navigate]); |
| 45 | + |
| 46 | + if (loading) { |
| 47 | + return <Loader/> |
| 48 | + } |
| 49 | + |
| 50 | + const columns = [ |
| 51 | + { |
| 52 | + nonSortable: true, |
| 53 | + key: "icon", |
| 54 | + header: "", |
| 55 | + mapper: user => <div className="member-icon"> |
| 56 | + <Tooltip standalone={true} |
| 57 | + children={<UserIcon/>} |
| 58 | + tip={I18n.t("tooltips.userIcon", |
| 59 | + { |
| 60 | + name: user.name, |
| 61 | + createdAt: dateFromEpoch(user.createdAt, false), |
| 62 | + lastActivity: dateFromEpoch(user.lastActivity, false) |
| 63 | + })}/> |
| 64 | + </div> |
| 65 | + }, |
| 66 | + { |
| 67 | + key: "name", |
| 68 | + header: I18n.t("users.name_email"), |
| 69 | + mapper: user => ( |
| 70 | + <div className="user-name-email"> |
| 71 | + <span className="name">{user.name}</span> |
| 72 | + <span className="email">{user.email}</span> |
| 73 | + </div>) |
| 74 | + }, |
| 75 | + { |
| 76 | + key: "schac_home_organization", |
| 77 | + header: I18n.t("users.schacHomeOrganization"), |
| 78 | + mapper: user => <span>{user.schac_home_organization}</span> |
| 79 | + }, |
| 80 | + { |
| 81 | + key: "authority", |
| 82 | + header: I18n.t("users.highestAuthority"), |
| 83 | + mapper: user => { |
| 84 | + const authority = user.institution_admin ? AUTHORITIES.INSTITUTION_ADMIN : AUTHORITIES.APPLICATION_MANAGER; |
| 85 | + return <Chip type={chipTypeForUserRole(authority)} |
| 86 | + label={I18n.t(`access.${authority}`)}/> |
| 87 | + } |
| 88 | + }, |
| 89 | + { |
| 90 | + key: "createdAt", |
| 91 | + header: I18n.t("users.createdAt"), |
| 92 | + mapper: user => shortDateFromEpoch(user.createdAt, false) |
| 93 | + }, |
| 94 | + { |
| 95 | + key: "lastActivity", |
| 96 | + header: I18n.t("users.lastActivity"), |
| 97 | + mapper: user => shortDateFromEpoch(user.lastActivity, false) |
| 98 | + }, |
| 99 | + { |
| 100 | + key: "roles", |
| 101 | + header: I18n.t("users.roles"), |
| 102 | + mapper: user => isEmpty(user.roles) ? "-" : |
| 103 | + <ul>{user.roles.split(",").map(role => <li key={role}>{role}</li>)}</ul> |
| 104 | + }, |
| 105 | + { |
| 106 | + nonSortable: true, |
| 107 | + key: "actions", |
| 108 | + header: "", |
| 109 | + mapper: user => (currentUser.id !== user.id) ? |
| 110 | + ((user.institution_admin_by_invite || !isEmpty(user.roles)) ? < |
| 111 | + span onClick={() => doRemoveInstitutionAdmin(user, true)}> |
| 112 | + <TrashIcon/> |
| 113 | + </span> : null) |
| 114 | + : <Chip type={ChipType.Main_400} label={I18n.t("forms.you")}/> |
| 115 | + } |
| 116 | + ].filter(column => column !== null); |
| 117 | + |
| 118 | + const doRemoveInstitutionAdmin = (user, showConfirmation) => { |
| 119 | + const isInstitutionAdmin = user.institution_admin; |
| 120 | + if (showConfirmation) { |
| 121 | + setConfirmation({ |
| 122 | + cancel: () => setConfirmationOpen(false), |
| 123 | + action: () => doRemoveInstitutionAdmin(user, false), |
| 124 | + question: I18n.t("users.removeInstitutionAdminConfirmation", { |
| 125 | + userName: user.name |
| 126 | + }), |
| 127 | + confirmationTxt: I18n.t("confirmationDialog.confirm"), |
| 128 | + confirmationHeader: I18n.t("confirmationDialog.title") |
| 129 | + }); |
| 130 | + setConfirmationOpen(true); |
| 131 | + } else { |
| 132 | + setLoading(true); |
| 133 | + const promise = isInstitutionAdmin ? removeInstitutionAdmin(user) : removeApplicationManager(user); |
| 134 | + promise |
| 135 | + .then(() => { |
| 136 | + setConfirmationOpen(false); |
| 137 | + setFlash(I18n.t("users.removeInstitutionAdminFlash", {userName: user.name})); |
| 138 | + loadAdmins(); |
| 139 | + }).finally(() => { |
| 140 | + setLoading(false); |
| 141 | + }) |
| 142 | + } |
| 143 | + }; |
| 144 | + |
| 145 | + return ( |
| 146 | + <div className="mod-users"> |
| 147 | + {confirmationOpen && <ConfirmationDialog isOpen={confirmationOpen} |
| 148 | + cancel={confirmation.cancel} |
| 149 | + confirm={confirmation.action} |
| 150 | + confirmationTxt={confirmation.confirmationTxt} |
| 151 | + confirmationHeader={confirmation.confirmationHeader} |
| 152 | + isError={confirmation.error} |
| 153 | + question={confirmation.question}/>} |
| 154 | + <Entities entities={admins} |
| 155 | + modelName="institutionAdmins" |
| 156 | + defaultSort="name" |
| 157 | + columns={columns} |
| 158 | + newLabel={currentUser.superUser ? I18n.t("invitations.newInvite") : null} |
| 159 | + showNew={false} |
| 160 | + customNoEntities={I18n.t(`users.noResults`)} |
| 161 | + searchAttributes={["name", "email", "schacHomeOrganization"]} |
| 162 | + inputFocus={true} |
| 163 | + /> |
| 164 | + </div> |
| 165 | + ); |
| 166 | + |
| 167 | +} |
0 commit comments