forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateOrganizationPage.js
More file actions
171 lines (154 loc) · 6.24 KB
/
CreateOrganizationPage.js
File metadata and controls
171 lines (154 loc) · 6.24 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
import React from 'react'
import { Box, Button, Flex, SimpleGrid, Heading, Stack, useToast, useDisclosure } from '@chakra-ui/react'
import { t, Trans } from '@lingui/macro'
import { useMutation } from '@apollo/client'
import { Formik } from 'formik'
import { Link as RouteLink, useNavigate } from 'react-router-dom'
import { useLingui } from '@lingui/react'
import { CreateOrganizationField } from '../components/fields/CreateOrganizationField'
import { InfoButton, InfoBox, InfoPanel } from '../components/InfoPanel'
import { LoadingMessage } from '../components/LoadingMessage'
import { getRequirement, schemaToValidation } from '../utilities/fieldRequirements'
import { CREATE_ORGANIZATION } from '../graphql/mutations'
export default function CreateOrganizationPage() {
const toast = useToast()
const navigate = useNavigate()
const { i18n } = useLingui()
const { isOpen, onToggle } = useDisclosure()
const fieldRequirement = getRequirement('field')
const acronymRequirement = getRequirement('acronym').required(i18n._(t`This field cannot be empty`))
const validationSchema = schemaToValidation({
nameEN: fieldRequirement,
nameFR: fieldRequirement,
acronymEN: acronymRequirement,
acronymFR: acronymRequirement,
cityEN: fieldRequirement,
cityFR: fieldRequirement,
provinceEN: fieldRequirement,
provinceFR: fieldRequirement,
countryEN: fieldRequirement,
countryFR: fieldRequirement,
})
const [createOrganization, { loading }] = useMutation(CREATE_ORGANIZATION, {
onError(error) {
toast({
title: t`An error occurred.`,
description: error.message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ createOrganization }) {
if (createOrganization.result.__typename === 'Organization') {
toast({
title: t`Organization created`,
description: t`${createOrganization.result.name} was created`,
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
navigate('/admin')
} else if (createOrganization.result.__typename === 'OrganizationError') {
toast({
title: t`Unable to create new organization.`,
description: createOrganization.result.description,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else {
toast({
title: t`Incorrect send method received.`,
description: t`Incorrect createOrganization.result typename.`,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
console.log('Incorrect createOrganization.result typename.')
}
},
})
if (loading) return <LoadingMessage />
return (
<Box px="4" mx="auto" overflow="hidden" w="100%">
<Formik
validationSchema={validationSchema}
initialValues={{
nameEN: '',
nameFR: '',
acronymEN: '',
acronymFR: '',
cityEN: '',
cityFR: '',
provinceEN: '',
provinceFR: '',
countryEN: '',
countryFR: '',
}}
onSubmit={async (values) => {
createOrganization({
variables: {
nameEN: values.nameEN,
nameFR: values.nameFR,
acronymEN: values.acronymEN,
acronymFR: values.acronymFR,
zoneEN: '',
zoneFR: '',
sectorEN: '',
sectorFR: '',
countryEN: values.countryEN,
countryFR: values.countryFR,
provinceEN: values.provinceEN,
provinceFR: values.provinceFR,
cityEN: values.cityEN,
cityFR: values.cityFR,
},
})
}}
>
{({ handleSubmit, isSubmitting }) => (
<form id="form" onSubmit={handleSubmit}>
<Flex>
<Heading as="h1" fontSize="2xl" textAlign="center">
<Trans>Create an organization</Trans>
</Heading>
</Flex>
<InfoPanel isOpen={isOpen} onToggle={onToggle}>
<InfoBox title="Name" info="The name of the Organization." />
<InfoBox title="Acronym" info="The acronym of the Organization." />
<InfoBox title="City" info="The city the Organization is based in." />
<InfoBox title="Province" info="The province the Organization is based in." />
<InfoBox title="Country" info="The country the Organization is based in." />
</InfoPanel>
<SimpleGrid columns={{ base: 1, md: 2 }} spacing={{ base: 2, lg: 4 }} mt="4">
<CreateOrganizationField name="nameEN" language={t`English`} label={t`Name`} />
<CreateOrganizationField name="nameFR" language={t`French`} label={t`Name`} />
<CreateOrganizationField name="acronymEN" language={t`English`} label={t`Acronym`} />
<CreateOrganizationField name="acronymFR" language={t`French`} label={t`Acronym`} />
<CreateOrganizationField name="cityEN" language={t`English`} label={t`City`} />
<CreateOrganizationField name="cityFR" language={t`French`} label={t`City`} />
<CreateOrganizationField name="provinceEN" language={t`English`} label={t`Province`} />
<CreateOrganizationField name="provinceFR" language={t`French`} label={t`Province`} />
<CreateOrganizationField name="countryEN" language={t`English`} label={t`Country`} />
<CreateOrganizationField name="countryFR" language={t`French`} label={t`Country`} />
</SimpleGrid>
<Stack spacing={4} isInline justifyContent="space-between" my="6">
<Button variant="primaryOutline" as={RouteLink} to="/admin">
<Trans>Back</Trans>
</Button>
<Button variant="primary" type="submit" id="submitBtn" isLoading={isSubmitting}>
<Trans>Create Organization</Trans>
</Button>
</Stack>
</form>
)}
</Formik>
<InfoButton isOpen={isOpen} onToggle={onToggle} left="50%" />
</Box>
)
}