File size: 5,552 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 treeSelect from '@automattic/tree-select';

import 'calypso/state/invites/init';

/**
 * Returns true if currently requesting invites for the given site, or false
 * otherwise.
 * @param  {Object}  state  Global state tree
 * @param  {number}  siteId Site ID
 * @returns {boolean}        Whether invites are being requested
 */
export function isRequestingInvitesForSite( state, siteId ) {
	return !! state.invites.requesting[ siteId ];
}

/**
 * Returns an array of all pending invite objects known for the given site, or
 * `null` if there is no data for that site.
 * @param  {Object} state  Global state tree
 * @param  {number} siteId Site ID
 * @returns {?Array}        The list of pending invites for the given site
 */
export function getPendingInvitesForSite( state, siteId ) {
	const invites = state.invites.items[ siteId ];
	if ( ! invites ) {
		return null;
	}
	return invites.pending;
}

/**
 * Returns an array of all accepted invite objects known for the given site, or
 * `null` if there is no data for that site.
 * @param  {Object} state  Global state tree
 * @param  {number} siteId Site ID
 * @returns {?Array}        The list of accepted invites for the given site
 */
export function getAcceptedInvitesForSite( state, siteId ) {
	const invites = state.invites.items[ siteId ];
	if ( ! invites ) {
		return null;
	}
	return invites.accepted;
}

/**
 * Returns an array of all invite links for the given site, or
 * `null` if there are none.
 * @param  {Object} state  Global state tree
 * @param  {number} siteId Site ID
 * @returns {?Array}       The list of invite links for the given site
 */
export function getInviteLinksForSite( state, siteId ) {
	const inviteLinks = state.invites.links[ siteId ];
	if ( ! inviteLinks ) {
		return null;
	}
	return inviteLinks;
}

/**
 * Returns the total number of invites found for the given site, or `null`.
 * @param  {Object}  state  Global state tree
 * @param  {number}  siteId Site ID
 * @returns {?number}        The number of invites found for the given site
 */
export function getNumberOfInvitesFoundForSite( state, siteId ) {
	return state.invites.counts[ siteId ] || null;
}

const findInvite = ( invites, inviteId ) => invites.find( ( { key } ) => key === inviteId );
/**
 * Returns an invite object for the given site and invite ID, or `null` if no
 * invite with the given ID exists for the site.
 * @param  {Object}  state    Global state tree
 * @param  {number}  siteId   Site ID
 * @param  {string}  inviteId Invite ID
 * @returns {?Object}          Invite object (if found)
 */
export const getInviteForSite = treeSelect(
	( state, siteId ) => [ state.invites.items[ siteId ] ],
	( [ siteInvites ], siteId, inviteId ) => {
		if ( ! siteInvites ) {
			return null;
		}
		return (
			findInvite( siteInvites.pending, inviteId ) ||
			findInvite( siteInvites.accepted, inviteId ) ||
			null
		);
	}
);

/**
 * Returns true if currently requesting an invite resend for the given site and
 * invite ID, or false otherwise.
 * @param  {Object}  state    Global state tree
 * @param  {number}  siteId   Site ID
 * @param  {string}  inviteId Invite ID
 * @returns {boolean}          Whether invites resend is being requested
 */
export function isRequestingInviteResend( state, siteId, inviteId ) {
	return 'requesting' === state.invites.requestingResend[ siteId ]?.[ inviteId ];
}

/**
 * Returns true if request to resend invite for the given site and
 * invite ID was successful, or false otherwise.
 * @param  {Object}  state    Global state tree
 * @param  {number}  siteId   Site ID
 * @param  {string}  inviteId Invite ID
 * @returns {boolean}          Whether invite resend was a success
 */
export function didInviteResendSucceed( state, siteId, inviteId ) {
	return 'success' === state.invites.requestingResend[ siteId ]?.[ inviteId ];
}

/**
 * Returns true if currently deleting an invite for the given site and
 * invite ID, or false otherwise.
 * @param  {Object}  state    Global state tree
 * @param  {number}  siteId   Site ID
 * @param  {string}  inviteId Invite ID
 * @returns {boolean}          Whether invites resend is being requested
 */
export function isDeletingInvite( state, siteId, inviteId ) {
	return 'requesting' === state.invites.deleting[ siteId ]?.[ inviteId ];
}

/**
 * Returns true if the invite for the given site and invite ID was successfully
 * deleted, or false otherwise.
 * @param  {Object}  state    Global state tree
 * @param  {number}  siteId   Site ID
 * @param  {string}  inviteId Invite ID
 * @returns {boolean}          Whether invites resend is being requested
 */
export function didInviteDeletionSucceed( state, siteId, inviteId ) {
	return 'success' === state.invites.deleting[ siteId ]?.[ inviteId ];
}

/**
 * Returns true if currently deleting any invite for the given site,
 * or false otherwise.
 * @param  {Object}  state    Global state tree
 * @param  {number}  siteId   Site ID
 * @returns {boolean}          Whether an invite is being deleted
 */
export function isDeletingAnyInvite( state, siteId ) {
	const siteInvites = state.invites.deleting[ siteId ];
	if ( ! siteInvites ) {
		return false;
	}
	return Object.values( siteInvites ).includes( 'requesting' );
}

/**
 * Returns token validation object
 * @param {Object} 	state	Global state tree
 */
export function getTokenValidation( state ) {
	return state.invites.validation;
}

/**
 * Returns send invite state object
 * @param {Object} 	state	Global state tree
 */
export function getSendInviteState( state ) {
	return state.invites.inviting;
}