File size: 3,470 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { withStorageKey } from '@automattic/state-utils';
import {
	HOSTING_GEO_AFFINITY_REQUEST,
	HOSTING_GEO_AFFINITY_SET,
	HOSTING_PHP_VERSION_SET,
	HOSTING_WP_VERSION_REQUEST,
	HOSTING_WP_VERSION_SET,
	HOSTING_SFTP_USER_UPDATE,
	HOSTING_SFTP_USERS_SET,
	HOSTING_SSH_ACCESS_SET,
	HOSTING_STATIC_FILE_404_SET,
	HOSTING_CLEAR_CACHE_REQUEST,
	HOSTING_SFTP_USERS_REQUEST,
	HOSTING_SSH_ACCESS_REQUEST,
	HOSTING_CLEAR_EDGE_CACHE_SUCCESS,
} from 'calypso/state/action-types';
import {
	combineReducers,
	keyedReducer,
	withPersistence,
	withSchemaValidation,
} from 'calypso/state/utils';

export const sftpUsers = ( state = {}, { type, users } ) => {
	if ( type === HOSTING_SFTP_USERS_SET ) {
		return users;
	}

	if ( type === HOSTING_SFTP_USER_UPDATE && Array.isArray( state ) ) {
		return state.map( ( user ) => {
			const updatedUser = users.find( ( u ) => u.username === user.username );
			return {
				...user,
				...updatedUser,
			};
		} );
	}

	return state;
};

export const isLoadingSftpUsers = ( state = null, { type } ) => {
	switch ( type ) {
		case HOSTING_SFTP_USERS_REQUEST:
			return true;
		case HOSTING_SFTP_USERS_SET:
			return false;
	}

	return state;
};

const geoAffinity = ( state = null, { type, setting } ) => {
	switch ( type ) {
		case HOSTING_GEO_AFFINITY_SET:
			return setting;
	}

	return state;
};

const isFetchingGeoAffinity = ( state = null, { type } ) => {
	switch ( type ) {
		case HOSTING_GEO_AFFINITY_REQUEST:
			return true;
		case HOSTING_GEO_AFFINITY_SET:
			return false;
	}

	return state;
};

const phpVersion = ( state = null, { type, version } ) => {
	switch ( type ) {
		case HOSTING_PHP_VERSION_SET:
			return version;
	}

	return state;
};

const isFetchingWpVersion = ( state = false, { type } ) => {
	switch ( type ) {
		case HOSTING_WP_VERSION_REQUEST:
			return true;
		case HOSTING_WP_VERSION_SET:
			return false;
	}

	return state;
};

const wpVersion = ( state = null, { type, version } ) => {
	switch ( type ) {
		case HOSTING_WP_VERSION_SET:
			return version;
	}

	return state;
};

const sshAccess = ( state = null, { type, status } ) => {
	switch ( type ) {
		case HOSTING_SSH_ACCESS_SET:
			return status;
	}

	return state;
};

const isLoadingSshAccess = ( state = null, { type } ) => {
	switch ( type ) {
		case HOSTING_SSH_ACCESS_REQUEST:
			return true;
		case HOSTING_SSH_ACCESS_SET:
			return false;
	}

	return state;
};

const staticFile404 = ( state = null, { type, setting } ) => {
	switch ( type ) {
		case HOSTING_STATIC_FILE_404_SET:
			return setting;
	}

	return state;
};

export const lastCacheClearTimestamp = withSchemaValidation(
	{ type: 'integer' },
	withPersistence( ( state = null, { type } ) => {
		switch ( type ) {
			case HOSTING_CLEAR_CACHE_REQUEST:
				return new Date().valueOf();
		}

		return state;
	} )
);

export const lastEdgeCacheClearTimestamp = withSchemaValidation(
	{ type: 'integer' },
	withPersistence( ( state = null, { type } ) => {
		switch ( type ) {
			case HOSTING_CLEAR_EDGE_CACHE_SUCCESS:
				return new Date().valueOf();
		}

		return state;
	} )
);

const atomicHostingReducer = combineReducers( {
	geoAffinity,
	isFetchingGeoAffinity,
	phpVersion,
	sftpUsers,
	isLoadingSftpUsers,
	sshAccess,
	isLoadingSshAccess,
	staticFile404,
	isFetchingWpVersion,
	wpVersion,
	lastCacheClearTimestamp,
	lastEdgeCacheClearTimestamp,
} );

const reducer = keyedReducer( 'siteId', atomicHostingReducer );
export default withStorageKey( 'atomicHosting', reducer );