Skip to content

Commit

Permalink
Merge pull request #22 from uniqueway/feature/no-data-dependence
Browse files Browse the repository at this point in the history
allow responses w/o data envelope. workaround for #6 until #19 is done
  • Loading branch information
devvmh authored Aug 4, 2016
2 parents b13165a + cea5966 commit 3f63424
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ If you ever worry about your cache getting out of sync, it's easy to manually sy

- allow dispatching multiple actions for API_CALL
- consider allowing dispatching multiple actions for CREATE/UPDATE/DELETE
- it would be great to support nested models - automatically stripping the models out of a parent object, moving them into their own store, and storing the parent with just an id reference. This might make component logic kind of intense if we aren't careful.
- configurable keys: This module is still mostly agnostic about the format your data comes in from the server as, but in particular it expects records to live in response.data when running FETCH, and it expects all records to have an id attribute. It would be great to analyze this further and make those keys configurable.
- configurable keys: It would be great to integrate normalizr, so people could specify a response schema and have their data automatically normalized into the store. This would also enable support for nested models for free.
- it would be great to support nested models in selectors, perhaps using normalizr somehow.
- tests for every public function
- tests for every private function too

Expand Down
2 changes: 1 addition & 1 deletion src/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type CrudAction<T> = {
export type Success<T:{ id: ID }> = {
type: typeof FETCH_SUCCESS | typeof FETCH_ONE_SUCCESS | typeof CREATE_SUCCESS | typeof UPDATE_SUCCESS | typeof DELETE_SUCCESS,
meta: Meta,
payload: {
payload: T | {
data: T,
},
error?: boolean,
Expand Down
11 changes: 8 additions & 3 deletions src/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ function byIdReducer(state = byIdInitialState, action) {
switch (action.type) {
case FETCH_SUCCESS:
const data = state.toJS()
action.payload.data.forEach((record) => {
const payload = ('data' in action.payload) ? action.payload.data : action.payload
payload.forEach((record) => {
data[record.id] = {
record,
fetchTime: action.meta.fetchTime,
Expand Down Expand Up @@ -111,10 +112,14 @@ function collectionReducer(state = collectionInitialState, action) {
.set('fetchTime', 0)
.set('error', null)
case FETCH_SUCCESS:
const ids = action.payload.data.map((elt) => elt.id)
const originalPayload = action.payload || {}
const payload = ('data' in originalPayload) ? action.payload.data : action.payload
const otherInfo = ('data' in originalPayload) ? originalPayload : {}
if ('data' in originalPayload) delete otherInfo.data
const ids = payload.map((elt) => elt.id)
return state.set('params', fromJS(action.meta.params))
.set('ids', fromJS(ids))
.set('otherInfo', fromJS(action.payload || {}).delete('data'))
.set('otherInfo', fromJS(otherInfo))
.set('error', null)
.set('fetchTime', action.meta.fetchTime)
case FETCH_ERROR:
Expand Down

0 comments on commit 3f63424

Please sign in to comment.