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
107 lines (100 loc) · 3.05 KB
/
EditableUserLanguage.js
File metadata and controls
107 lines (100 loc) · 3.05 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
import React from 'react'
import { string } from 'prop-types'
import { Heading, Stack, Select, useToast } from '@chakra-ui/core'
import WithPseudoBox from './withPseudoBox'
import { t, Trans } from '@lingui/macro'
import { Formik, Field } from 'formik'
import { useMutation } from '@apollo/client'
import { UPDATE_USER_PROFILE } from './graphql/mutations'
import { useUserState } from './UserState'
import { useLingui } from '@lingui/react'
import { object, string as yupString } from 'yup'
import { fieldRequirements } from './fieldRequirements'
import { TrackerButton } from './TrackerButton'
function EditableUserLanguage({ currentLang }) {
const { currentUser } = useUserState()
const toast = useToast()
const { i18n } = useLingui()
const [updateUserProfile, { error: _updateUserProfileError }] = useMutation(
UPDATE_USER_PROFILE,
{
context: {
headers: {
authorization: currentUser.jwt,
},
},
onError: ({ message }) => {
toast({
title: i18n._(t`An error occurred while updating your language.`),
description: message,
status: 'error',
duration: 9000,
isClosable: true,
})
},
onCompleted() {
toast({
title: t`Changed User Language`,
description: t`You have successfully updated your password.`,
status: 'success',
duration: 9000,
isClosable: true,
})
},
},
)
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: {
input: {
preferredLang: values.lang,
},
},
})
}}
>
{({ handleSubmit, isSubmitting, getFieldProps }) => (
<form id="langForm" onSubmit={handleSubmit}>
<Stack isInline align="center" justifyContent="space-between">
<Field
id="lang"
component={Select}
{...getFieldProps('lang')}
w={['40%', '57%']}
>
<option value="ENGLISH">English</option>
<option value="FRENCH">Français</option>
</Field>
<TrackerButton
type="submitBtn"
isLoading={isSubmitting}
variant="primary"
>
<Trans>Save Language</Trans>
</TrackerButton>
</Stack>
</form>
)}
</Formik>
</Stack>
)
}
EditableUserLanguage.propTypes = {
currentLang: string,
}
export default WithPseudoBox(EditableUserLanguage)