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
143 lines (132 loc) · 4.15 KB
/
AdminPage.js
File metadata and controls
143 lines (132 loc) · 4.15 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
import React, { useCallback, useState } from 'react'
import { Button, Flex, Stack, Text, useToast } from '@chakra-ui/react'
import { AddIcon } from '@chakra-ui/icons'
import { t, Trans } from '@lingui/macro'
import { useQuery } from '@apollo/client'
import { Link as RouteLink } from 'react-router-dom'
import { useLingui } from '@lingui/react'
import { AdminPanel } from './AdminPanel'
import { OrganizationInformation } from './OrganizationInformation'
import { ADMIN_PAGE } from '../graphql/queries'
import { Dropdown } from '../components/Dropdown'
import { ErrorFallbackMessage } from '../components/ErrorFallbackMessage'
import { useDebouncedFunction } from '../utilities/useDebouncedFunction'
import { bool } from 'prop-types'
export default function AdminPage({ isLoginRequired }) {
const [selectedOrg, setSelectedOrg] = useState('none')
const [orgDetails, setOrgDetails] = useState({})
const [searchTerm, setSearchTerm] = useState('')
const [debouncedSearchTerm, setDebouncedSearchTerm] = useState('')
const toast = useToast()
const { i18n } = useLingui()
const memoizedSetDebouncedSearchTermCallback = useCallback(() => {
setDebouncedSearchTerm(searchTerm)
}, [searchTerm])
useDebouncedFunction(memoizedSetDebouncedSearchTermCallback, 500)
const { loading, error, data } = useQuery(ADMIN_PAGE, {
fetchPolicy: 'cache-and-network',
nextFetchPolicy: 'cache-first',
variables: {
first: 100,
orderBy: { field: 'NAME', direction: 'ASC' },
isAdmin: true,
includeSuperAdminOrg: true,
search: debouncedSearchTerm,
},
onError: (error) => {
const [_, message] = error.message.split(': ')
toast({
title: 'Error',
description: message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
})
if (error) {
return <ErrorFallbackMessage error={error} />
}
let dropdown
let options = []
if (loading) {
dropdown = (
<Dropdown
label={i18n._(t`Organization: `)}
labelDirection="row"
options={[]}
placeholder={i18n._(t`Select an organization`)}
onSearch={(val) => setSearchTerm(val)}
searchValue={searchTerm}
/>
)
} else {
options = []
data.findMyOrganizations?.edges.forEach((edge) => {
const { slug, name, id } = edge.node
options.push({ label: name, value: { slug: slug, id: id } })
})
dropdown = (
<Dropdown
label={i18n._(t`Organization: `)}
labelDirection="row"
options={options}
placeholder={i18n._(t`Select an organization`)}
onSearch={(val) => setSearchTerm(val)}
searchValue={searchTerm}
onChange={(opt) => {
setOrgDetails(opt.value)
setSelectedOrg(opt.label)
}}
/>
)
}
return (
<Stack spacing={10} w="100%" px={4}>
<Flex
direction={{ base: 'column', md: 'row' }}
align="center"
justifyContent="space-between"
>
{dropdown}
<Button
variant="primary"
ml={{ base: '0', md: 'auto' }}
w={{ base: '100%', md: 'auto' }}
mt={{ base: 2, md: 0 }}
as={RouteLink}
to="/create-organization"
>
<AddIcon mr={2} aria-hidden="true" />
<Trans>Create Organization</Trans>
</Button>
</Flex>
{selectedOrg !== 'none' ? (
<>
<OrganizationInformation
orgSlug={orgDetails.slug}
mb="1rem"
removeOrgCallback={setSelectedOrg}
// set key, this resets state when switching orgs (closes editing box)
key={orgDetails.slug}
isLoginRequired={isLoginRequired}
/>
<AdminPanel
orgSlug={orgDetails.slug}
orgId={orgDetails.id}
permission={data?.isUserSuperAdmin ? 'SUPER_ADMIN' : 'ADMIN'}
mr="4"
/>
</>
) : (
<Text fontSize="2xl" fontWeight="bold" textAlign="center">
<Trans>Select an organization to view admin options</Trans>
</Text>
)}
</Stack>
)
}
AdminPage.propTypes = {
isLoginRequired: bool,
}