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
129 lines (122 loc) · 3.93 KB
/
EditableUserLanguage.js
File metadata and controls
129 lines (122 loc) · 3.93 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
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 { i18n } from '@lingui/core'
import { Formik, Field } from 'formik'
import { useMutation } from '@apollo/client'
import { UPDATE_USER_PROFILE } from './graphql/mutations'
import { useUserState } from './UserState'
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 [updateUserProfile, { error: _updateUserProfileError }] = useMutation(
UPDATE_USER_PROFILE,
{
context: {
headers: {
authorization: currentUser.jwt,
},
},
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
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)