forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrganizationDetails.js
More file actions
205 lines (195 loc) · 5.96 KB
/
Copy pathOrganizationDetails.js
File metadata and controls
205 lines (195 loc) · 5.96 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import React, { useState } from 'react'
import { useQuery } from '@apollo/client'
import { Trans } from '@lingui/macro'
import { Layout } from './Layout'
import {
IconButton,
Heading,
Stack,
useToast,
Tabs,
TabList,
TabPanels,
Tab,
TabPanel,
Box,
Divider,
Text,
Icon,
} from '@chakra-ui/core'
import { ORG_DETAILS_PAGE } from './graphql/queries'
import { useUserState } from './UserState'
import { useParams, useHistory } from 'react-router-dom'
import { OrganizationSummary } from './OrganizationSummary'
import { ErrorBoundary } from 'react-error-boundary'
import { ErrorFallbackMessage } from './ErrorFallbackMessage'
import { LoadingMessage } from './LoadingMessage'
import { DomainCard } from './DomainCard'
import { ListOf } from './ListOf'
import { PaginationButtons } from './PaginationButtons'
import { UserCard } from './UserCard'
export default function OrganizationDetails() {
const { orgSlug } = useParams()
const { currentUser, isLoggedIn } = useUserState()
const toast = useToast()
const history = useHistory()
const { loading, _error, data } = useQuery(ORG_DETAILS_PAGE, {
variables: { slug: orgSlug },
context: {
headers: {
authorization: currentUser.jwt,
},
},
onError: (error) => {
const [_, message] = error.message.split(': ')
toast({
title: 'Error',
description: message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
})
let orgName = ''
if (data?.organization) {
orgName = data.organization.name
}
let domains = []
if (data?.organization?.domains?.edges) {
domains = data.organization.domains.edges.map((e) => e.node)
}
let users = []
if (data?.organization?.affiliations?.edges) {
users = data.organization.affiliations.edges.map((e) => e.node)
}
const [currentPage, setCurrentPage] = useState(1)
const [domainsPerPage] = useState(10)
// Get current domains
const indexOfLastDomain = currentPage * domainsPerPage
const indexOfFirstDomain = indexOfLastDomain - domainsPerPage
const currentDomains = domains.slice(indexOfFirstDomain, indexOfLastDomain)
// Change page
const paginate = (pageNumber) => setCurrentPage(pageNumber)
if (loading) {
return (
<LoadingMessage>
<Trans>Organization Details</Trans>
</LoadingMessage>
)
}
return (
<Layout>
<Stack isInline align="center" mb="4">
<IconButton
icon="arrow-left"
onClick={history.goBack}
color="gray.900"
fontSize="2xl"
aria-label="back to organizations"
/>
<Heading as="h1" textAlign={['center', 'left']}>
<Trans>{orgName}</Trans>
</Heading>
{data?.organization?.verified && (
<Icon name="check-circle" color="blue.500" size="icons.lg" />
)}
</Stack>
<Tabs isFitted>
<TabList mb="4">
<Tab>
<Trans>Summary</Trans>
</Tab>
<Tab>
<Trans>Domains</Trans>
</Tab>
{isLoggedIn() && (
<Tab>
<Trans>Users</Trans>
</Tab>
)}
</TabList>
<TabPanels>
<TabPanel>
<ErrorBoundary FallbackComponent={ErrorFallbackMessage}>
<OrganizationSummary
summaries={data.organization.summaries}
domainCount={data.organization.domainCount}
userCount={data.organization.affiliations.totalCount}
city={data.organization.city}
province={data.organization.province}
/>
</ErrorBoundary>
</TabPanel>
<TabPanel>
<ErrorBoundary FallbackComponent={ErrorFallbackMessage}>
<ListOf
elements={currentDomains}
ifEmpty={() => (
<Text fontSize="xl" fontWeight="bold">
<Trans>No Domains</Trans>
</Text>
)}
mb="4"
>
{({ id, domain, lastRan, status }, index) => (
<ErrorBoundary
key={`${id}:${index}`}
FallbackComponent={ErrorFallbackMessage}
>
<Box>
<DomainCard
url={domain}
lastRan={lastRan}
status={status}
/>
<Divider borderColor="gray.900" />
</Box>
</ErrorBoundary>
)}
</ListOf>
{domains.length > 0 && (
<PaginationButtons
perPage={domainsPerPage}
total={domains.length}
paginate={paginate}
currentPage={currentPage}
/>
)}
<Trans>
*All data represented is mocked for demonstration purposes
</Trans>
</ErrorBoundary>
</TabPanel>
{isLoggedIn() && (
<TabPanel>
<ListOf
elements={users}
ifEmpty={() => (
<Text fontSize="xl" fontWeight="bold">
<Trans>No Users</Trans>
</Text>
)}
mb="4"
>
{({ permission, user }, index) => (
<ErrorBoundary FallbackComponent={ErrorFallbackMessage}>
<Box key={`${user.id}:${index}`}>
<UserCard
userName={user.userName}
role={permission}
tfa={user.tfaValidated}
/>
<Divider borderColor="gray.900" />
</Box>
</ErrorBoundary>
)}
</ListOf>
</TabPanel>
)}
</TabPanels>
</Tabs>
</Layout>
)
}