File size: 5,779 Bytes
1e92f2d |
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 |
import { recordTracksEvent } from '@automattic/calypso-analytics';
import { Card } from '@automattic/components';
import { ToggleControl } from '@wordpress/components';
import clsx from 'clsx';
import cookie from 'cookie';
import { localize } from 'i18n-calypso';
import { flowRight as compose } from 'lodash';
import { Component } from 'react';
import ReactDOM from 'react-dom';
import { connect } from 'react-redux';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import Main from 'calypso/components/main';
import NavigationHeader from 'calypso/components/navigation-header';
import PageViewTracker from 'calypso/lib/analytics/page-view-tracker';
import twoStepAuthorization from 'calypso/lib/two-step-authorization';
import withFormBase from 'calypso/me/form-base/with-form-base';
import ReauthRequired from 'calypso/me/reauth-required';
import { getCurrentUser } from 'calypso/state/current-user/selectors';
import { successNotice, removeNotice } from 'calypso/state/notices/actions';
import { isFetchingUserSettings } from 'calypso/state/user-settings/selectors';
import { DeveloperSurveyNotice } from './dev-survey-notice';
import { DeveloperFeatures } from './features/index';
import { getIAmDeveloperCopy } from './get-i-am-a-developer-copy';
import './style.scss';
class Developer extends Component {
surveyNoticeWrapper = null;
constructor( props ) {
super( props );
this.state = {
isDeveloperSurveyNoticeVisible: false,
};
}
setDeveloperSurveyCookie = ( value, maxAge ) => {
document.cookie = cookie.serialize( 'developer_survey', value, {
path: '/',
maxAge,
} );
};
shouldShowDeveloperSurveyNotice = ( isDeveloperAccount ) => {
const cookies = cookie.parse( document.cookie );
const isDevAccountEnabled = isDeveloperAccount ?? this.props.getSetting( 'is_dev_account' );
return (
isDevAccountEnabled && ! [ 'completed', 'dismissed' ].includes( cookies.developer_survey )
);
};
showDeveloperSurveyNotice = () => {
if ( ! this.surveyNoticeWrapper ) {
this.surveyNoticeWrapper = document.createElement( 'div' );
document.body.appendChild( this.surveyNoticeWrapper );
}
this.setState( { isDeveloperSurveyNoticeVisible: true } );
recordTracksEvent( 'calypso_me_developer_survey_impression' );
};
hideDeveloperSurveyNotice = () => {
this.setState( { isDeveloperSurveyNoticeVisible: false } );
};
handleToggleIsDevAccount = ( isDeveloperAccount ) => {
this.props.setUserSetting( 'is_dev_account', isDeveloperAccount );
recordTracksEvent( 'calypso_me_is_dev_account_toggled', {
enabled: isDeveloperAccount ? 1 : 0,
} );
if ( this.shouldShowDeveloperSurveyNotice( isDeveloperAccount ) ) {
this.showDeveloperSurveyNotice();
} else {
this.hideDeveloperSurveyNotice();
}
setTimeout( () => this.props.removeNotice( 'save-user-settings' ), 3000 );
};
componentDidMount() {
const urlParams = new URLSearchParams( window.location.search );
if ( urlParams.get( 'survey' ) === 'completed' ) {
this.setDeveloperSurveyCookie( 'completed', 365 * 24 * 60 * 60 ); // 1 years
this.props.successNotice( this.props.translate( 'Thank you for your feedback!' ), {
duration: 3000,
} );
}
}
componentWillUnmount() {
this.surveyNoticeWrapper && document.body.removeChild( this.surveyNoticeWrapper );
}
componentDidUpdate( prevProps ) {
const isJustLoadedUserSettings =
prevProps.isFetchingUserSettings && ! this.props.isFetchingUserSettings;
if ( isJustLoadedUserSettings && this.shouldShowDeveloperSurveyNotice() ) {
this.showDeveloperSurveyNotice();
}
}
render() {
return (
<>
<Main className="developer" wideLayout>
<PageViewTracker path="/me/developer" title="Me > Developer" />
<ReauthRequired twoStepAuthorization={ twoStepAuthorization } />
<NavigationHeader
navigationItems={ [] }
title={ this.props.translate( 'Developer Features' ) }
subtitle={ this.props.translate(
'Take WordPress.com further with early access to new developer features.'
) }
className="developer__header"
/>
<div className="developer-is-dev-account">
<Card
className={ clsx( 'developer__is-dev-account-card', {
'is-loading': this.props.isFetchingUserSettings,
} ) }
>
<form onChange={ this.props.submitForm }>
<FormFieldset>
<ToggleControl
disabled={
this.props.isFetchingUserSettings || this.props.isUpdatingUserSettings
}
checked={ this.props.getSetting( 'is_dev_account' ) }
onChange={ this.handleToggleIsDevAccount }
label={ getIAmDeveloperCopy( this.props.translate ) }
__nextHasNoMarginBottom
/>
</FormFieldset>
</form>
</Card>
</div>
<DeveloperFeatures />
</Main>
{ this.state.isDeveloperSurveyNoticeVisible &&
ReactDOM.createPortal(
<DeveloperSurveyNotice
localeSlug={ this.props.currentUser.localeSlug }
onSurveyClick={ () => {
recordTracksEvent( 'calypso_me_developer_survey_clicked' );
this.hideDeveloperSurveyNotice();
} }
onClose={ ( remindTimeInSeconds, buttonName ) => {
recordTracksEvent( 'calypso_me_developer_survey_dismissed', {
context: buttonName,
} );
this.setDeveloperSurveyCookie( 'dismissed', remindTimeInSeconds );
this.hideDeveloperSurveyNotice();
} }
/>,
this.surveyNoticeWrapper
) }
</>
);
}
}
export default compose(
connect(
( state ) => ( {
isFetchingUserSettings: isFetchingUserSettings( state ),
currentUser: getCurrentUser( state ),
} ),
{
successNotice,
removeNotice,
}
),
localize,
withFormBase
)( Developer );
|