File size: 4,144 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { withStorageKey } from '@automattic/state-utils';
import { Reducer, AnyAction } from 'redux';
import { combineReducers } from 'calypso/state/utils';
import { AppState } from 'calypso/types';
import {
	JETPACK_AGENCY_DASHBOARD_PURCHASED_LICENSE_CHANGE,
	JETPACK_AGENCY_DASHBOARD_SELECT_LICENSE,
	JETPACK_AGENCY_DASHBOARD_UNSELECT_LICENSE,
	JETPACK_AGENCY_DASHBOARD_RESET_SITE,
	JETPACK_AGENCY_DASHBOARD_SITE_MONITOR_STATUS_CHANGE,
	JETPACK_AGENCY_DASHBOARD_SELECT_SITE_LICENSE,
	JETPACK_AGENCY_DASHBOARD_UNSELECT_SITE_LICENSE,
	JETPACK_AGENCY_DASHBOARD_RESET_SITE_LICENSES,
} from './action-types';
import type {
	PurchasedProductsInfo,
	SiteMonitorStatus,
} from 'calypso/jetpack-cloud/sections/agency-dashboard/sites-overview/types';

const purchasedLicense: Reducer<
	{ purchasedLicenseInfo: PurchasedProductsInfo | null },
	AnyAction
> = ( state = { purchasedLicenseInfo: null }, action: AnyAction ): AppState => {
	switch ( action?.type ) {
		case JETPACK_AGENCY_DASHBOARD_PURCHASED_LICENSE_CHANGE:
			return { ...state, purchasedLicenseInfo: action.payload };
	}
	return state;
};

const siteMonitorStatus: Reducer< { statuses: SiteMonitorStatus }, AnyAction > = (
	state = { statuses: {} },
	action: AnyAction
): AppState => {
	switch ( action?.type ) {
		case JETPACK_AGENCY_DASHBOARD_SITE_MONITOR_STATUS_CHANGE:
			return { ...state, statuses: { ...state.statuses, [ action.siteId ]: action.status } };
	}
	return state;
};

const selectedLicenses: Reducer<
	{ siteId: null | number; licenses: Array< string > },
	AnyAction
> = ( state = { siteId: null, licenses: [] }, action: AnyAction ): AppState => {
	switch ( action?.type ) {
		case JETPACK_AGENCY_DASHBOARD_SELECT_LICENSE:
			return {
				...state,
				siteId: action.siteId,
				licenses:
					action.siteId !== state.siteId
						? // If the site is different, reset existing licenses.
						  [ action.license ]
						: // Otherwise, append the license to the list.
						  [ ...state.licenses, action.license ],
			};
		case JETPACK_AGENCY_DASHBOARD_UNSELECT_LICENSE: {
			const filtered = state.licenses.filter( ( license ) => license !== action.license );

			return {
				...state,
				// Reset the selected site in case there are no selected licenses.
				siteId: filtered.length === 0 ? null : action.siteId,
				licenses: filtered,
			};
		}
		case JETPACK_AGENCY_DASHBOARD_RESET_SITE:
			return {
				...state,
				siteId: null,
				licenses: [],
			};
	}
	return state;
};

type License = { siteId: number; products: Array< string > };

const addLicense = ( state: AppState, siteId: number, license: string ) => {
	const foundSite = state.licenses.find(
		( licenceItem: License ) => licenceItem.siteId === siteId
	);
	if ( ! foundSite ) {
		return [ ...state.licenses, { siteId, products: [ license ] } ];
	}
	return state.licenses.map( ( licenceItem: License ) =>
		licenceItem.siteId === siteId
			? { ...licenceItem, products: Array.from( new Set( [ ...licenceItem.products, license ] ) ) }
			: licenceItem
	);
};

const removeLicense = ( state: AppState, siteId: number, license: string ) => {
	return state.licenses.map( ( licenceItem: License ) =>
		licenceItem.siteId === siteId
			? {
					...licenceItem,
					products: licenceItem.products.filter( ( product: string ) => product !== license ),
			  }
			: licenceItem
	);
};

const selectedSiteLicenses: Reducer<
	{ licenses: Array< { siteId: number; products: Array< string > } > },
	AnyAction
> = ( state = { licenses: [] }, action: AnyAction ): AppState => {
	switch ( action?.type ) {
		case JETPACK_AGENCY_DASHBOARD_SELECT_SITE_LICENSE:
			return { ...state, licenses: addLicense( state, action.siteId, action.license ) };
		case JETPACK_AGENCY_DASHBOARD_UNSELECT_SITE_LICENSE:
			return { ...state, licenses: removeLicense( state, action.siteId, action.license ) };
		case JETPACK_AGENCY_DASHBOARD_RESET_SITE_LICENSES:
			return { ...state, licenses: [] };
		default:
			return state;
	}
};

const combinedReducer = combineReducers( {
	purchasedLicense,
	selectedLicenses,
	siteMonitorStatus,
	selectedSiteLicenses,
} );

export default withStorageKey( 'agencyDashboard', combinedReducer );