File size: 2,882 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
import { withStorageKey } from '@automattic/state-utils';
import {
	SELECTED_SITE_SET,
	SECTION_LOADING_SET,
	NOTIFICATIONS_PANEL_TOGGLE,
	MOST_RECENTLY_SELECTED_SITE_SET,
} from 'calypso/state/action-types';
import { combineReducers, withSchemaValidation } from 'calypso/state/utils';
import actionLog from './action-log/reducer';
import appBannerDismissed from './app-banner-dismissed/reducer';
import appBannerVisibility from './app-banner-visibility/reducer';
import checkout from './checkout/reducer';
import language from './language/reducer';
import layoutFocus from './layout-focus/reducer';
import masterbarVisibility from './masterbar-visibility/reducer';
import mediaModal from './media-modal/reducer';
import postTypeList from './post-type-list/reducer';
import preview from './preview/reducer';
import section from './section/reducer';

/**
 * Tracks the currently selected site ID.
 * @param  {Object} state  Current state
 * @param  {Object} action Action payload
 * @returns {Object}        Updated state
 */
export const selectedSiteId = withSchemaValidation(
	{ type: [ 'number', 'null' ] },
	( state = null, action ) => {
		switch ( action.type ) {
			case SELECTED_SITE_SET:
				return action.siteId || null;
		}

		return state;
	}
);

export const mostRecentlySelectedSiteId = withSchemaValidation(
	{ type: [ 'number', 'null' ] },
	( state = null, action ) => {
		switch ( action.type ) {
			case MOST_RECENTLY_SELECTED_SITE_SET:
				// Don't set nullish values for this. No selection is not a valid previous selection.
				if ( action.siteId ) {
					return action.siteId;
				}
				return state;
		}

		return state;
	}
);

export const siteSelectionInitialized = withSchemaValidation(
	{ type: 'boolean' },
	( state = false, action ) => {
		switch ( action.type ) {
			case SELECTED_SITE_SET:
				return true;
		}

		return state;
	}
);

export function isSectionLoading( state = false, action ) {
	switch ( action.type ) {
		case SECTION_LOADING_SET:
			return action.isSectionLoading;
	}
	return state;
}

/**
 * Tracks if the notifications panel is open
 * @param   {Object} state       Current state
 * @param   {Object} action      Action payload
 * @param   {string} action.type The action type identifier. In this case it's looking for NOTIFICATIONS_PANEL_TOGGLE
 * @returns {Object}             Updated state
 */
export function isNotificationsOpen( state = false, { type } ) {
	if ( type === NOTIFICATIONS_PANEL_TOGGLE ) {
		return ! state;
	}
	return state;
}

const reducer = combineReducers( {
	actionLog,
	appBannerVisibility,
	appBannerDismissed,
	checkout,
	isSectionLoading,
	isNotificationsOpen,
	language,
	layoutFocus,
	masterbarVisibility,
	mediaModal,
	postTypeList,
	preview,
	mostRecentlySelectedSiteId,
	section,
	selectedSiteId,
	siteSelectionInitialized,
} );

export { reducer };

export default withStorageKey( 'ui', reducer );