/
index.tsx
74 lines (63 loc) · 2.16 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
74
import { useSelect, useDispatch } from '@wordpress/data';
import * as React from 'react';
import DomainStep from '../launch-steps/domain-step';
import FinalStep from '../launch-steps/final-step';
import NameStep from '../launch-steps/name-step';
import PlanStep from '../launch-steps/plan-step';
import { LAUNCH_STORE } from '../stores';
import type { LaunchSelect } from '@automattic/data-stores';
interface Props {
onSubmit?: () => void;
}
const Launch: React.FunctionComponent< Props > = ( { onSubmit } ) => {
const { step: currentStep } = useSelect(
( select ) => ( select( LAUNCH_STORE ) as LaunchSelect ).getState(),
[]
);
const LaunchStep = useSelect(
( select ) => ( select( LAUNCH_STORE ) as LaunchSelect ).getLaunchStep(),
[]
);
const LaunchSequence = useSelect(
( select ) => ( select( LAUNCH_STORE ) as LaunchSelect ).getLaunchSequence(),
[]
);
const firstIncompleteStep = useSelect(
( select ) => ( select( LAUNCH_STORE ) as LaunchSelect ).getFirstIncompleteStep(),
[]
);
const { setStep, setSidebarFullscreen, unsetSidebarFullscreen } = 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;
setSidebarFullscreen();
}
setStep( LaunchSequence[ prevSequence ] );
};
const handleNextStep = () => {
const nextSequence = currentSequence + 1;
const maxSequence = LaunchSequence.length - 1;
if ( nextSequence > maxSequence ) {
onSubmit?.();
}
unsetSidebarFullscreen();
setStep( LaunchSequence[ nextSequence ] );
};
const CurrentLaunchStep = LaunchStepComponents[ currentStep ];
React.useEffect( () => {
if ( firstIncompleteStep && firstIncompleteStep !== LaunchStep.Name ) {
setStep( firstIncompleteStep );
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [] );
return <CurrentLaunchStep onPrevStep={ handlePrevStep } onNextStep={ handleNextStep } />;
};
export default Launch;