/
index.tsx
73 lines (60 loc) · 1.97 KB
/
index.tsx
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
/**
* External dependencies
*/
import * as React from 'react';
import { useSelect, useDispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import NameStep from '../launch-steps/name-step';
import DomainStep from '../launch-steps/domain-step';
import PlanStep from '../launch-steps/plan-step';
import FinalStep from '../launch-steps/final-step';
import { LAUNCH_STORE } from '../stores';
import './styles.scss';
interface Props {
onSubmit?: () => void;
}
const Launch: React.FunctionComponent< Props > = ( { onSubmit } ) => {
const { step: currentStep } = useSelect( ( select ) => select( LAUNCH_STORE ).getState() );
const LaunchStep = useSelect( ( select ) => select( LAUNCH_STORE ).getLaunchStep() );
const LaunchSequence = useSelect( ( select ) => select( LAUNCH_STORE ).getLaunchSequence() );
const firstIncompleteStep = useSelect( ( select ) =>
select( LAUNCH_STORE ).getFirstIncompleteStep()
);
const { setStep } = useDispatch( LAUNCH_STORE );
const LaunchStepComponents = {
[ LaunchStep.Name ]: NameStep,
[ LaunchStep.Domain ]: DomainStep,
[ LaunchStep.Plan ]: PlanStep,
[ LaunchStep.Final ]: FinalStep,
};
const currentSequence = LaunchSequence.indexOf( currentStep );
const handlePrevStep = () => {
let prevSequence = currentSequence - 1;
if ( prevSequence < 0 ) {
prevSequence = 0;
}
setStep( LaunchSequence[ prevSequence ] );
};
const handleNextStep = () => {
const nextSequence = currentSequence + 1;
const maxSequence = LaunchSequence.length - 1;
if ( nextSequence > maxSequence ) {
onSubmit?.();
}
setStep( LaunchSequence[ nextSequence ] );
};
const CurrentLaunchStep = LaunchStepComponents[ currentStep ];
React.useEffect( () => {
if ( firstIncompleteStep && firstIncompleteStep !== LaunchStep.Name ) {
setStep( firstIncompleteStep );
}
}, [] );
return (
<div className="nux-launch">
<CurrentLaunchStep onPrevStep={ handlePrevStep } onNextStep={ handleNextStep } />
</div>
);
};
export default Launch;