|
| 1 | +import "./UserRoleAudits.scss"; |
| 2 | +import {useAppStore} from "../stores/AppStore"; |
| 3 | +import React, {useEffect, useState} from "react"; |
| 4 | +import {Entities} from "../components/Entities"; |
| 5 | +import I18n from "../locale/I18n"; |
| 6 | +import {useNavigate} from "react-router-dom"; |
| 7 | +import {AUTHORITIES, isUserAllowed} from "../utils/UserRole"; |
| 8 | +import {fetchRoles, searchUserRoleAudits} from "../api"; |
| 9 | +import {isEmpty} from "../utils/Utils"; |
| 10 | +import debounce from "lodash.debounce"; |
| 11 | +import {defaultPagination, pageCount} from "../utils/Pagination"; |
| 12 | +import {dateFromEpoch, shortDateFromEpoch} from "../utils/Date"; |
| 13 | +import SelectField from "../components/SelectField"; |
| 14 | +import {UnitHeader} from "../components/UnitHeader"; |
| 15 | +import Logo from "@surfnet/sds/icons/illustrative-icons/database-hand.svg"; |
| 16 | + |
| 17 | +export const UserRoleAudits = () => { |
| 18 | + const {user} = useAppStore(state => state); |
| 19 | + |
| 20 | + const [searching, setSearching] = useState(isUserAllowed(AUTHORITIES.INSTITUTION_ADMIN, user)); |
| 21 | + const [userRoleAudits, setUserRoleAudits] = useState([]); |
| 22 | + const [roles, setRoles] = useState([]); |
| 23 | + const [selectedRole, setSelectedRole] = useState(null); |
| 24 | + const [paginationQueryParams, setPaginationQueryParams] = useState(defaultPagination("userEmail")); |
| 25 | + const [totalElements, setTotalElements] = useState(0); |
| 26 | + const navigate = useNavigate(); |
| 27 | + |
| 28 | + if (!isUserAllowed(AUTHORITIES.INSTITUTION_ADMIN, user)) { |
| 29 | + navigate("/home") |
| 30 | + } |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + fetchRoles() |
| 34 | + .then(res => { |
| 35 | + setRoles(res); |
| 36 | + useAppStore.setState({ |
| 37 | + breadcrumbPath: [ |
| 38 | + {path: "/home", value: I18n.t("tabs.home")}, |
| 39 | + {value: I18n.t("header.links.audit")} |
| 40 | + ] |
| 41 | + }); |
| 42 | + }) |
| 43 | + }, [user]); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + searchUserRoleAudits(paginationQueryParams, isEmpty(selectedRole) ? null : selectedRole.value) |
| 47 | + .then(page => { |
| 48 | + setUserRoleAudits(page.content); |
| 49 | + setTotalElements(page.totalElements); |
| 50 | + setSearching(false); |
| 51 | + }) |
| 52 | + }, [paginationQueryParams, selectedRole]); |
| 53 | + |
| 54 | + const search = (query, sorted, reverse, page) => { |
| 55 | + const paginationQueryParamsChanged = sorted !== paginationQueryParams.sort || reverse !== paginationQueryParams.sortDirection || |
| 56 | + page !== paginationQueryParams.pageNumber; |
| 57 | + if ((!isEmpty(query) && query.trim().length > 2) || paginationQueryParamsChanged) { |
| 58 | + delayedAutocomplete(query, sorted, reverse, page); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + const delayedAutocomplete = debounce((query, sorted, reverse, page) => { |
| 63 | + setSearching(true); |
| 64 | + //this will trigger a new search |
| 65 | + setPaginationQueryParams({ |
| 66 | + query: query, |
| 67 | + pageNumber: page, |
| 68 | + pageSize: pageCount, |
| 69 | + sort: sorted, |
| 70 | + sortDirection: reverse ? "DESC" : "ASC" |
| 71 | + }) |
| 72 | + }, 375); |
| 73 | + |
| 74 | + const filters = () => { |
| 75 | + return ( |
| 76 | + <SelectField |
| 77 | + value={selectedRole} |
| 78 | + options={roles.map(role => ({value:role.id, label: role.name}))} |
| 79 | + searchable={true} |
| 80 | + clearable={true} |
| 81 | + placeholder={I18n.t("userRoleAudit.rolePlaceHolder")} |
| 82 | + onChange={option => setSelectedRole(option)} |
| 83 | + /> |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + const columns = [ |
| 88 | + { |
| 89 | + key: "userEmail", |
| 90 | + header: I18n.t("userRoleAudit.email"), |
| 91 | + mapper: userRoleAudit => userRoleAudit.userEmail |
| 92 | + }, |
| 93 | + { |
| 94 | + key: "roleName", |
| 95 | + header: I18n.t("userRoleAudit.roleName"), |
| 96 | + mapper: userRoleAudit => userRoleAudit.roleName |
| 97 | + }, |
| 98 | + { |
| 99 | + key: "action", |
| 100 | + header: I18n.t("userRoleAudit.action"), |
| 101 | + mapper: userRoleAudit => I18n.t(`userRoleAudit.actions.${userRoleAudit.action}`) |
| 102 | + }, |
| 103 | + { |
| 104 | + key: "authority", |
| 105 | + header: I18n.t("userRoleAudit.authority"), |
| 106 | + mapper: userRoleAudit => userRoleAudit.authority |
| 107 | + }, |
| 108 | + { |
| 109 | + key: "createdAt", |
| 110 | + header: I18n.t("userRoleAudit.createdAt"), |
| 111 | + mapper: userRoleAudit => shortDateFromEpoch(userRoleAudit.createdAt, true) |
| 112 | + }, |
| 113 | + { |
| 114 | + key: "endDate", |
| 115 | + header: I18n.t("userRoleAudit.endDate"), |
| 116 | + mapper: userRoleAudit => shortDateFromEpoch(userRoleAudit.endDate, true) |
| 117 | + }, |
| 118 | + ]; |
| 119 | + |
| 120 | + return ( |
| 121 | + <div className={"mod-user-role-audits"}> |
| 122 | + <UnitHeader obj={({name: I18n.t("userRoleAudit.header"), svg: Logo, style: "small"})}> |
| 123 | + <p>{I18n.t("userRoleAudit.info")}</p> |
| 124 | + </UnitHeader> |
| 125 | + <Entities |
| 126 | + entities={userRoleAudits} |
| 127 | + modelName="userRoleAudit" |
| 128 | + showNew={false} |
| 129 | + defaultSort="userEmail" |
| 130 | + columns={columns} |
| 131 | + searchAttributes={["userEmail", "roleName"]} |
| 132 | + loading={false} |
| 133 | + filters={filters()} |
| 134 | + inputFocus={!searching} |
| 135 | + hideTitle={searching} |
| 136 | + customSearch={search} |
| 137 | + totalElements={totalElements} |
| 138 | + busy={searching} |
| 139 | + /> |
| 140 | + </div> |
| 141 | + ); |
| 142 | + |
| 143 | +} |
0 commit comments