-
Notifications
You must be signed in to change notification settings - Fork 96
/
index.js
130 lines (114 loc) · 4.03 KB
/
index.js
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
130
import React from 'react';
import { connect } from 'react-redux';
import { translate } from 'react-i18next';
import Joyride from 'react-joyride';
import throttle from 'lodash.throttle';
import { FontIcon } from '../fontIcon';
import { steps } from './steps';
import breakpoints from './../../constants/breakpoints';
import { settingsUpdated } from '../../actions/settings';
class Onboarding extends React.Component {
constructor(props) {
super(props);
this.onboardingStarted = false;
this.state = {
isDesktop: window.innerWidth > breakpoints.m,
intro: true,
skip: false,
steps: [],
};
}
componentWillReceiveProps(nextProps) {
if ((nextProps.appLoaded && !this.stepsAdded)
|| this.props.showDelegates !== nextProps.showDelegates) {
this.addSteps(nextProps.showDelegates);
}
}
componentDidMount() {
window.addEventListener('resize', throttle(this.resizeWindow.bind(this), 1000));
if (this.props.appLoaded) {
this.addSteps(this.props.showDelegates);
}
}
componentWillUnmount() {
window.removeEventListener('resize', this.resizeWindow.bind(this));
}
resizeWindow() {
this.setState({ isDesktop: window.innerWidth > breakpoints.m });
}
addSteps(showDelegates) {
let newSteps = steps(this.props.t);
if (!showDelegates) {
newSteps = newSteps.filter(step => step.selector !== '#delegates');
}
this.setState({ steps: newSteps });
this.stepsAdded = true;
}
reset() {
this.onboardingStarted = false;
this.onboardingFinished = false;
this.joyride.reset(true);
this.setState({ intro: true, skip: false });
this.props.settingsUpdated({ onBoarding: false });
}
onboardingCallback(data) {
// index 0 is the step you will see when you skip the onboarding
const onboardingNotStarted = data.index === 0 && !this.onboardingStarted;
const onboardingStarted = data.index === 1;
const onboardingStep = data.index > 1;
const skipOnboarding = data.action === 'skip';
const lastStepReached = data.index === this.state.steps.length - 1;
const onboardingFinished = data.type === 'finished';
if (onboardingNotStarted) {
this.joyride.next(); // skip to welcome step
}
if (onboardingStarted) {
this.onboardingFinished = false;
this.onboardingStarted = true;
}
if (onboardingStep) {
this.setState({ intro: false });
}
if (skipOnboarding) {
this.setState({ skip: true });
this.joyride.reset(true); // go to step 0 to show the skip step
}
if (lastStepReached) {
this.onboardingFinished = true;
}
if (onboardingFinished) {
if (this.onboardingFinished) this.reset();
this.onboardingFinished = true;
}
}
render() {
const { isDesktop, skip, intro } = this.state;
return <div className={this.props.isAuthenticated && this.props.start && isDesktop ? 'joyride-showing' : ''}> <Joyride
ref={(el) => { this.joyride = el; }}
steps={this.state.steps}
run={this.props.isAuthenticated && this.props.start && isDesktop}
locale={{
last: (<span>{this.props.t('Complete')}</span>),
skip: skip ? <span>{this.props.t('Use Lisk Hub')}</span> : <span>{this.props.t('Click here to skip')}</span>,
next: intro ? <span>{this.props.t('Start the tour')}</span> : <span>{this.props.t('Next')} <FontIcon value='arrow-right'/></span>,
}}
callback={this.onboardingCallback.bind(this)}
showOverlay={true}
showSkipButton={true}
autoStart={true}
type='continuous'
holePadding={0}
keyboardNavigation={false}
/></div>;
}
}
const mapDispatchToProps = dispatch => ({
settingsUpdated: data => dispatch(settingsUpdated(data)),
});
const mapStateToProps = state => ({
isAuthenticated: !!state.account.publicKey,
showDelegates: state.settings.advancedMode,
start: Object.prototype.hasOwnProperty.call(state.settings, 'onBoarding') && state.settings.onBoarding,
});
export { Onboarding };
export default connect(mapStateToProps, mapDispatchToProps)(translate()(Onboarding));