File size: 8,235 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
import config from '@automattic/calypso-config';
import { translate } from 'i18n-calypso';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { getLocaleSlug } from 'calypso/lib/i18n-utils';
import wpcom from 'calypso/lib/wp';
import { acceptedNotice } from 'calypso/my-sites/invites/utils';
import {
INVITES_DELETE_REQUEST,
INVITES_DELETE_REQUEST_FAILURE,
INVITES_DELETE_REQUEST_SUCCESS,
INVITES_REQUEST,
INVITES_REQUEST_FAILURE,
INVITES_REQUEST_SUCCESS,
INVITES_VALIDATE_TOKEN,
INVITES_VALIDATE_TOKEN_SUCCESS,
INVITES_VALIDATE_TOKEN_FAILURE,
INVITE_ACCEPTED,
INVITES_SEND,
INVITES_SEND_ERROR,
INVITES_SEND_FAILURE,
INVITES_SEND_SUCCESS,
INVITE_RESEND_REQUEST,
INVITE_RESEND_REQUEST_FAILURE,
INVITE_RESEND_REQUEST_SUCCESS,
} from 'calypso/state/action-types';
import { fetchCurrentUser } from 'calypso/state/current-user/actions';
import { getInviteForSite } from 'calypso/state/invites/selectors';
import { errorNotice, successNotice } from 'calypso/state/notices/actions';
import { receiveSite } from 'calypso/state/sites/actions';
import 'calypso/state/invites/init';
/**
* Triggers a network request to fetch invites for the specified site.
* @param {?number} siteId Site ID
* @returns {Function} Action thunk
*/
export function requestSiteInvites( siteId ) {
return async ( dispatch ) => {
dispatch( {
type: INVITES_REQUEST,
siteId,
} );
try {
const { found, invites, links } = await wpcom.req.get( `/sites/${ siteId }/invites`, {
status: 'all',
number: 100,
} );
dispatch( {
type: INVITES_REQUEST_SUCCESS,
siteId,
found,
invites,
links,
} );
} catch ( error ) {
dispatch( {
type: INVITES_REQUEST_FAILURE,
siteId,
error,
} );
}
};
}
export function resendInvite( siteId, inviteId ) {
return async ( dispatch ) => {
dispatch( {
type: INVITE_RESEND_REQUEST,
siteId: siteId,
inviteId: inviteId,
} );
try {
const data = await wpcom.req.post( `/sites/${ siteId }/invites/${ inviteId }/resend` );
dispatch( {
type: INVITE_RESEND_REQUEST_SUCCESS,
siteId,
inviteId,
data,
} );
} catch ( error ) {
dispatch( {
type: INVITE_RESEND_REQUEST_FAILURE,
siteId,
inviteId,
error,
} );
dispatch( errorNotice( translate( 'Invitation failed to resend.' ) ) );
}
};
}
export function deleteInvite( siteId, inviteId ) {
return deleteInvites( siteId, [ inviteId ] );
}
const deleteInvitesFailureNotice = ( siteId, inviteIds ) => ( dispatch, getState ) => {
for ( const inviteId of inviteIds ) {
const invite = getInviteForSite( getState(), siteId, inviteId );
const invitee = ( invite.user.email || invite.user.login ) ?? '';
dispatch(
errorNotice(
invitee.length > 20
? translate( 'An error occurred while deleting the invite for %s….', {
args: invitee.slice( 0, 20 ),
} )
: translate( 'An error occurred while deleting the invite for %s.', { args: invitee } )
)
);
}
};
export function deleteInvites( siteId, inviteIds ) {
return async ( dispatch ) => {
dispatch( {
type: INVITES_DELETE_REQUEST,
siteId: siteId,
inviteIds: inviteIds,
} );
try {
const data = await wpcom.req.post(
{
path: `/sites/${ siteId }/invites/delete`,
apiNamespace: 'wpcom/v2',
},
{
invite_ids: inviteIds,
}
);
if ( data.deleted.length > 0 ) {
dispatch( {
type: INVITES_DELETE_REQUEST_SUCCESS,
siteId,
inviteIds: data.deleted,
data,
} );
dispatch(
successNotice(
translate( 'Invite deleted.', 'Invites deleted.', { count: inviteIds.length } ),
{
displayOnNextPage: true,
}
)
);
}
// It's possible for the request to succeed, but the deletion to fail.
if ( data.invalid.length > 0 ) {
dispatch( {
type: INVITES_DELETE_REQUEST_FAILURE,
siteId,
inviteIds: data.invalid,
data,
} );
dispatch( deleteInvitesFailureNotice( siteId, inviteIds ) );
}
} catch ( error ) {
dispatch( {
type: INVITES_DELETE_REQUEST_FAILURE,
siteId,
inviteIds,
error,
} );
dispatch( deleteInvitesFailureNotice( siteId, inviteIds ) );
}
};
}
function inviteAccepted( invite ) {
return { type: INVITE_ACCEPTED, invite };
}
export function createAccount( userData, invite ) {
return ( dispatch ) => {
const result = wpcom.req.post( '/users/new', {
...userData,
validate: false,
send_verification_email: userData.email !== invite.sentTo,
locale: getLocaleSlug(),
client_id: config( 'wpcom_signup_id' ),
client_secret: config( 'wpcom_signup_key' ),
} );
result
.then( () => {
recordTracksEvent( 'calypso_invite_account_created', {
is_p2_site: invite.site?.is_wpforteams_site ?? false,
inviter_blog_id: invite.site?.ID ?? false,
} );
} )
.catch( ( error ) => {
if ( error.message ) {
dispatch( errorNotice( error.message ) );
}
recordTracksEvent( 'calypso_invite_account_creation_failed', { error: error.error } );
} );
return result;
};
}
export function generateInviteLinks( siteId ) {
return async ( dispatch ) => {
await wpcom.req.post( {
path: `/sites/${ siteId }/invites/links/generate`,
apiNamespace: 'wpcom/v2',
} );
dispatch( requestSiteInvites( siteId ) );
};
}
export function disableInviteLinks( siteId ) {
return async ( dispatch ) => {
await wpcom.req.post( {
path: `/sites/${ siteId }/invites/links/disable`,
apiNamespace: 'wpcom/v2',
} );
dispatch( requestSiteInvites( siteId ) );
};
}
export function acceptInvite( invite, emailVerificationSecret ) {
return async ( dispatch ) => {
try {
const data = await wpcom.req.get(
`/sites/${ invite.site.ID }/invites/${ invite.inviteKey }/accept`,
{
activate: invite.activationKey,
email_verification_secret: emailVerificationSecret,
include_domain_only: true,
apiVersion: '1.3',
}
);
if ( invite.role !== 'follower' && invite.role !== 'viewer' ) {
dispatch( receiveSite( data.site ) );
await dispatch( fetchCurrentUser() );
}
const acceptedNoticeResult = acceptedNotice( invite, true, true );
if ( ! invite.site.is_vip && ! invite.site.is_wpforteams_site ) {
dispatch( successNotice( acceptedNoticeResult[ 0 ], acceptedNoticeResult[ 1 ] ) );
}
recordTracksEvent( 'calypso_invite_accepted', {
is_p2_site: invite.site.is_wpforteams_site ?? false,
inviter_blog_id: invite.site.ID,
} );
dispatch( inviteAccepted( invite ) );
return data;
} catch ( error ) {
if ( error.message ) {
dispatch( errorNotice( error.message ) );
}
recordTracksEvent( 'calypso_invite_accept_failed', { error: error.error } );
throw error;
}
};
}
export function validateTokens( siteId, invitees, role ) {
return async ( dispatch ) => {
dispatch( {
type: INVITES_VALIDATE_TOKEN,
} );
try {
const data = await wpcom.req.post( `/sites/${ siteId }/invites/validate`, {
invitees,
role,
} );
dispatch( {
type: INVITES_VALIDATE_TOKEN_SUCCESS,
data,
} );
recordTracksEvent( 'calypso_invite_create_validation_success' );
} catch ( e ) {
dispatch( {
type: INVITES_VALIDATE_TOKEN_FAILURE,
} );
recordTracksEvent( 'calypso_invite_create_validation_failed' );
}
};
}
export function sendInvites( siteId, invitees, role, message, isExternal ) {
return async ( dispatch ) => {
dispatch( {
type: INVITES_SEND,
} );
try {
const response = await wpcom.req.post( `/sites/${ siteId }/invites/new`, {
invitees,
role,
message,
is_external: isExternal,
source: 'calypso',
} );
const errorsCount = Object.keys( response.errors ).length;
if ( errorsCount ) {
dispatch( {
type: INVITES_SEND_ERROR,
errorType: errorsCount === invitees.length ? 'all' : 'partial',
} );
recordTracksEvent( 'calypso_invite_send_failed' );
} else {
dispatch( {
type: INVITES_SEND_SUCCESS,
} );
recordTracksEvent( 'calypso_invite_send_success', { role } );
}
} catch ( e ) {
dispatch( {
type: INVITES_SEND_FAILURE,
} );
recordTracksEvent( 'calypso_invite_send_failed' );
}
};
}
|