Refactor welcome wizard controller to function component

[MAILPOET-1916]
This commit is contained in:
Rostislav Wolny
2019-03-20 15:24:22 +01:00
committed by M. Shull
parent 496103da5e
commit 3ddf14213d

View File

@@ -1,5 +1,5 @@
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import React from 'react'; import React, { useState, useEffect } from 'react';
import MailPoet from 'mailpoet'; import MailPoet from 'mailpoet';
import WelcomeWizardHeader from './header.jsx'; import WelcomeWizardHeader from './header.jsx';
import WelcomeWizardSenderStep from './steps/sender_step.jsx'; import WelcomeWizardSenderStep from './steps/sender_step.jsx';
@@ -8,57 +8,42 @@ import WelcomeWizardEmailCourseStep from './steps/email_course_step.jsx';
import WelcomeWizardUsageTrackingStep from './steps/usage_tracking_step.jsx'; import WelcomeWizardUsageTrackingStep from './steps/usage_tracking_step.jsx';
import WelcomeWizardWooCommerceStep from './steps/woo_commerce_step.jsx'; import WelcomeWizardWooCommerceStep from './steps/woo_commerce_step.jsx';
class WelcomeWizardStepsController extends React.Component { const WelcomeWizardStepsController = (props) => {
constructor(props) { const [stepsCount] = useState(window.is_woocommerce_active ? 4 : 3);
super(props); const [shouldSetSender] = useState(!window.is_mp2_migration_complete);
const [loading, setLoading] = useState(false);
const [sender, setSender] = useState(window.sender_data);
const [replyTo, setReplyTo] = useState(window.reply_to_data);
this.state = { useEffect(() => {
stepsCount: window.is_woocommerce_active ? 4 : 3, const step = parseInt(props.match.params.step, 10);
shouldSetSender: !window.is_mp2_migration_complete, if (step > stepsCount || step < 1) {
loading: false, props.history.push('/steps/1');
sender: window.sender_data,
replyTo: window.reply_to_data,
};
this.finishWizard = this.finishWizard.bind(this);
this.updateSettings = this.updateSettings.bind(this);
this.activateTracking = this.activateTracking.bind(this);
this.updateSender = this.updateSender.bind(this);
this.updateReplyTo = this.updateReplyTo.bind(this);
this.submitSender = this.submitSender.bind(this);
this.showWooCommerceStepOrFinish = this.showWooCommerceStepOrFinish.bind(this);
this.componentDidUpdate();
} }
});
componentDidUpdate() { function finishWizard() {
const step = parseInt(this.props.match.params.step, 10); setLoading(true);
if (step > this.state.stepsCount || step < 1) {
this.props.history.push('/steps/1');
}
}
finishWizard() {
this.setState({ loading: true });
window.location = window.finish_wizard_url; window.location = window.finish_wizard_url;
} }
showWooCommerceStepOrFinish() { function showWooCommerceStepOrFinish() {
if (this.state.stepsCount === 4) { if (stepsCount === 4) {
this.props.history.push('/steps/4'); props.history.push('/steps/4');
} else { } else {
this.finishWizard(); finishWizard();
} }
} }
updateSettings(data) { function updateSettings(data) {
this.setState({ loading: true }); setLoading(true);
return MailPoet.Ajax.post({ return MailPoet.Ajax.post({
api_version: window.mailpoet_api_version, api_version: window.mailpoet_api_version,
endpoint: 'settings', endpoint: 'settings',
action: 'set', action: 'set',
data, data,
}).then(() => this.setState({ loading: false })).fail((response) => { }).then(() => setLoading(false)).fail((response) => {
this.setState({ loading: false }); setLoading(false);
if (response.errors.length > 0) { if (response.errors.length > 0) {
MailPoet.Notice.error( MailPoet.Notice.error(
response.errors.map(error => error.message), response.errors.map(error => error.message),
@@ -68,57 +53,53 @@ class WelcomeWizardStepsController extends React.Component {
}); });
} }
activateTracking() { function activateTracking() {
this.updateSettings({ analytics: { enabled: true } }).then(() => ( updateSettings({ analytics: { enabled: true } }).then(() => (
this.showWooCommerceStepOrFinish())); showWooCommerceStepOrFinish()));
} }
updateSender(data) { function updateSender(data) {
this.setState(prevState => ({ setSender({ ...sender, ...data });
sender: { ...prevState.sender, ...data },
}));
} }
updateReplyTo(data) { function updateReplyTo(data) {
this.setState(prevState => ({ setReplyTo({ ...replyTo, ...data });
replyTo: { ...prevState.replyTo, ...data },
}));
} }
submitSender() { function submitSender() {
this.updateSettings({ updateSettings({
sender: this.state.sender, sender,
reply_to: this.state.replyTo, reply_to: replyTo,
}).then(() => (this.props.history.push('/steps/2'))); }).then(() => (props.history.push('/steps/2')));
} }
render() { const step = parseInt(props.match.params.step, 10);
const step = parseInt(this.props.match.params.step, 10);
return ( return (
<div className="mailpoet_welcome_wizard_steps mailpoet_welcome_wizard_centered_column"> <div className="mailpoet_welcome_wizard_steps mailpoet_welcome_wizard_centered_column">
<WelcomeWizardHeader <WelcomeWizardHeader
current_step={step} current_step={step}
steps_count={this.state.stepsCount} steps_count={stepsCount}
logo_src={window.mailpoet_logo_url} logo_src={window.mailpoet_logo_url}
/> />
{ step === 1 && this.state.shouldSetSender { step === 1 && shouldSetSender
? ( ? (
<WelcomeWizardSenderStep <WelcomeWizardSenderStep
update_sender={this.updateSender} update_sender={updateSender}
submit_sender={this.submitSender} submit_sender={submitSender}
update_reply_to={this.updateReplyTo} update_reply_to={updateReplyTo}
finish={this.finishWizard} finish={finishWizard}
loading={this.state.loading} loading={loading}
sender={this.state.sender} sender={sender}
reply_to={this.state.replyTo} reply_to={replyTo}
/> />
) : null ) : null
} }
{ step === 1 && !this.state.shouldSetSender { step === 1 && !shouldSetSender
? ( ? (
<WelcomeWizardMigratedUserStep <WelcomeWizardMigratedUserStep
next={() => this.props.history.push('/steps/2')} next={() => props.history.push('/steps/2')}
/> />
) : null ) : null
} }
@@ -126,7 +107,7 @@ class WelcomeWizardStepsController extends React.Component {
{ step === 2 { step === 2
? ( ? (
<WelcomeWizardEmailCourseStep <WelcomeWizardEmailCourseStep
next={() => this.props.history.push('/steps/3')} next={() => props.history.push('/steps/3')}
illustration_url={window.email_course_illustration} illustration_url={window.email_course_illustration}
/> />
) : null ) : null
@@ -135,11 +116,11 @@ class WelcomeWizardStepsController extends React.Component {
{ step === 3 { step === 3
? ( ? (
<WelcomeWizardUsageTrackingStep <WelcomeWizardUsageTrackingStep
skip_action={this.showWooCommerceStepOrFinish} skip_action={showWooCommerceStepOrFinish}
allow_action={this.activateTracking} allow_action={activateTracking}
allow_text={this.state.stepsCount === 4 allow_text={stepsCount === 4
? MailPoet.I18n.t('allowAndContinue') : MailPoet.I18n.t('allowAndFinish')} ? MailPoet.I18n.t('allowAndContinue') : MailPoet.I18n.t('allowAndFinish')}
loading={this.state.loading} loading={loading}
/> />
) : null ) : null
} }
@@ -147,16 +128,15 @@ class WelcomeWizardStepsController extends React.Component {
{ step === 4 { step === 4
? ( ? (
<WelcomeWizardWooCommerceStep <WelcomeWizardWooCommerceStep
next={this.finishWizard} next={finishWizard}
screenshot_src={window.woocommerce_screenshot_url} screenshot_src={window.woocommerce_screenshot_url}
loading={this.state.loading} loading={loading}
/> />
) : null ) : null
} }
</div> </div>
); );
} };
}
WelcomeWizardStepsController.propTypes = { WelcomeWizardStepsController.propTypes = {
match: PropTypes.shape({ match: PropTypes.shape({