|
|
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'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const items = withSchemaValidation( itemsSchema, ( state = [], action ) => { |
|
|
switch ( action.type ) { |
|
|
case ACTIVE_PROMOTIONS_RECEIVE: |
|
|
|
|
|
if ( ! Array.isArray( action.activePromotions ) ) { |
|
|
return []; |
|
|
} |
|
|
return action.activePromotions; |
|
|
} |
|
|
|
|
|
return 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; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|