redux-json-api-handlers
Redux JSON-API handlers V2. New api
Api methods
createLoadHandler(resourceType: string, options: object): nextState
Use it for handle success data loading
Options:
const options = {
mapToKey: bool|string,
withLoading: bool,
singular: bool,
withReplace: bool,
addToState: object,
}
Simple example
Initial state looks like:
state = {
posts: {
isLoaded: false,
isLoading: false,
posts: {}
}
}
const handlers = {
[LOAD_POSTS.SUCCESS]: createLoadHandler('posts')
}
Resulted state looks like:
state = {
posts: {
isLoaded: true,
isLoading: false,
posts: [1]
}
}
Custom example
Initial state looks like:
state = {
posts: {
isLoadedPostIds: false,
isLoadingPostIds: false,
post: null,
}
}
Handler:
const handlers = {
[LOAD_POSTS.SUCCESS]: createLoadHandler('posts', { mapToKey: 'post', singular: true })
}
Resulted state looks like:
state = {
posts: {
isLoadedPostIds: true,
isLoadingPostIds: false,
postIds: 1,
}
}
createDeleteHandler(stateKey: string): nextState
Use it for handle delete data.
Action payload should have deletedId
param
Simple example
Initial state looks like:
state = {
posts: {
posts: [1, 2]
}
}
Handler:
const handlers = {
[DELETE_POST.SUCCESS]: createDeleteHandler('posts')
}
Resulted state looks like:
state = {
posts: {
posts: [2]
}
}
Custom example
Initial state looks like:
state = {
posts: {
postIds: [1, 2]
}
}
const handlers = {
[DELETE_POST.SUCCESS]: createDeleteHandler('posts', {
mapToKey: 'postIds',
})
}
Resulted state looks like:
state = {
posts: {
postIds: [1]
}
}