File size: 1,454 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { combineReducers } from '@wordpress/data';
import type { PlanAction } from './actions';
import type { Plan, PlanFeature, FeaturesByType, PlanProduct } from './types';
import type { Reducer } from 'redux';

// create a Locale type just for code readability
type Locale = string;

export const features: Reducer< Record< Locale, Record< string, PlanFeature > >, PlanAction > = (
	state = {},
	action
) => {
	switch ( action.type ) {
		case 'SET_FEATURES':
			return { ...state, [ action.locale ]: action.features };
		default:
			return state;
	}
};

export const featuresByType: Reducer< Record< Locale, Array< FeaturesByType > >, PlanAction > = (
	state = {},
	action
) => {
	switch ( action.type ) {
		case 'SET_FEATURES_BY_TYPE':
			return { ...state, [ action.locale ]: action.featuresByType };
		default:
			return state;
	}
};

export const plans: Reducer< Record< Locale, Plan[] >, PlanAction > = ( state = {}, action ) => {
	switch ( action.type ) {
		case 'SET_PLANS':
			return { ...state, [ action.locale ]: action.plans };
		default:
			return state;
	}
};

export const planProducts: Reducer< PlanProduct[], PlanAction > = ( state = [], action ) => {
	switch ( action.type ) {
		case 'SET_PLAN_PRODUCTS':
			return action.products;
		default:
			return state;
	}
};

const reducer = combineReducers( {
	features,
	featuresByType,
	planProducts,
	plans,
} );

export type State = ReturnType< typeof reducer >;

export default reducer;