forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminPage.js
More file actions
128 lines (119 loc) · 3.35 KB
/
AdminPage.js
File metadata and controls
128 lines (119 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React, { useState } from 'react'
import { Stack, Text, Select, useToast } from '@chakra-ui/core'
import { Trans, t } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import { Layout } from './Layout'
import AdminPanel from './AdminPanel'
import { USER_AFFILIATIONS } from './graphql/queries'
import { useQuery } from '@apollo/client'
import { useUserState } from './UserState'
export default function AdminPage() {
const { currentUser } = useUserState()
const [orgName, setOrgName] = useState()
const { i18n } = useLingui()
const toast = useToast()
const { loading, error, data } = useQuery(USER_AFFILIATIONS, {
context: {
headers: {
authorization: currentUser.jwt,
},
},
onError: error => {
const [_, message] = error.message.split(': ')
toast({
title: 'Error',
description: message,
status: 'failure',
duration: 9000,
isClosable: true,
position: 'bottom-left',
})
},
})
if (loading) {
return <p>Loading user affilliations...</p>
}
if (error) {
return <p>{String(error)}</p>
}
const adminAffiliations = {}
if (data?.user[0]?.affiliations?.edges) {
const {
affiliations: { edges },
} = data.user[0]
for (let i = 0; i < edges.length; i++) {
const {
node: {
permission,
organization: { acronym },
},
} = edges[i]
if (permission === 'ADMIN' || permission === 'SUPER_ADMIN') {
adminAffiliations[acronym] = permission
}
}
}
const adminOrgs = Object.keys(adminAffiliations)
const options = [
<option hidden key="default">
{i18n._(t`Select an organization`)}
</option>,
]
for (let i = 0; i < adminOrgs.length; i++) {
options.push(
<option key={'option' + i} value={adminOrgs[i]}>
{adminOrgs[i]}
</option>,
)
}
if (options.length > 1) {
return (
<Layout>
<Stack spacing={10}>
<Text fontSize="3xl" fontWeight="bold">
<Trans>Welcome, Admin</Trans>
</Text>
<Stack isInline align="center">
<Text fontWeight="bold" fontSize="xl">
<Trans>Organization: </Trans>
</Text>
<Select
aria-label="Select an organization"
w={['100%', '25%']}
size="lg"
variant="filled"
onChange={e => {
setOrgName(e.target.value)
}}
>
{options}
</Select>
</Stack>
{options.length > 1 && orgName ? (
<Stack>
<AdminPanel
orgName={orgName}
permission={adminAffiliations[orgName]}
mr="4"
/>
<Trans>
*search bars do not actively search databases currently. They
are used to demonstrate the 'add' button feature
</Trans>
</Stack>
) : (
<Text fontSize="2xl" fontWeight="bold" textAlign="center">
<Trans>Select an organization to view admin options</Trans>
</Text>
)}
</Stack>
</Layout>
)
} else {
return (
<Text fontSize="3xl" fontWeight="bold">
<Trans>You do not have admin permissions in any organization</Trans>
</Text>
)
}
}