Created
February 20, 2019 16:12
-
-
Save rbinksy/b50a3d5e9a12569142dcc38c489d083a to your computer and use it in GitHub Desktop.
Higher Order Reducer Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
IAssetRequestRecord, | |
RequestType | |
} from 'records/typescript/AssetRequest'; | |
import { createSelector } from 'reselect'; | |
import sortByDate from 'utils/sortByDate'; | |
import sortAlphbeticReverse from 'utils/sortAlphbeticReverse'; | |
import withAssetRequestsReducer, { | |
getAssetRequests, | |
initialAssetRequestsState, | |
withAssetRequestsActions | |
} from 'containers/WithAssetRequests/reducer'; | |
export enum ActionTypes { | |
REDELIVER_TO_RBM_START = 'PRODUCTIONDETAIL/REDELIVER_TO_RBM_START', | |
REDELIVER_TO_RBM_SUCCESS = 'PRODUCTIONDETAIL/REDELIVER_TO_RBM_SUCCESS', | |
REDELIVER_TO_RBM_FAILURE = 'PRODUCTIONDETAIL/REDELIVER_TO_RBM_FAILURE', | |
RETRANSCODE_START = 'PRODUCTIONDETAIL/RETRANSCODE_START', | |
RETRANSCODE_SUCCESS = 'PRODUCTIONDETAIL/RETRANSCODE_SUCCESS', | |
RETRANSCODE_FAILURE = 'PRODUCTIONDETAIL/RETRANSCODE_FAILURE' | |
} | |
export default withAssetRequestsReducer( | |
( | |
state = initialAssetRequestsState, | |
action: { | |
type: ActionTypes; | |
payload: any; | |
} | |
) => { | |
const { type, payload } = action; | |
switch (type) { | |
case ActionTypes.REDELIVER_TO_RBM_SUCCESS: | |
return state.setIn( | |
['entities', 'ASSETREQUESTS', payload.id.toString()], | |
payload | |
); | |
case ActionTypes.RETRANSCODE_SUCCESS: | |
return state.updateIn(['entities', 'ASSETREQUESTS'], assetReqs => | |
assetReqs.mergeDeep(payload)); | |
default: | |
return state; | |
} | |
}, | |
'PRODUCTIONDETAIL' | |
); | |
export const actions = withAssetRequestsActions( | |
{ | |
redeliverToRBM: (prodId: string) => ({ | |
type: ActionTypes.REDELIVER_TO_RBM_START, | |
payload: prodId | |
}), | |
reTranscode: (prodId: string) => ({ | |
type: ActionTypes.RETRANSCODE_START, | |
payload: prodId | |
}) | |
}, | |
'PRODUCTIONDETAIL' | |
); | |
export const getAssetReqsGroupedByType = createSelector( | |
state => getAssetRequests(state, 'productionDetail'), | |
assetReqs => | |
assetReqs | |
.sort( | |
(a: IAssetRequestRecord, b: IAssetRequestRecord) => | |
sortByDate('requiredBy')(a, b) | |
|| sortAlphbeticReverse('fulfilmentHouse')(a, b) | |
|| sortAlphbeticReverse('bundleCodeDescription')(a, b) | |
|| sortAlphbeticReverse('channelCode')(a, b) | |
|| sortAlphbeticReverse('state')(a, b) | |
) | |
.reverse() | |
.groupBy(({ requestType }) => | |
[RequestType.CATCHUP, RequestType.VOD].includes(requestType) | |
? 'catchup-vod' | |
: requestType) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment