forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditableUserLanguage.js
More file actions
121 lines (113 loc) · 3.66 KB
/
EditableUserLanguage.js
File metadata and controls
121 lines (113 loc) · 3.66 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
import React from 'react'
import { Button, Heading, Select, Stack, useToast } from '@chakra-ui/react'
import { t, Trans } from '@lingui/macro'
import { i18n } from '@lingui/core'
import { Field, Formik } from 'formik'
import { useMutation } from '@apollo/client'
import { object, string as yupString } from 'yup'
import { string } from 'prop-types'
import { UPDATE_USER_PROFILE } from '../graphql/mutations'
import { fieldRequirements } from '../utilities/fieldRequirements'
export function EditableUserLanguage({ currentLang }) {
const toast = useToast()
const [updateUserProfile, { error: _updateUserProfileError }] = useMutation(
UPDATE_USER_PROFILE,
{
onError: ({ message }) => {
toast({
title: t`An error occurred while updating your language.`,
description: message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ updateUserProfile }) {
if (updateUserProfile.result.__typename === 'UpdateUserProfileResult') {
toast({
title: t`Changed User Language`,
description: t`You have successfully updated your preferred language.`,
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else if (
updateUserProfile.result.__typename === 'UpdateUserProfileError'
) {
toast({
title: t`Unable to update to your preferred language, please try again.`,
description: updateUserProfile.result.description,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else {
toast({
title: t`Incorrect send method received.`,
description: t`Incorrect updateUserProfile.result typename.`,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
console.log('Incorrect updateUserProfile.result typename.')
}
},
},
)
const validationSchema = object().shape({
lang: yupString()
.required(i18n._(fieldRequirements.lang.required.message))
.oneOf(fieldRequirements.lang.oneOf.types),
})
return (
<Stack spacing="4">
<Heading as="h3" size="md">
<Trans>Language:</Trans>
</Heading>
<Formik
validateOnBlur={false}
initialValues={{ lang: currentLang }}
validationSchema={validationSchema}
onSubmit={async (values) => {
// Submit update detail mutation
await updateUserProfile({
variables: {
preferredLang: values.lang,
},
})
}}
>
{({ handleSubmit, isSubmitting, getFieldProps }) => (
<form id="langForm" onSubmit={handleSubmit}>
<Stack isInline align="center" justifyContent="space-between">
<Field
data-testid="user-language-select"
id="lang"
component={Select}
{...getFieldProps('lang')}
w="57%"
>
<option value="ENGLISH">English</option>
<option value="FRENCH">Français</option>
</Field>
<Button
variant="primary"
type="submitBtn"
isLoading={isSubmitting}
>
<Trans>Save Language</Trans>
</Button>
</Stack>
</form>
)}
</Formik>
</Stack>
)
}
EditableUserLanguage.propTypes = {
currentLang: string,
}