2019-05-17 03:04:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component } from '@wordpress/element';
|
2022-02-17 19:15:11 +00:00
|
|
|
import { filter, isEqual } from 'lodash';
|
2019-05-27 16:37:02 +00:00
|
|
|
import { Stepper } from '@woocommerce/components';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { updateQueryString } from '@woocommerce/navigation';
|
2019-05-27 16:37:02 +00:00
|
|
|
|
2022-02-17 19:15:11 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import UnsavedChangesModal from './unsaved-changes-modal';
|
|
|
|
|
2019-05-17 03:04:52 +00:00
|
|
|
export default class ProfileWizardHeader extends Component {
|
2022-02-17 19:15:11 +00:00
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
this.state = {
|
|
|
|
showUnsavedChangesModal: false,
|
|
|
|
};
|
|
|
|
this.lastClickedStepKey = null;
|
2022-03-17 09:39:50 +00:00
|
|
|
this.onStepClick = this.onStepClick.bind( this );
|
2022-02-17 19:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shouldWarnForUnsavedChanges( step ) {
|
|
|
|
if ( typeof this.props.stepValueChanges[ step ] !== 'undefined' ) {
|
|
|
|
const initialValues = this.props.stepValueChanges[ step ]
|
|
|
|
.initialValues;
|
|
|
|
const currentValues = this.props.stepValueChanges[ step ]
|
|
|
|
.currentValues;
|
|
|
|
|
|
|
|
if (
|
|
|
|
Array.isArray( initialValues ) &&
|
|
|
|
Array.isArray( currentValues )
|
|
|
|
) {
|
|
|
|
initialValues.sort();
|
|
|
|
currentValues.sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ! isEqual( initialValues, currentValues );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
findCurrentStep() {
|
|
|
|
return this.props.steps.find(
|
|
|
|
( s ) => s.key === this.props.currentStep
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
moveToLastClickedStep() {
|
|
|
|
if ( this.lastClickedStepKey ) {
|
|
|
|
updateQueryString( { step: this.lastClickedStepKey } );
|
|
|
|
this.lastClickedStepKey = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
saveCurrentStepChanges() {
|
|
|
|
const currentStep = this.findCurrentStep();
|
|
|
|
if ( ! currentStep ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const stepValueChanges = this.props.stepValueChanges[ currentStep.key ];
|
|
|
|
if ( typeof stepValueChanges.onSave === 'function' ) {
|
|
|
|
stepValueChanges.onSave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:37:02 +00:00
|
|
|
renderStepper() {
|
2019-11-06 00:26:08 +00:00
|
|
|
const { currentStep, steps } = this.props;
|
2020-02-14 02:23:21 +00:00
|
|
|
const visibleSteps = filter( steps, ( step ) => !! step.label );
|
|
|
|
const currentStepIndex = visibleSteps.findIndex(
|
|
|
|
( step ) => step.key === currentStep
|
|
|
|
);
|
2019-11-06 00:26:08 +00:00
|
|
|
|
2022-03-17 09:39:50 +00:00
|
|
|
visibleSteps.forEach( ( step, index ) => {
|
2019-11-06 00:26:08 +00:00
|
|
|
const previousStep = visibleSteps[ index - 1 ];
|
2022-03-17 09:39:50 +00:00
|
|
|
step.isComplete = step.isComplete || index < currentStepIndex;
|
2019-11-06 00:26:08 +00:00
|
|
|
|
2022-03-17 09:39:50 +00:00
|
|
|
const canClickStepLabel =
|
|
|
|
! previousStep || previousStep.isComplete || step.isComplete;
|
2019-11-06 00:26:08 +00:00
|
|
|
|
2022-03-17 09:39:50 +00:00
|
|
|
if ( canClickStepLabel ) {
|
|
|
|
step.onClick = this.onStepClick;
|
2019-11-06 00:26:08 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
return <Stepper steps={ visibleSteps } currentStep={ currentStep } />;
|
2019-05-27 16:37:02 +00:00
|
|
|
}
|
2019-11-06 00:26:08 +00:00
|
|
|
|
2022-03-17 09:39:50 +00:00
|
|
|
onStepClick( key ) {
|
|
|
|
const { currentStep } = this.props;
|
|
|
|
if ( this.shouldWarnForUnsavedChanges( currentStep ) ) {
|
|
|
|
this.setState( { showUnsavedChangesModal: true } );
|
|
|
|
this.lastClickedStepKey = key;
|
|
|
|
} else {
|
|
|
|
updateQueryString( { step: key } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 03:04:52 +00:00
|
|
|
render() {
|
2022-02-17 19:15:11 +00:00
|
|
|
const currentStep = this.findCurrentStep();
|
2020-03-09 23:16:21 +00:00
|
|
|
|
|
|
|
if ( ! currentStep || ! currentStep.label ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return (
|
2020-03-09 23:16:21 +00:00
|
|
|
<div className="woocommerce-profile-wizard__header">
|
2022-02-17 19:15:11 +00:00
|
|
|
{ this.state.showUnsavedChangesModal && (
|
|
|
|
<UnsavedChangesModal
|
|
|
|
onClose={ () => {
|
|
|
|
this.setState( { showUnsavedChangesModal: false } );
|
|
|
|
this.moveToLastClickedStep();
|
|
|
|
} }
|
|
|
|
onSave={ () => {
|
|
|
|
this.saveCurrentStepChanges();
|
|
|
|
this.setState( { showUnsavedChangesModal: false } );
|
|
|
|
this.moveToLastClickedStep();
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
2020-03-09 23:16:21 +00:00
|
|
|
{ this.renderStepper() }
|
2020-02-14 02:23:21 +00:00
|
|
|
</div>
|
|
|
|
);
|
2019-05-17 03:04:52 +00:00
|
|
|
}
|
|
|
|
}
|