File size: 3,533 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 |
import {
CURRENT_USER_RECEIVE,
CURRENT_USER_SET_EMAIL_VERIFIED,
CURRENT_USER_SET_JETPACK_PARTNER_TYPE,
SITE_RECEIVE,
SITES_RECEIVE,
} from 'calypso/state/action-types';
import { combineReducers, withSchemaValidation } from 'calypso/state/utils';
import emailVerification from './email-verification/reducer';
import { capabilitiesSchema, flagsSchema, idSchema, lasagnaSchema } from './schema';
/**
* Tracks the current user ID.
*
* In development, if you are receiving Redux errors like this:
*
* Error: Given action "CURRENT_USER_RECEIVE", reducer "id" returned undefined.
*
* This is likely caused by a server-side error or stored state corruption/auth token expiry.
* @param {Object} state Current state
* @param {Object} action Action payload
* @returns {Object} Updated state
*/
export const id = withSchemaValidation( idSchema, ( state = null, action ) => {
switch ( action.type ) {
case CURRENT_USER_RECEIVE:
return action.user.ID;
}
return state;
} );
export const user = ( state = null, action ) => {
switch ( action.type ) {
case CURRENT_USER_RECEIVE:
return action.user;
case CURRENT_USER_SET_EMAIL_VERIFIED:
return {
...state,
email_verified: action.verified,
};
case CURRENT_USER_SET_JETPACK_PARTNER_TYPE:
return {
...state,
jetpack_partner_types: [
...( state.jetpack_partner_types || [] ),
action.jetpack_partner_type,
],
has_jetpack_partner_access: true,
};
}
return state;
};
export const flags = withSchemaValidation( flagsSchema, ( state = [], action ) => {
switch ( action.type ) {
case CURRENT_USER_RECEIVE:
return action.user.meta?.data?.flags?.active_flags ?? [];
}
return state;
} );
/**
* Compare if two sets of capabilities are equal.
*
* Capability sets are simple objects with boolean flags,
* so comparison is as simple as comparing objects at the first level.
* @param {Object} capA First set of capabilities
* @param {Object} capB Second set of capabilities
* @returns {boolean} True if capability sets are the same, false otherwise.
*/
function areCapabilitiesEqual( capA, capB ) {
if ( ! capA || ! capB ) {
return false;
}
const keysA = Object.keys( capA );
const keysB = Object.keys( capB );
return keysA.length === keysB.length && keysA.every( ( key ) => capB[ key ] === capA[ key ] );
}
/**
* Returns the updated capabilities state after an action has been dispatched.
* The state maps site ID keys to an object of current user capabilities for
* that site.
* @param {Object} state Current state
* @param {Object} action Action payload
* @returns {Object} Updated state
*/
export const capabilities = withSchemaValidation( capabilitiesSchema, ( state = {}, action ) => {
switch ( action.type ) {
case SITE_RECEIVE:
case SITES_RECEIVE: {
const sites = action.site ? [ action.site ] : action.sites;
return sites.reduce( ( memo, site ) => {
if ( ! site.capabilities || areCapabilitiesEqual( site.capabilities, memo[ site.ID ] ) ) {
return memo;
}
if ( memo === state ) {
memo = { ...state };
}
memo[ site.ID ] = site.capabilities;
return memo;
}, state );
}
}
return state;
} );
export const lasagnaJwt = withSchemaValidation( lasagnaSchema, ( state = null, action ) => {
switch ( action.type ) {
case CURRENT_USER_RECEIVE:
return action.user.lasagna_jwt || null;
}
return state;
} );
export default combineReducers( {
id,
user,
capabilities,
flags,
emailVerification,
lasagnaJwt,
} );
|