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
170 lines (160 loc) · 4.83 KB
/
AdminPage.js
File metadata and controls
170 lines (160 loc) · 4.83 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import React, { useState } from 'react'
import { Divider, Icon, Select, Stack, Text, useToast } from '@chakra-ui/core'
import { t, Trans } from '@lingui/macro'
import { Layout } from './Layout'
import AdminPanel from './AdminPanel'
import { ADMIN_AFFILIATIONS, IS_USER_SUPER_ADMIN } from './graphql/queries'
import { useQuery } from '@apollo/client'
import { ErrorFallbackMessage } from './ErrorFallbackMessage'
import { LoadingMessage } from './LoadingMessage'
import { TrackerButton } from './TrackerButton'
import { Link as RouteLink } from 'react-router-dom'
import OrganizationInformation from './OrganizationInformation'
export default function AdminPage() {
const [selectedOrg, setSelectedOrg] = useState('none')
const [orgDetails, setOrgDetails] = useState({})
const toast = useToast()
const { loading, error, data } = useQuery(ADMIN_AFFILIATIONS, {
fetchPolicy: 'cache-and-network',
variables: {
first: 100,
orderBy: { field: 'ACRONYM', direction: 'ASC' },
isAdmin: true,
includeSuperAdminOrg: true,
},
onError: (error) => {
const [_, message] = error.message.split(': ')
toast({
title: 'Error',
description: message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
})
const { data: isSA } = useQuery(IS_USER_SUPER_ADMIN, {
onError: (error) => {
const [_, message] = error.message.split(': ')
toast({
title: 'Error',
description: message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
})
if (loading) {
return (
<LoadingMessage>
<Trans>Admin Affiliations</Trans>
</LoadingMessage>
)
}
if (error) {
return <ErrorFallbackMessage error={error} />
}
const adminAffiliations = {}
data.findMyOrganizations?.edges.forEach((edge) => {
const { slug, acronym, id } = edge.node
adminAffiliations[acronym] = {
slug: slug,
id: id,
}
})
const adminOrgsAcronyms = Object.keys(adminAffiliations)
const options = [
<option hidden key="default" value="none">
{t`Select an organization`}
</option>,
]
adminOrgsAcronyms.forEach((acronym) => {
options.push(
<option key={acronym} value={acronym}>
{acronym}
</option>,
)
})
if (options.length > 1) {
return (
<Layout>
<Stack spacing={10}>
<Text fontSize="4xl" fontWeight="bold" textAlign={['center', 'left']}>
<Trans>Welcome, Admin</Trans>
</Text>
<Stack flexDirection={['column', 'row']} align="center">
<Text fontWeight="bold" fontSize="2xl">
<Trans>Organization: </Trans>
</Text>
<Select
w={['100%', '25%']}
size="lg"
variant="filled"
onChange={(e) => {
setOrgDetails(adminAffiliations[e.target.value])
setSelectedOrg(e.target.value)
}}
value={selectedOrg}
>
{options}
</Select>
<TrackerButton
ml={['0', 'auto']}
w={['100%', 'auto']}
variant="primary"
as={RouteLink}
to="/create-organization"
>
<Icon name="add" />
<Trans>Create Organization</Trans>
</TrackerButton>
</Stack>
{options.length > 1 && selectedOrg !== 'none' ? (
<>
<OrganizationInformation
orgSlug={orgDetails.slug}
mb="1rem"
removeOrgCallback={setSelectedOrg}
// set key, this resets state when switching orgs (closes editing box)
key={orgDetails.slug}
/>
<AdminPanel
orgSlug={orgDetails.slug}
orgId={orgDetails.id}
permission={isSA?.isUserSuperAdmin ? 'SUPER_ADMIN' : 'ADMIN'}
mr="4"
/>
</>
) : (
<Text fontSize="2xl" fontWeight="bold" textAlign="center">
<Trans>Select an organization to view admin options</Trans>
</Text>
)}
</Stack>
</Layout>
)
} else {
return (
<Layout>
<Stack align="center">
<Text fontSize="3xl" fontWeight="bold" textAlign="center">
<Trans>You do not have admin permissions in any organization</Trans>
</Text>
<Divider />
<TrackerButton
w={['100%', 'auto']}
variant="primary"
as={RouteLink}
to="/create-organization"
>
<Icon name="add" />
<Trans>Create Organization</Trans>
</TrackerButton>
</Stack>
</Layout>
)
}
}