File size: 2,110 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 |
import { withStorageKey } from '@automattic/state-utils';
import {
ACTIVE_PROMOTIONS_RECEIVE,
ACTIVE_PROMOTIONS_REQUEST,
ACTIVE_PROMOTIONS_REQUEST_SUCCESS,
ACTIVE_PROMOTIONS_REQUEST_FAILURE,
} from 'calypso/state/action-types';
import { combineReducers, withSchemaValidation } from 'calypso/state/utils';
import { itemsSchema } from './schema';
/**
* ActivePromotions `Reducer` function
* root state -> state.activePromotions.items =>
* [ '', '', '', ... '' ]
* @param {Object} state - current state
* @param {Object} action - activePromotions action
* @returns {Object} updated state
*/
export const items = withSchemaValidation( itemsSchema, ( state = [], action ) => {
switch ( action.type ) {
case ACTIVE_PROMOTIONS_RECEIVE:
// Sometimes an empty PHP can be serialized as `{}` object, force to array in that case
if ( ! Array.isArray( action.activePromotions ) ) {
return [];
}
return action.activePromotions;
}
return state;
} );
/**
* `Reducer` function which handles request/response actions
* to/from WP REST-API
* @param {Object} state - current state
* @param {Object} action - activePromotions action
* @returns {Object} updated state
*/
export const requesting = ( state = false, action ) => {
switch ( action.type ) {
case ACTIVE_PROMOTIONS_REQUEST:
return true;
case ACTIVE_PROMOTIONS_REQUEST_SUCCESS:
case ACTIVE_PROMOTIONS_REQUEST_FAILURE:
return false;
}
return state;
};
/**
* `Reducer` function which handles ERROR REST-API response actions
* @param {Object} state - current state
* @param {Object} action - activePromotions action
* @returns {Object} updated state
*/
export const error = ( state = false, action ) => {
switch ( action.type ) {
case ACTIVE_PROMOTIONS_REQUEST:
case ACTIVE_PROMOTIONS_REQUEST_SUCCESS:
return false;
case ACTIVE_PROMOTIONS_REQUEST_FAILURE:
return true;
}
return state;
};
const combinedReducer = combineReducers( {
items,
requesting,
error,
} );
const activePromotionsReducer = withStorageKey( 'activePromotions', combinedReducer );
export default activePromotionsReducer;
|