-
Notifications
You must be signed in to change notification settings - Fork 2k
/
selectors.js
123 lines (107 loc) · 3.6 KB
/
selectors.js
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
/**
* External dependencies
*/
import { every, filter, find, get, includes, some } from 'lodash';
/**
* Internal dependencies
*/
import createSelector from 'lib/create-selector';
export const isRequesting = function ( state, siteId ) {
// if the `isRequesting` attribute doesn't exist yet,
// we assume we are still launching the fetch action, so it's true
if ( typeof state.plugins.premium.isRequesting[ siteId ] === 'undefined' ) {
return true;
}
return state.plugins.premium.isRequesting[ siteId ];
};
export const hasRequested = function ( state, siteId ) {
if ( typeof state.plugins.premium.hasRequested[ siteId ] === 'undefined' ) {
return false;
}
return state.plugins.premium.hasRequested[ siteId ];
};
/**
* Gets the list of plugins for a site and optionally filters to a single specific
* plugin.
*
* @param {object} state The current state.
* @param {number} siteId The site ID.
* @param {string?} forPlugin Name of a specific plugin to filter for, `false` otherwise to return the full list.
* @returns {Array<object>} The list of plugins.
*/
export const getPluginsForSite = function ( state, siteId, forPlugin = false ) {
const pluginList = state.plugins.premium.plugins[ siteId ];
if ( typeof pluginList === 'undefined' ) {
return [];
}
// patch to solve a bug in jp 4.3 ( https://github.com/Automattic/jetpack/issues/5498 )
if ( forPlugin === 'backups' || forPlugin === 'scan' ) {
forPlugin = 'vaultpress';
}
return filter( pluginList, ( plugin ) => {
// eslint-disable-next-line no-extra-boolean-cast
if ( !! forPlugin ) {
return forPlugin === plugin.slug;
}
return true;
} );
};
export const isStarted = function ( state, siteId, forPlugin = false ) {
const pluginList = getPluginsForSite( state, siteId, forPlugin );
return ! every( pluginList, ( item ) => {
return 'wait' === item.status;
} );
};
export const isFinished = function ( state, siteId, forPlugin = false ) {
const pluginList = getPluginsForSite( state, siteId, forPlugin );
if ( pluginList.length === 0 ) {
return true;
}
return ! some( pluginList, ( item ) => {
return 'done' !== item.status && item.error === null;
} );
};
export const isInstalling = function ( state, siteId, forPlugin = false ) {
const pluginList = getPluginsForSite( state, siteId, forPlugin );
if ( pluginList.length === 0 ) {
return false;
}
// If any plugin is not done/waiting/error'd, it's in an installing state.
return some( pluginList, ( item ) => {
return ! includes( [ 'done', 'wait' ], item.status ) && item.error === null;
} );
};
export const getActivePlugin = function ( state, siteId, forPlugin = false ) {
const pluginList = getPluginsForSite( state, siteId, forPlugin );
const plugin = find( pluginList, ( item ) => {
return ! includes( [ 'done', 'wait' ], item.status ) && item.error === null;
} );
if ( typeof plugin === 'undefined' ) {
return false;
}
return plugin;
};
export const getNextPlugin = function ( state, siteId, forPlugin = false ) {
const pluginList = getPluginsForSite( state, siteId, forPlugin );
const plugin = find( pluginList, ( item ) => {
return 'wait' === item.status && item.error === null;
} );
if ( typeof plugin === 'undefined' ) {
return false;
}
return plugin;
};
export const getPluginKeys = createSelector(
( state, siteId, forPlugin = false ) => {
const pluginList = getPluginsForSite( state, siteId, forPlugin );
return pluginList.reduce( ( keys, plugin ) => {
const key = get( plugin, 'key' );
const slug = get( plugin, 'slug' );
return {
...keys,
[ slug ]: key,
};
}, {} );
},
( state, siteId ) => [ state.plugins.premium.plugins[ siteId ] ]
);