forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsideUserSwitch.js
More file actions
95 lines (92 loc) · 2.95 KB
/
InsideUserSwitch.js
File metadata and controls
95 lines (92 loc) · 2.95 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
import { useMutation } from '@apollo/client'
import { QuestionOutlineIcon } from '@chakra-ui/icons'
import { Badge, Flex, Switch, useToast, Tooltip } from '@chakra-ui/react'
import { Trans, t } from '@lingui/macro'
import { bool } from 'prop-types'
import React from 'react'
import { UPDATE_USER_PROFILE } from '../graphql/mutations'
import { useUserVar } from '../utilities/userState'
export function InsideUserSwitch({ insideUser }) {
const toast = useToast()
const { login, currentUser } = useUserVar()
const [updateUserProfile, { error: _updateUserProfileError }] = useMutation(
UPDATE_USER_PROFILE,
{
onError: ({ message }) => {
toast({
title: t`An error occurred while updating your inside user preference.`,
description: message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ updateUserProfile }) {
if (updateUserProfile.result.__typename === 'UpdateUserProfileResult') {
toast({
title: t`Inside user status changed`,
description: t`You have successfully updated your inside user preference.`,
status: 'success',
duration: 9000,
isClosable: true,
position: 'top-left',
})
login({
...currentUser,
insideUser: updateUserProfile.result.user.insideUser,
})
} else if (
updateUserProfile.result.__typename === 'UpdateUserProfileError'
) {
toast({
title: t`Unable to update to your inside user status, please try again.`,
description: updateUserProfile.result.description,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
} else {
toast({
title: t`Incorrect update method received.`,
description: t`Incorrect updateUserProfile.result typename.`,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
console.log('Incorrect updateUserProfile.result typename.')
}
},
},
)
return (
<Flex p="1" align="center">
<Tooltip
label={t`For users interested in using new features that are still in progress.`}
>
<QuestionOutlineIcon tabIndex={0} />
</Tooltip>
<Switch
isFocusable={true}
id="Inside User"
name="Inside User"
aria-label="Feature Preview"
mx="2"
defaultChecked={insideUser}
onChange={async (e) =>
await updateUserProfile({
variables: { insideUser: e.target.checked },
})
}
/>
<Badge variant="outline" color="gray.900" p="1.5">
<Trans>Feature Preview</Trans>
</Badge>
</Flex>
)
}
InsideUserSwitch.propTypes = {
insideUser: bool.isRequired,
}