Skip to content

Commit 51d877c

Browse files
authored
Remove i18n-calypso moment from marketing-survey. (#36786)
* Remove i18n-calypso moment from marketing-survey. It was only used in `enriched-survey-data`, which doesn't need localization. It probably doesn't need `moment` either, but I'm leaving that for future explorations. * Make timestamp the last enrichedSurveyData param. This simplifies code by taking advantage of ES6 optional parameters. * Drop moment and switch to native dates.
1 parent 4b0113d commit 51d877c

File tree

6 files changed

+32
-47
lines changed

6 files changed

+32
-47
lines changed

client/components/marketing-survey/cancel-auto-renewal-form/index.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
/** @format */
2-
31
/**
42
* External dependencies
53
*/
64
import PropTypes from 'prop-types';
75
import React, { Component } from 'react';
86
import { connect } from 'react-redux';
9-
import { localize, moment } from 'i18n-calypso';
7+
import { localize } from 'i18n-calypso';
108

119
/**
1210
* Internal dependencies
@@ -22,6 +20,10 @@ import { submitSurvey } from 'lib/upgrades/actions';
2220
import { isDomainRegistration, isPlan } from 'lib/products-values';
2321
import enrichedSurveyData from 'components/marketing-survey/cancel-purchase-form/enriched-survey-data';
2422
import PrecancellationChatButton from 'components/marketing-survey/cancel-purchase-form/precancellation-chat-button';
23+
24+
/**
25+
* Style dependencies
26+
*/
2527
import './style.scss';
2628

2729
class CancelAutoRenewalForm extends Component {
@@ -91,7 +93,7 @@ class CancelAutoRenewalForm extends Component {
9193
submitSurvey(
9294
'calypso-cancel-auto-renewal',
9395
selectedSite.ID,
94-
enrichedSurveyData( surveyData, moment(), selectedSite, purchase )
96+
enrichedSurveyData( surveyData, selectedSite, purchase )
9597
);
9698

9799
this.props.onClose();
Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
/** @format */
2-
31
/**
42
* External dependencies
53
*/
6-
74
import { get } from 'lodash';
85

9-
export default function enrichedSurveyData( surveyData, moment, site, purchase ) {
6+
const DAY_IN_MS = 1000 * 60 * 60 * 24;
7+
8+
export default function enrichedSurveyData( surveyData, site, purchase, timestamp = new Date() ) {
109
const purchaseStartDate = get( purchase, 'subscribedDate', null );
1110
const siteStartDate = get( site, 'options.created_at', null );
1211
const purchaseId = get( purchase, 'id', null );
1312
const productSlug = get( purchase, 'productSlug', null );
1413

15-
return Object.assign(
16-
{
17-
purchase: productSlug,
18-
purchaseId,
19-
},
20-
purchaseStartDate && {
21-
daysSincePurchase: moment.diff( purchaseStartDate, 'days', true ),
22-
},
23-
siteStartDate && {
24-
daysSinceSiteCreation: moment.diff( siteStartDate, 'days', true ),
25-
},
26-
surveyData
27-
);
14+
return {
15+
purchase: productSlug,
16+
purchaseId,
17+
...( purchaseStartDate && {
18+
daysSincePurchase: ( new Date( timestamp ) - new Date( purchaseStartDate ) ) / DAY_IN_MS,
19+
} ),
20+
...( siteStartDate && {
21+
daysSinceSiteCreation: ( new Date( timestamp ) - new Date( siteStartDate ) ) / DAY_IN_MS,
22+
} ),
23+
...surveyData,
24+
};
2825
}

client/components/marketing-survey/cancel-purchase-form/index.jsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
/** @format */
2-
31
/**
42
* External dependencies
53
*/
6-
74
import PropTypes from 'prop-types';
85
import React from 'react';
96
import { shuffle } from 'lodash';
107
import { connect } from 'react-redux';
11-
import { localize, moment } from 'i18n-calypso';
8+
import { localize } from 'i18n-calypso';
129

1310
/**
1411
* Internal Dependencies
@@ -251,7 +248,7 @@ class CancelPurchaseForm extends React.Component {
251248
submitSurvey(
252249
'calypso-remove-purchase',
253250
selectedSite.ID,
254-
enrichedSurveyData( surveyData, moment(), selectedSite, purchase )
251+
enrichedSurveyData( surveyData, selectedSite, purchase )
255252
).then( () => {
256253
this.setState( {
257254
isSubmitting: false,
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/**
2-
* External dependencies
3-
*/
4-
import { expect } from 'chai';
5-
import moment from 'moment';
6-
71
/**
82
* Internal dependencies
93
*/
@@ -13,7 +7,7 @@ jest.mock( 'lib/analytics', () => ( {} ) );
137

148
describe( 'enrichedSurveyData', () => {
159
test( 'should duplicate survey data if no site or purchase are provided', () => {
16-
expect( enrichedSurveyData( { key: 'value' }, moment() ) ).to.deep.equal( {
10+
expect( enrichedSurveyData( { key: 'value' } ) ).toEqual( {
1711
key: 'value',
1812
purchase: null,
1913
purchaseId: null,
@@ -23,7 +17,7 @@ describe( 'enrichedSurveyData', () => {
2317
test( 'should add purchase id and slug to survey data if purchase is provided', () => {
2418
const site = null;
2519
const purchase = { id: 'purchase id', productSlug: 'product slug' };
26-
expect( enrichedSurveyData( { key: 'value' }, moment(), site, purchase ).purchase ).to.equal(
20+
expect( enrichedSurveyData( { key: 'value' }, site, purchase ).purchase ).toEqual(
2721
'product slug'
2822
);
2923
} );
@@ -32,9 +26,8 @@ describe( 'enrichedSurveyData', () => {
3226
const site = null;
3327
const purchase = { subscribedDate: '2017-01-09T03:00:00+00:00' };
3428
expect(
35-
enrichedSurveyData( {}, moment( '2017-01-19T03:00:00+00:00' ), site, purchase )
36-
.daysSincePurchase
37-
).to.equal( 10 );
29+
enrichedSurveyData( {}, site, purchase, '2017-01-19T03:00:00+00:00' ).daysSincePurchase
30+
).toEqual( 10 );
3831
} );
3932

4033
test( 'should add daysSinceSiteCreation to survey data when site.options.created_at is provided', () => {
@@ -43,8 +36,7 @@ describe( 'enrichedSurveyData', () => {
4336
};
4437
const purchase = null;
4538
expect(
46-
enrichedSurveyData( {}, moment( '2017-01-19T03:00:00+00:00' ), site, purchase )
47-
.daysSinceSiteCreation
48-
).to.equal( 10 );
39+
enrichedSurveyData( {}, site, purchase, '2017-01-19T03:00:00+00:00' ).daysSinceSiteCreation
40+
).toEqual( 10 );
4941
} );
5042
} );

client/components/marketing-survey/gsuite-cancel-purchase-dialog/index.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
/** @format */
2-
31
/**
42
* External dependencies
53
*/
64
import { connect } from 'react-redux';
7-
import { localize, moment } from 'i18n-calypso';
5+
import { localize } from 'i18n-calypso';
86
import page from 'page';
97
import PropTypes from 'prop-types';
108
import React, { Component } from 'react';
@@ -106,7 +104,7 @@ class GSuiteCancelPurchaseDialog extends Component {
106104
},
107105
type: 'remove',
108106
};
109-
survey.addResponses( enrichedSurveyData( surveyData, moment(), site, purchase ) );
107+
survey.addResponses( enrichedSurveyData( surveyData, site, purchase ) );
110108

111109
const response = await survey.submit();
112110
if ( ! response.success ) {

client/my-sites/site-settings/disconnect-site/confirm.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class ConfirmDisconnection extends PureComponent {
2626
reason: PropTypes.string,
2727
text: PropTypes.oneOfType( [ PropTypes.string, PropTypes.arrayOf( PropTypes.string ) ] ),
2828
// Provided by HOCs
29-
moment: PropTypes.func,
3029
purchase: PropTypes.object,
3130
siteId: PropTypes.number,
3231
siteSlug: PropTypes.string,
@@ -44,7 +43,7 @@ class ConfirmDisconnection extends PureComponent {
4443
];
4544

4645
submitSurvey = () => {
47-
const { moment, purchase, reason, site, siteId, text } = this.props;
46+
const { purchase, reason, site, siteId, text } = this.props;
4847

4948
const surveyData = {
5049
'why-cancel': {
@@ -59,7 +58,7 @@ class ConfirmDisconnection extends PureComponent {
5958
submitSurvey(
6059
'calypso-disconnect-jetpack-july2019',
6160
siteId,
62-
enrichedSurveyData( surveyData, moment(), site, purchase )
61+
enrichedSurveyData( surveyData, site, purchase )
6362
);
6463
};
6564

0 commit comments

Comments
 (0)