-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.jsx
169 lines (143 loc) · 3.96 KB
/
index.jsx
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { localize, getLocaleSlug } from 'i18n-calypso';
import { get, findLast, findIndex } from 'lodash';
import Gridicon from 'components/gridicon';
import classnames from 'classnames';
/**
* Internal dependencies
*/
import { Button } from '@automattic/components';
import { getStepUrl } from 'signup/utils';
import { recordTracksEvent } from 'state/analytics/actions';
import { submitSignupStep } from 'state/signup/progress/actions';
import { getSignupProgress } from 'state/signup/progress/selectors';
import { getFilteredSteps } from '../utils';
/**
* Style dependencies
*/
import './style.scss';
export class NavigationLink extends Component {
static propTypes = {
goToNextStep: PropTypes.func,
direction: PropTypes.oneOf( [ 'back', 'forward' ] ),
flowName: PropTypes.string.isRequired,
labelText: PropTypes.string,
cssClass: PropTypes.string,
positionInFlow: PropTypes.number,
previousPath: PropTypes.string,
signupProgress: PropTypes.object,
stepName: PropTypes.string.isRequired,
// Allows to force a back button in the first step for example.
allowBackFirstStep: PropTypes.bool,
};
static defaultProps = {
labelText: '',
allowBackFirstStep: false,
};
/**
* Returns the previous step , skipping over steps with the
* `wasSkipped` property.
*
* @return {Object} The previous step object
*/
getPreviousStep() {
const { flowName, signupProgress, stepName } = this.props;
let steps = getFilteredSteps( flowName, signupProgress );
steps = steps.slice(
0,
findIndex( steps, step => step.stepName === stepName )
);
const previousStep = findLast( steps, step => ! step.wasSkipped );
return previousStep || { stepName: null };
}
getBackUrl() {
if ( this.props.direction !== 'back' ) {
return;
}
if ( this.props.backUrl ) {
return this.props.backUrl;
}
const previousStep = this.getPreviousStep();
const stepSectionName = get(
this.props.signupProgress,
[ previousStep.stepName, 'stepSectionName' ],
''
);
return getStepUrl(
previousStep.lastKnownFlow || this.props.flowName,
previousStep.stepName,
stepSectionName,
getLocaleSlug()
);
}
handleClick = () => {
if ( this.props.direction === 'forward' ) {
this.props.submitSignupStep(
{ stepName: this.props.stepName },
this.props.defaultDependencies
);
this.props.goToNextStep();
}
this.recordClick();
};
recordClick() {
const tracksProps = {
flow: this.props.flowName,
step: this.props.stepName,
};
if ( this.props.direction === 'back' ) {
this.props.recordTracksEvent( 'calypso_signup_previous_step_button_click', tracksProps );
}
if ( this.props.direction === 'forward' ) {
this.props.recordTracksEvent( 'calypso_signup_skip_step', tracksProps );
}
}
render() {
const { translate, labelText } = this.props;
if (
this.props.positionInFlow === 0 &&
this.props.direction === 'back' &&
! this.props.stepSectionName &&
! this.props.allowBackFirstStep
) {
return null;
}
let backGridicon, forwardGridicon, text;
if ( this.props.direction === 'back' ) {
backGridicon = <Gridicon icon="arrow-left" size={ 18 } />;
text = labelText ? labelText : translate( 'Back' );
}
if ( this.props.direction === 'forward' ) {
forwardGridicon = <Gridicon icon="arrow-right" size={ 18 } />;
text = labelText ? labelText : translate( 'Skip for now' );
}
const buttonClasses = classnames(
'navigation-link',
this.props.direction,
this.props.cssClass
);
return (
<Button
borderless
className={ buttonClasses }
href={ this.getBackUrl() }
onClick={ this.handleClick }
>
{ backGridicon }
{ text }
{ forwardGridicon }
</Button>
);
}
}
export default connect(
state => ( {
signupProgress: getSignupProgress( state ),
} ),
{ recordTracksEvent, submitSignupStep }
)( localize( NavigationLink ) );