/
selectors.ts
53 lines (46 loc) · 1.69 KB
/
selectors.ts
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
/**
* External dependencies
*/
import { select } from '@wordpress/data';
/**
* Internal dependencies
*/
import { LaunchSequence, LaunchStep } from './data';
import type { State } from './reducer';
import { LaunchStepType } from './types';
export const getLaunchSequence = () => LaunchSequence;
export const getLaunchStep = () => LaunchStep;
export const getState = ( state: State ) => state;
export const hasPaidDomain = ( state: State ): boolean => {
if ( ! state.domain ) {
return false;
}
return ! state.domain.is_free;
};
export const getSelectedDomain = ( state: State ) => state.domain;
export const getSelectedPlan = ( state: State ) => state.plan;
// Completion status of steps is derived from the state of the launch flow
export const isStepCompleted = ( state: State, step: LaunchStepType ) => {
if ( step === LaunchStep.Plan ) {
return !! getSelectedPlan( state );
}
if ( step === LaunchStep.Name ) {
const site = select( 'core' ).getEntityRecord( 'root', 'site', undefined );
return !! site?.title;
}
if ( step === LaunchStep.Domain ) {
return !! getSelectedDomain( state ) || state.confirmedDomainSelection;
}
return false;
};
// Check if all steps are completed except the last one
export const isFlowCompleted = ( state: State ) =>
LaunchSequence.slice( 0, LaunchSequence.length - 1 ).every( ( step ) =>
isStepCompleted( state, step )
);
// Check if at least one step is completed
export const isFlowStarted = ( state: State ) =>
LaunchSequence.some( ( step ) => isStepCompleted( state, step ) );
// Get first incomplete step
export const getFirstIncompleteStep = ( state: State ): LaunchStepType | undefined =>
LaunchSequence.find( ( step ) => ! isStepCompleted( state, step ) );