|
| 1 | +import React, {useEffect, useState} from "react"; |
| 2 | +import I18n from "../locale/I18n"; |
| 3 | +import "./MineInvitations.scss"; |
| 4 | +import {Chip} from "@surfnet/sds"; |
| 5 | +import {Entities} from "../components/Entities"; |
| 6 | +import "./Users.scss"; |
| 7 | +import {shortDateFromEpoch} from "../utils/Date"; |
| 8 | + |
| 9 | +import {chipTypeForUserRole, invitationExpiry} from "../utils/Authority"; |
| 10 | +import {invitationsMine} from "../api"; |
| 11 | +import {useAppStore} from "../stores/AppStore"; |
| 12 | +import {INVITATION_STATUS} from "../utils/UserRole"; |
| 13 | + |
| 14 | + |
| 15 | +export const MineInvitations = () => { |
| 16 | + |
| 17 | + const {user} = useAppStore(state => state); |
| 18 | + const [invitations, setInvitations] = useState([]); |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + invitationsMine() |
| 22 | + .then(res => { |
| 23 | + res.forEach(invitation => { |
| 24 | + invitation.intendedRoles = (invitation.roles || []) |
| 25 | + .sort((r1, r2) => r1.role.name.localeCompare(r2.role.name)) |
| 26 | + .map(role => role.role.name).join(", "); |
| 27 | + const now = new Date(); |
| 28 | + invitation.status = new Date(invitation.expiryDate * 1000) < now ? INVITATION_STATUS.EXPIRED : invitation.status; |
| 29 | + }); |
| 30 | + setInvitations(res); |
| 31 | + }) |
| 32 | + }, |
| 33 | + [user]) |
| 34 | + |
| 35 | + |
| 36 | + const columns = [ |
| 37 | + { |
| 38 | + key: "email", |
| 39 | + header: I18n.t("users.email"), |
| 40 | + mapper: invitation => <span>{invitation.email}</span> |
| 41 | + }, |
| 42 | + { |
| 43 | + key: "intended_authority", |
| 44 | + header: I18n.t("users.authority"), |
| 45 | + mapper: invitation => <Chip type={chipTypeForUserRole(invitation.intendedAuthority)} |
| 46 | + label={I18n.t(`access.${invitation.intendedAuthority}`)}/> |
| 47 | + }, |
| 48 | + { |
| 49 | + key: "intendedRoles", |
| 50 | + nonSortable: true, |
| 51 | + header: I18n.t("invitations.intendedRoles"), |
| 52 | + mapper: invitation => invitation.intendedRoles |
| 53 | + }, |
| 54 | + { |
| 55 | + key: "name", |
| 56 | + header: I18n.t("invitations.inviter"), |
| 57 | + mapper: invitation => <div className="user-name-email"> |
| 58 | + <span className="name">{invitation.inviter.name}</span> |
| 59 | + <span className="email">{invitation.inviter.email}</span> |
| 60 | + </div> |
| 61 | + }, |
| 62 | + { |
| 63 | + key: "created_at", |
| 64 | + header: I18n.t("invitations.createdAt"), |
| 65 | + mapper: invitation => shortDateFromEpoch(invitation.createdAt, false) |
| 66 | + }, |
| 67 | + { |
| 68 | + key: "expiry_date", |
| 69 | + header: I18n.t("invitations.expiryDate"), |
| 70 | + mapper: invitation => invitationExpiry(invitation) |
| 71 | + } |
| 72 | + ] |
| 73 | + |
| 74 | + return (<div className="mod-mine-invitations"> |
| 75 | + <Entities entities={invitations} |
| 76 | + modelName="invitations" |
| 77 | + defaultSort="email" |
| 78 | + title={I18n.t("invitations.mine")} |
| 79 | + columns={columns} |
| 80 | + customNoEntities={I18n.t(`invitations.noResults`)} |
| 81 | + showNew={false} |
| 82 | + loading={false} |
| 83 | + searchAttributes={["intendedRoles", "email", "intendedAuthority", "inviter__email", "inviter__name"]} |
| 84 | + inputFocus={true} |
| 85 | + > |
| 86 | + </Entities> |
| 87 | + </div>) |
| 88 | + |
| 89 | +} |
0 commit comments