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
249 lines (235 loc) · 7.01 KB
/
OrganizationDetails.js
File metadata and controls
249 lines (235 loc) · 7.01 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import React from 'react'
import { useQuery, useMutation } from '@apollo/client'
import { t, Trans } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import {
Box,
Button,
Flex,
Heading,
IconButton,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
Tab,
TabList,
TabPanel,
TabPanels,
Tabs,
useDisclosure,
useToast,
} from '@chakra-ui/react'
import { ArrowLeftIcon, CheckCircleIcon } from '@chakra-ui/icons'
import { Link as RouteLink, useParams, useHistory } from 'react-router-dom'
import { ErrorBoundary } from 'react-error-boundary'
import { OrganizationDomains } from './OrganizationDomains'
import { OrganizationAffiliations } from './OrganizationAffiliations'
import { OrganizationSummary } from './OrganizationSummary'
import { ErrorFallbackMessage } from '../components/ErrorFallbackMessage'
import { LoadingMessage } from '../components/LoadingMessage'
import { useDocumentTitle } from '../utilities/useDocumentTitle'
import { ORG_DETAILS_PAGE } from '../graphql/queries'
import { LEAVE_ORG } from '../graphql/mutations'
export default function OrganizationDetails() {
const { orgSlug } = useParams()
const toast = useToast()
const history = useHistory()
const { i18n } = useLingui()
useDocumentTitle(`${orgSlug}`)
const { loading, _error, data } = useQuery(ORG_DETAILS_PAGE, {
variables: { slug: orgSlug },
errorPolicy: 'ignore', // allow partial success
})
const [leaveOrganization, { loading: loadingLeaveOrg }] = useMutation(
LEAVE_ORG,
{
onError(error) {
toast({
title: i18n._(t`An error occurred.`),
description: error.message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ leaveOrganization }) {
if (leaveOrganization.result.__typename === 'LeaveOrganizationResult') {
toast({
title: i18n._(t`Organization left successfully`),
description: i18n._(t`You have successfully left ${orgSlug}`),
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
leaveOrgOnClose()
history.push('/organizations')
} else if (leaveOrganization.result.__typename === 'AffiliationError') {
toast({
title: i18n._(t`Unable to leave organization.`),
description: leaveOrganization.result.description,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else {
toast({
title: i18n._(t`Incorrect send method received.`),
description: i18n._(
t`Incorrect leaveOrganization.result typename.`,
),
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
console.log('Incorrect leaveOrganization.result typename.')
}
},
},
)
const {
isOpen: leaveOrgIsOpen,
onOpen: leaveOrgOnOpen,
onClose: leaveOrgOnClose,
} = useDisclosure()
if (loading) {
return (
<LoadingMessage>
<Trans>Organization Details</Trans>
</LoadingMessage>
)
}
const orgName = data?.organization?.name ?? ''
return (
<Box w="100%" px={4}>
<Flex
flexDirection="row"
align="center"
mb="4"
flexWrap={{ base: 'wrap', md: 'nowrap' }}
>
<IconButton
icon={<ArrowLeftIcon />}
as={RouteLink}
to={'/organizations'}
color="gray.900"
fontSize="2xl"
aria-label="back to organizations"
mr="0.5rem"
order={0}
/>
<Heading
as="h1"
textAlign={{ base: 'center', md: 'left' }}
mr={{ base: '0', md: '0.5rem' }}
order={{ base: 2, md: 1 }}
flexBasis={{ base: '100%', md: 'auto' }}
>
{orgName}
{data?.organization?.verified && (
<>
{' '}
<CheckCircleIcon color="blue.500" boxSize="icons.lg" />
</>
)}
</Heading>
<Button
variant="danger"
order={{ base: 1, md: 2 }}
onClick={() => {
leaveOrgOnOpen()
}}
flexShrink={0}
ml="auto"
>
<Trans> Leave Organization </Trans>
</Button>
</Flex>
<Tabs isFitted>
<TabList mb="4">
<Tab>
<Trans>Summary</Trans>
</Tab>
<Tab>
<Trans>Domains</Trans>
</Tab>
{!isNaN(data?.organization?.affiliations?.totalCount) && (
<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}>
<OrganizationDomains orgSlug={orgSlug} domainsPerPage={10} />
</ErrorBoundary>
</TabPanel>
{!isNaN(data?.organization?.affiliations?.totalCount) && (
<TabPanel>
<ErrorBoundary FallbackComponent={ErrorFallbackMessage}>
<OrganizationAffiliations orgSlug={orgSlug} usersPerPage={10} />
</ErrorBoundary>
</TabPanel>
)}
</TabPanels>
</Tabs>
<Modal
isOpen={leaveOrgIsOpen}
onClose={leaveOrgOnClose}
motionPreset="slideInBottom"
>
<ModalOverlay />
<ModalContent pb={4}>
<ModalHeader>
<Trans>Leave Organization</Trans>
</ModalHeader>
<ModalCloseButton />
<ModalBody>
<Trans>
Are you sure you wish to leave {orgName}? You will have to be
invited back in to access it.
</Trans>
</ModalBody>
<ModalFooter>
<Button variant="primaryOutline" mr="4" onClick={leaveOrgOnClose}>
<Trans>Cancel</Trans>
</Button>
<Button
variant="primary"
mr="4"
onClick={async () => {
await leaveOrganization({
variables: {
orgId: data?.organization?.id,
},
})
}}
isLoading={loadingLeaveOrg}
>
<Trans>Confirm</Trans>
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</Box>
)
}