-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.jsx
239 lines (209 loc) · 6.39 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/** @format */
/**
* External dependencies
*/
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { localize } from 'i18n-calypso';
import { countBy, find, get, noop } from 'lodash';
import Gridicon from 'gridicons';
import store from 'store';
/**
* Internal dependencies
*/
import Button from 'components/button';
import Card from 'components/card';
import Gauge from 'components/gauge';
import ProgressBar from 'components/progress-bar';
import QuerySiteChecklist from 'components/data/query-site-checklist';
import getSiteChecklist from 'state/selectors/get-site-checklist';
import { getSite, getSiteSlug } from 'state/sites/selectors';
import { launchTask, tasks as onboardingTasks } from 'my-sites/checklist/onboardingChecklist';
import { mergeObjectIntoArrayById } from 'my-sites/checklist/util';
import ChecklistShowShare from 'my-sites/checklist/share';
import { recordTracksEvent } from 'state/analytics/actions';
import { requestGuidedTour } from 'state/ui/guided-tours/actions';
import { getABTestVariation } from 'lib/abtest';
const storeKeyForNeverShow = 'sitesNeverShowChecklistBanner';
export class ChecklistBanner extends Component {
static propTypes = {
task: PropTypes.shape( {
id: PropTypes.string,
title: PropTypes.string,
description: PropTypes.string,
image: PropTypes.string,
} ),
completed: PropTypes.number,
total: PropTypes.number,
siteId: PropTypes.number,
siteSlug: PropTypes.string,
};
static defaultProps = {
task: null,
onClick: noop,
completed: 0,
total: 1,
};
state = {
closed: false,
};
handleClick = () => {
const { requestTour, task, track, siteSlug } = this.props;
launchTask( {
task,
location: 'checklist_banner',
requestTour,
siteSlug,
track,
} );
};
handleClose = () => {
const { siteId } = this.props;
const sitesNeverShowBanner = store.get( storeKeyForNeverShow ) || {};
sitesNeverShowBanner[ `${ siteId }` ] = true;
store.set( storeKeyForNeverShow, sitesNeverShowBanner );
this.setState( { closed: true } );
this.props.track( 'calypso_checklist_banner_close', {
site_id: siteId,
} );
};
getNextTask() {
const { completed, siteSlug, total, translate } = this.props;
if ( completed === total ) {
return {
id: 'ready-to-share',
title: translate( 'Your site is ready to share' ),
description: translate(
'We did it! You have completed {{a}}all the tasks{{/a}} on our checklist.',
{
components: {
a: <a href={ `/checklist/${ siteSlug }` } />,
},
}
),
image: '/calypso/images/stats/tasks/ready-to-share.svg',
};
}
return this.props.task;
}
canShow() {
if ( this.state.closed ) {
return false;
}
if ( this.props.siteDesignType !== 'blog' ) {
return false;
}
if (
getABTestVariation( 'checklistThankYouForPaidUser' ) !== 'show' &&
getABTestVariation( 'checklistThankYouForFreeUser' ) !== 'show'
) {
return false;
}
const sitesNeverShowBanner = store.get( storeKeyForNeverShow );
if ( get( sitesNeverShowBanner, String( this.props.siteId ) ) === true ) {
return false;
}
return true;
}
renderShareButtons() {
return (
<ChecklistShowShare
className="checklist-banner__actions"
siteSlug={ this.props.siteSlug }
recordTracksEvent={ this.props.track }
/>
);
}
renderTaskButton() {
return (
<div className="checklist-banner__actions">
<Button onClick={ this.handleClick } className="checklist-banner__button" primary>
{ this.props.translate( 'Do it' ) }
</Button>
<a href={ `/checklist/${ this.props.siteSlug }` } className="checklist-banner__link">
{ this.props.translate( 'View your checklist' ) }
</a>
</div>
);
}
render() {
const { completed, total, translate, siteId } = this.props;
const task = this.getNextTask();
const percentage = Math.round( ( completed / total ) * 100 ) || 0;
if ( ! this.canShow() ) {
return null;
}
return (
<Card className="checklist-banner">
{ siteId && <QuerySiteChecklist siteId={ siteId } /> }
<div className="checklist-banner__gauge">
<span className="checklist-banner__gauge-additional-text">{ translate( 'setup' ) }</span>
<Gauge
width={ 152 }
height={ 152 }
lineWidth={ 18 }
percentage={ +percentage }
metric={ translate( 'completed' ) }
colors={ [ '#ffffff', '#47b766' ] }
/>
</div>
<div className="checklist-banner__progress">
<span className="checklist-banner__progress-title">{ translate( 'Site setup' ) }</span>
<span className="checklist-banner__progress-desc">
{ translate( '%(percentage)s%% completed', { args: { percentage } } ) }
{ completed === total && (
<Button
borderless
className="checklist-banner__close-mobile"
onClick={ this.handleClose }
>
<Gridicon size={ 24 } icon="cross" color="#87a6bc" />
</Button>
) }
</span>
<ProgressBar value={ completed } total={ total } color="#47b766" />
</div>
<div className="checklist-banner__content">
<h3 className="checklist-banner__title">{ task && task.title }</h3>
<p className="checklist-banner__description">{ task && task.description }</p>
{ completed === total ? this.renderShareButtons() : this.renderTaskButton() }
</div>
{ task &&
task.image && (
<img src={ task.image } aria-hidden="true" alt="" className="checklist-banner__image" />
) }
{ completed === total && (
<Button borderless className="checklist-banner__close" onClick={ this.handleClose }>
<Gridicon size={ 24 } icon="cross" color="#87a6bc" />
</Button>
) }
</Card>
);
}
}
const mapStateToProps = ( state, { siteId } ) => {
const tasksFromServer = get( getSiteChecklist( state, siteId ), [ 'tasks' ] );
const tasks = tasksFromServer
? mergeObjectIntoArrayById( onboardingTasks, tasksFromServer )
: null;
const task = find( tasks, { completed: false } );
const { true: completed } = countBy( tasks, 'completed' );
const siteSlug = getSiteSlug( state, siteId );
const siteDesignType = get( getSite( state, siteId ), 'options.design_type' );
return {
task,
completed,
total: tasks && tasks.length,
siteSlug,
siteDesignType,
};
};
const mapDispatchToProps = {
track: recordTracksEvent,
requestTour: requestGuidedTour,
};
export default connect(
mapStateToProps,
mapDispatchToProps
)( localize( ChecklistBanner ) );