This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Released v2.0.0-alpha.3 with a basic endpoint reducer (does not suppo…
…rt simultaneous requests atm), ref #2
- Loading branch information
1 parent
7f5050a
commit 77ad9cd
Showing
3 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {Map} from 'immutable'; | ||
|
||
import {InvalidConfigError} from './errors'; | ||
import {API_SIGNATURE} from './types'; | ||
|
||
/** | ||
* Create a Redux reducer for an API. | ||
* | ||
* @param {object} api API configuration | ||
* @return {function} The created Redux reducer | ||
*/ | ||
export const createApiReducer = (api) => { | ||
// Validate API configuration | ||
if (typeof api !== 'object') { | ||
throw new InvalidConfigError(`Invalid API configuration: ${api}`); | ||
} | ||
|
||
// Gather relevant information | ||
const { | ||
// entityReducer: options, | ||
mergedTypes: { | ||
request: requestTypes, | ||
success: successTypes, | ||
failure: failureTypes | ||
} | ||
} = api; | ||
|
||
const initialState = new Map(); | ||
|
||
// Return the reducer | ||
return (state = initialState, action) => { | ||
// Check if this is one of our actions (quicker than looping through 3 arrays) | ||
if (action.signature === API_SIGNATURE) { | ||
if (action.isEntity) { | ||
// Succes type | ||
// if (successTypes.indexOf(action.type) !== -1) { | ||
// | ||
// } | ||
return state; | ||
} | ||
|
||
// Endpoint action | ||
if (requestTypes.indexOf(action.type) !== -1) { | ||
return state | ||
.setIn(['endpoints', action.endpoint, 'loading'], true) | ||
.setIn(['endpoints', action.endpoint, 'finished'], false) | ||
.setIn(['endpoints', action.endpoint, 'data'], null) | ||
.setIn(['endpoints', action.endpoint, 'error'], null); | ||
} else if (successTypes.indexOf(action.type) !== -1) { | ||
return state | ||
.setIn(['endpoints', action.endpoint, 'loading'], false) | ||
.setIn(['endpoints', action.endpoint, 'finished'], true) | ||
.setIn(['endpoints', action.endpoint, 'data'], action.payload) | ||
.setIn(['endpoints', action.endpoint, 'error'], action.payloadError); | ||
} else if (failureTypes.indexOf(action.type) !== -1) { | ||
return state | ||
.setIn(['endpoints', action.endpoint, 'loading'], false) | ||
.setIn(['endpoints', action.endpoint, 'finished'], true) | ||
.setIn(['endpoints', action.endpoint, 'data'], action.payload) | ||
.setIn(['endpoints', action.endpoint, 'error'], action.hasPayloadError ? action.payloadError : action.error); | ||
} | ||
} | ||
|
||
return state; | ||
}; | ||
}; |