Reputation: 31
I'm trying to implement redux-orm and not sure of the best way to handle updating meta properties on my models. The return value of static reducers are ignored in the latest version, and looking through the API I can't see any way to modify the meta property from a model inside of the static reducer function.
IE, for standard entity state updates, here is the example from the repo:
static reducer(action, Book, session) {
switch (action.type) {
case 'CREATE_BOOK':
Book.create(action.payload);
break;
case 'UPDATE_BOOK':
Book.withId(action.payload.id).update(action.payload);
break;
case 'REMOVE_BOOK':
const book = Book.withId(action.payload);
book.delete();
break;
... this function calls the model directly and has no return value
I found a couple of examples of a static metaReducer implementation, but these are all with an older version. I could make a sibling reducer to handle this but I'd prefer for everything to be on my actual ORM entities, and to utilize the provided meta property.
If anyone has a simple example of implementing a meta reducer with the latest version of redux-orm it would be much appreciated.
Thanks!
Upvotes: 1
Views: 304
Reputation: 7448
Basically you'll have to ensure your models contains a key (in the below example here type
) that would allow you to easily retrieve the correct session
store for each resource.
Then it is just super easy to do basic CRUD operations with a meta reducer that would handle all your models through redux-orm
Here is a very simplified sample code I use in my app with redux-orm@^
0.9.0
export default function resourcesReducer (state, action) {
const session = orm.session(getDefaultState(state))
const resource = action.resource
switch (action.type) {
case RESOURCE_CREATE: {
session[resource.type].create(resource)
break
}
case RESOURCE_EDIT: {
session[resource.type].withId(resource.id).update(resource)
break
}
case RESOURCE_DELETE: {
session[resource.type].withId(resource.id).delete(resource)
break
}
}
return session.state
}
Upvotes: 1