forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTourComponent.js
More file actions
114 lines (104 loc) · 3.39 KB
/
TourComponent.js
File metadata and controls
114 lines (104 loc) · 3.39 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
import React, { useEffect } from 'react'
import Joyride from 'react-joyride'
import { useTour } from '../hooks/useTour'
import { mainTourSteps } from '../config/tourSteps'
import { t, Trans } from '@lingui/macro'
import { useUserVar } from '../../utilities/userState'
import theme from '../../theme/canada'
import { useLocation } from 'react-router-dom'
import { toursConfig, matchPathname } from './TourButton'
import { COMPLETE_TOUR } from '../../graphql/mutations'
import { useMutation } from '@apollo/client'
import { useToast } from '@chakra-ui/react'
export const TourComponent = () => {
const toast = useToast()
const { isEmailValidated } = useUserVar()
const { isTourOpen, endTour, startTour } = useTour()
const { login, currentUser, isLoggedIn } = useUserVar()
const { darkOrange } = theme.colors.tracker.logo
const { pathname } = useLocation()
const tourName = matchPathname(pathname, toursConfig)
function checkCompletedTour({ completedTours }) {
const completedTour = completedTours.find((tour) => tour.tourId === tourName)
return !!completedTour
}
const [completeTour, { _loading, _error, called }] = useMutation(COMPLETE_TOUR, {
onError: ({ message }) => {
toast({
title: t`An error occurred when completing the tour.`,
description: message,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
},
onCompleted({ completeTour }) {
if (completeTour.result.__typename === 'CompleteTourResult') {
login({
...currentUser,
completedTours: completeTour.result.user.completedTours,
})
} else if (completeTour.result.__typename === 'CompleteTourError') {
toast({
title: t`Unable to complete the tour.`,
description: completeTour.result.description,
status: 'error',
duration: 9000,
isClosable: true,
position: 'top-left',
})
}
},
})
// handles starting the tour based on the page and user state
useEffect(() => {
if (!tourName || called) return
const hasCompletedTour = checkCompletedTour({ completedTours: currentUser?.completedTours || [] })
if (!hasCompletedTour && isEmailValidated()) startTour()
}, [tourName, currentUser?.completedTours])
if (!tourName || !isLoggedIn()) {
return null
}
// handles the finishing and skipping/closing of tour
const handleJoyrideCallback = async ({ status, type, action }) => {
if (
(['finished', 'skipped'].includes(status) && type === 'tour:end') ||
(status === 'running' && type === 'step:after' && action === 'close')
) {
endTour()
await completeTour({
variables: {
tourId: tourName,
},
})
}
}
return (
<Joyride
key={`${tourName}-tour-${isTourOpen}`}
steps={mainTourSteps[tourName]['steps']}
run={isTourOpen}
continuous={true}
showProgress={false}
showSkipButton={true}
disableCloseOnEsc={false}
disableOverlayClose={false}
styles={{
buttonNext: {
backgroundColor: darkOrange,
},
buttonBack: {
color: darkOrange,
},
}}
locale={{
back: <Trans>Back</Trans>,
next: <Trans>Next</Trans>,
skip: <Trans>Skip</Trans>,
last: <Trans>Finish</Trans>,
}}
callback={handleJoyrideCallback}
/>
)
}