-
Notifications
You must be signed in to change notification settings - Fork 2k
/
notice.jsx
337 lines (300 loc) · 10.9 KB
/
notice.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import React from 'react';
import moment from 'moment';
import { connect } from 'react-redux';
import { localize } from 'i18n-calypso';
import config, { isEnabled } from 'calypso/config';
import { get, reject, transform } from 'lodash';
/**
* Internal dependencies
*/
import { getUrlParts } from 'calypso/lib/url/url-parts';
import Notice from 'calypso/components/notice';
import NoticeAction from 'calypso/components/notice/notice-action';
import getActiveDiscount from 'calypso/state/selectors/get-active-discount';
import { domainManagementList } from 'calypso/my-sites/domains/paths';
import {
hasDomainCredit,
isCurrentUserCurrentPlanOwner,
} from 'calypso/state/sites/plans/selectors';
import canCurrentUser from 'calypso/state/selectors/can-current-user';
import isDomainOnlySite from 'calypso/state/selectors/is-domain-only-site';
import isEligibleForFreeToPaidUpsell from 'calypso/state/selectors/is-eligible-for-free-to-paid-upsell';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import QuerySitePlans from 'calypso/components/data/query-site-plans';
import QueryActivePromotions from 'calypso/components/data/query-active-promotions';
import { getDomainsBySiteId } from 'calypso/state/sites/domains/selectors';
import { getProductsList } from 'calypso/state/products-list/selectors';
import QueryProductsList from 'calypso/components/data/query-products-list';
import { getCurrentUserCurrencyCode } from 'calypso/state/current-user/selectors';
import { getUnformattedDomainPrice, getUnformattedDomainSalePrice } from 'calypso/lib/domains';
import formatCurrency from '@automattic/format-currency/src';
import { getPreference } from 'calypso/state/preferences/selectors';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import { savePreference } from 'calypso/state/preferences/actions';
import isSiteMigrationInProgress from 'calypso/state/selectors/is-site-migration-in-progress';
import isSiteMigrationActiveRoute from 'calypso/state/selectors/is-site-migration-active-route';
import { getSectionName } from 'calypso/state/ui/selectors';
import { getTopJITM } from 'calypso/state/jitm/selectors';
import AsyncLoad from 'calypso/components/async-load';
import UpsellNudge from 'calypso/blocks/upsell-nudge';
import { preventWidows } from 'calypso/lib/formatting';
import isSiteWPForTeams from 'calypso/state/selectors/is-site-wpforteams';
const DOMAIN_UPSELL_NUDGE_DISMISS_KEY = 'domain_upsell_nudge_dismiss';
export class SiteNotice extends React.Component {
static propTypes = {
site: PropTypes.object,
};
static defaultProps = {};
getSiteRedirectNotice( site ) {
if ( ! site || this.props.isDomainOnly ) {
return null;
}
if ( ! ( site.options && site.options.is_redirect ) ) {
return null;
}
const { hostname } = getUrlParts( site.URL );
const { translate } = this.props;
return (
<Notice
icon="info-outline"
isCompact
showDismiss={ false }
text={ translate( 'Redirects to {{a}}%(url)s{{/a}}', {
args: { url: hostname },
components: { a: <a href={ site.URL } /> },
} ) }
>
<NoticeAction href={ domainManagementList( site.domain ) }>
{ translate( 'Edit' ) }
</NoticeAction>
</Notice>
);
}
domainCreditNotice() {
if ( ! this.props.hasDomainCredit || ! this.props.canManageOptions ) {
return null;
}
if ( isEnabled( 'signup/wpforteams' ) && this.props.isSiteWPForTeams ) {
return null;
}
const eventName = 'calypso_domain_credit_reminder_impression';
const eventProperties = { cta_name: 'current_site_domain_notice' };
const { translate } = this.props;
const noticeText = preventWidows( translate( 'Free domain available' ) );
const ctaText = translate( 'Claim' );
return (
<UpsellNudge
callToAction={ ctaText }
compact
event={ eventName }
forceHref={ true }
forceDisplay={ true }
href={ `/domains/add/${ this.props.site.slug }` }
title={ noticeText }
tracksClickName="calypso_domain_credit_reminder_click"
tracksClickProperties={ eventProperties }
tracksImpressionName={ eventName }
tracksImpressionProperties={ eventProperties }
/>
);
}
domainUpsellNudge() {
if ( ! this.props.isPlanOwner || this.props.domainUpsellNudgeDismissedDate ) {
return null;
}
if ( this.props.isJetpack ) {
return null;
}
if ( isEnabled( 'signup/wpforteams' ) && this.props.isSiteWPForTeams ) {
return null;
}
const eligibleDomains = reject(
this.props.domains,
( domain ) =>
domain.isWPCOMDomain ||
domain.name.endsWith( '.wpcomstaging.com' ) ||
( domain.registrationDate && moment( domain.registrationDate ).add( 7, 'days' ).isAfter() )
);
if ( eligibleDomains.length !== 1 ) {
return null;
}
const { site, currencyCode, productsList, translate } = this.props;
const priceAndSaleInfo = transform(
productsList,
function ( result, value, key ) {
if ( value.is_domain_registration && value.available ) {
const regularPrice = getUnformattedDomainPrice( key, productsList );
const minRegularPrice = get( result, 'minRegularPrice', regularPrice );
result.minRegularPrice = Math.min( minRegularPrice, regularPrice );
const salePrice = getUnformattedDomainSalePrice( key, productsList );
if ( salePrice ) {
const minSalePrice = get( result, 'minSalePrice', salePrice );
result.minSalePrice = Math.min( minSalePrice, salePrice );
result.saleTlds.push( value.tld );
}
}
},
{ saleTlds: [] }
);
if ( ! priceAndSaleInfo.minSalePrice && ! priceAndSaleInfo.minRegularPrice ) {
return null;
}
let noticeText;
if ( priceAndSaleInfo.minSalePrice ) {
if ( get( priceAndSaleInfo, 'saleTlds.length', 0 ) === 1 ) {
noticeText = translate( 'Get a %(tld)s domain for just %(salePrice)s for a limited time', {
args: {
tld: priceAndSaleInfo.saleTlds[ 0 ],
salePrice: formatCurrency( priceAndSaleInfo.minSalePrice, currencyCode ),
},
} );
} else {
noticeText = translate( 'Domains on sale starting at %(minSalePrice)s', {
args: {
minSalePrice: formatCurrency( priceAndSaleInfo.minSalePrice, currencyCode ),
},
} );
}
} else {
noticeText = translate( 'Add another domain from %(minDomainPrice)s', {
args: {
minDomainPrice: formatCurrency( priceAndSaleInfo.minRegularPrice, currencyCode ),
},
} );
}
return (
<UpsellNudge
callToAction={ translate( 'Add' ) }
compact
href={ `/domains/add/${ site.slug }` }
onDismissClick={ this.props.clickDomainUpsellDismiss }
dismissPreferenceName="calypso_upgrade_nudge_cta_click"
event="calypso_upgrade_nudge_impression"
forceDisplay={ true }
horizontal={ true }
title={ preventWidows( noticeText ) }
tracksClickName="calypso_upgrade_nudge_cta_click"
tracksClickProperties={ { cta_name: 'domain-upsell-nudge' } }
tracksImpressionName="calypso_upgrade_nudge_impression"
tracksImpressionProperties={ { cta_name: 'domain-upsell-nudge' } }
tracksDismissName="calypso_upgrade_nudge_cta_click"
tracksDismissProperties={ { cta_name: 'domain-upsell-nudge-dismiss' } }
/>
);
}
activeDiscountNotice() {
if ( ! this.props.activeDiscount ) {
return null;
}
if ( isEnabled( 'signup/wpforteams' ) && this.props.isSiteWPForTeams ) {
return null;
}
const { site, activeDiscount } = this.props;
const { nudgeText, nudgeEndsTodayText, ctaText, name } = activeDiscount;
const bannerText =
nudgeEndsTodayText && this.promotionEndsToday( activeDiscount )
? nudgeEndsTodayText
: nudgeText;
if ( ! bannerText ) {
return null;
}
const eventProperties = { cta_name: 'active-discount-sidebar' };
return (
<UpsellNudge
event="calypso_upgrade_nudge_impression"
forceDisplay={ true }
tracksClickName="calypso_upgrade_nudge_cta_click"
tracksClickProperties={ eventProperties }
tracksImpressionName="calypso_upgrade_nudge_impression"
tracksImpressionProperties={ eventProperties }
callToAction={ ctaText || 'Upgrade' }
href={ `/plans/${ site.slug }?discount=${ name }` }
title={ bannerText }
/>
);
}
promotionEndsToday( { endsAt } ) {
const now = new Date();
const format = 'YYYYMMDD';
return moment( now ).format( format ) === moment( endsAt ).format( format );
}
render() {
const { site, isMigrationInProgress, messagePath, hasJITM } = this.props;
if ( ! site || isMigrationInProgress ) {
return <div className="current-site__notices" />;
}
const discountOrFreeToPaid = this.activeDiscountNotice();
const siteRedirectNotice = this.getSiteRedirectNotice( site );
const domainCreditNotice = this.domainCreditNotice();
const showJitms =
! ( isEnabled( 'signup/wpforteams' ) && this.props.isSiteWPForTeams ) &&
( discountOrFreeToPaid || config.isEnabled( 'jitms' ) );
return (
<div className="current-site__notices">
<QueryProductsList />
<QueryActivePromotions />
{ siteRedirectNotice }
{ showJitms && (
<AsyncLoad
require="calypso/blocks/jitm"
messagePath={ messagePath }
template="sidebar-banner"
placeholder={ null }
/>
) }
<QuerySitePlans siteId={ site.ID } />
{ ! hasJITM && domainCreditNotice }
{ ! ( hasJITM || discountOrFreeToPaid || domainCreditNotice ) && this.domainUpsellNudge() }
</div>
);
}
}
export default connect(
( state, ownProps ) => {
const siteId = ownProps.site && ownProps.site.ID ? ownProps.site.ID : null;
const sectionName = getSectionName( state );
const messagePath = `calypso:${ sectionName }:sidebar_notice`;
const isMigrationInProgress =
isSiteMigrationInProgress( state, siteId ) || isSiteMigrationActiveRoute( state );
return {
isDomainOnly: isDomainOnlySite( state, siteId ),
isEligibleForFreeToPaidUpsell: isEligibleForFreeToPaidUpsell( state, siteId ),
isJetpack: isJetpackSite( state, siteId ),
activeDiscount: getActiveDiscount( state ),
hasDomainCredit: hasDomainCredit( state, siteId ),
canManageOptions: canCurrentUser( state, siteId, 'manage_options' ),
productsList: getProductsList( state ),
domains: getDomainsBySiteId( state, siteId ),
isPlanOwner: isCurrentUserCurrentPlanOwner( state, siteId ),
currencyCode: getCurrentUserCurrencyCode( state ),
domainUpsellNudgeDismissedDate: getPreference( state, DOMAIN_UPSELL_NUDGE_DISMISS_KEY ),
isSiteWPForTeams: isSiteWPForTeams( state, siteId ),
isMigrationInProgress,
hasJITM: getTopJITM( state, messagePath ),
messagePath,
};
},
( dispatch ) => {
return {
clickClaimDomainNotice: () =>
dispatch(
recordTracksEvent( 'calypso_domain_credit_reminder_click', {
cta_name: 'current_site_domain_notice',
} )
),
clickDomainUpsellGo: () =>
dispatch(
recordTracksEvent( 'calypso_upgrade_nudge_cta_click', {
cta_name: 'domain-upsell-nudge',
} )
),
clickDomainUpsellDismiss: () => {
dispatch( savePreference( DOMAIN_UPSELL_NUDGE_DISMISS_KEY, new Date().toISOString() ) );
},
};
}
)( localize( SiteNotice ) );