Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

florisvink
Copy link

As a possible fix for #80 I wrote this PR. If this is something you want to include into the module I can write some tests as well.

Text from the original issue see below:

Internally the module uses ImmutalbleJS for setting the api-states, but the api-reducer cannot be combined with existing reducers when the initialState of the store is Immutable:

For example:

const redux = require('redux');
const Immutable = require('immutable');
const redux_immutable = require('redux-immutable');
const redux_thunk = require('redux-thunk').default;
const redux_json_api = require('redux-json-api');

const api = redux_json_api.reducer;

const reducers = redux_immutable.combineReducers({api});
const initialState = Immutable.Map();
const middlewares = redux.applyMiddleware(redux_thunk);
const store = redux.createStore(reducers, initialState, middlewares );

store.subscribe(() => console.log(store.getState().toJS()) )

store.dispatch(redux_json_api.setEndpointHost('http://ip.jsontest.com/'));
store.dispatch(redux_json_api.setAccessToken('access_token_123123'));

store.dispatch(redux_json_api.readEndpoint('/'))

This code results in:
TypeError: Cannot read property 'endpoint' of undefined: getState().api.endpoint;

The code looks like:

const { host: apiHost, path: apiPath, headers } = getState().api.endpoint;

I think (in case the root-state is Immutable) this should be something like:

const { host: apiHost, path: apiPath, headers } = getState().get('api').endpoint;

@egeriis
Copy link
Collaborator

egeriis commented Oct 20, 2016

Hi @florisvink

Thank you for raising this concern. We, at Dixie, have been considering to use ImmutableJS for the store for a while too. So we find it very relevant.

What we have decided though, is that redux-json-api is not supposed to be a do-it-all lib, which knows about various ways of handling a Redux store. So to implement knowledge in redux-json-api about ImmutableJS, we don't find to be the best solution.

Our thought so far have been to, a some point, create a new extension, redux-json-api-immutablejs (or whatever) which would extend the core logic from this lib, but provide it through an ImmutableJS interface.

The same thing could then be done for other mutation-handling libs, so this library is agnostic about in what way your application handles state.

Any thoughts on this? Would you be interested in working on this prototype?

@egeriis
Copy link
Collaborator

egeriis commented Oct 25, 2016

Closed due to inactivity.

@egeriis egeriis closed this Oct 25, 2016
@florisvink
Copy link
Author

Hi @egeriis
Thanks for looking at this and sorry for my late reply. :)
I totally agree to keep this lib agnostic. For now I will work a modified version that works with other store mutation types. But would it be nice to make store-mutation-adaptors for this lib, so that 99% of the functionality is just this lib plus a small adapter routine.

For example

import { combineReducers } from 'redux';
import { createApiReducer } from 'redux-json-api';
import { storeMutator } from 'redux-json-api-immutable'

const api = createApiReducer(storeMutator);

export default combineReducers({
  api
});

where storeMutator is of course quite a simple function :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants