Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom separators for action types #165

Closed
roboslone opened this issue Jul 19, 2017 · 8 comments
Closed

Allow custom separators for action types #165

roboslone opened this issue Jul 19, 2017 · 8 comments
Assignees

Comments

@roboslone
Copy link
Contributor

This is a feature request, so it doesn't fit any template =/

I use redux-actions with nested structure to create reducers, like so:

const reducer = handleActions({
    search: {
        setQuery: (state, action) => { ... },
    },
    ...
})

With code above action with type search/setQuery will be processed by given reducer.
However, I cannot use such nested structure for actions dispatched by redux-promise-middleware since separator is hardcoded.

So I would like to change separator from _ to / and use nested structure in handleActions:

const reducer = handleActions({
    search: {
        fetchResults: {
            FULFILLED: (state, action) => { ... },
            PENDING: (state, action) => { ... },
            REJECTED: (state, action) => { ... },
        }
    },
    ...
})
@tomatau
Copy link
Collaborator

tomatau commented Jul 19, 2017

You can use type to reducer https://github.com/tomatau/type-to-reducer to handle nested actions. Your structure would suggest namespaces as well as separators e.g. customer/bills/add_invoice and that's tricky to configure, easier to use dynamic keys for the namespaces and nested action types for the separators

@roboslone
Copy link
Contributor Author

redux-actions deals with namespaces, so customer/bills/add_invoice would be proceesed by following reducer:

const reducer = handleActions({
    customer: {
        bills: {
            add_invoice: {
                PENDING: ...,
                ...
            },
        },
    },
})

@tomatau
Copy link
Collaborator

tomatau commented Jul 19, 2017

What's the benefit over using multiple reducers keyed by a namespace variable in conjunction with combineReducers from redux?

Const add_invoice = 'customer/bills/add_invoice`

ttr({
 [add_invoice]: { 
 Pending: () =≥ ...
})

You can also do add_invoice_Pending as a key with your reducer helper function and then no need to add an edge case feature into this library.

@roboslone
Copy link
Contributor Author

I don't see how this is relevant to the issue.
I can't avoid hardcoded separator using multiple reducers and therefore I can't use redux-actions with redux-promise-middleware the way I like.

Perhaps I didn't understand you, could you please provide a code snippet?

Here's what I'm looking for:

import { combineReducers } from 'redux'
import { createActions, handleActions } from 'redux-actions'


const actions = createActions({
    foo: {
        fetch: () => {
            return axios.get('/foo')
        }
    },
    bar: {
        fetch: () => {
            return axios.get('/bar')
        }
    }
})

const reducer = combineReducers({

    foo: handleActions({
        fetch: {
            PENDING: (state, action) => {...},
            FULFILLED: (state, action) => {...},
            REJECTED: (state, action) => {...},
        }
    }),

    bar: handleActions({
        fetch: {
            PENDING: (state, action) => {...},
            FULFILLED: (state, action) => {...},
            REJECTED: (state, action) => {...},
        }
    }),

})

However until separators are hardcoded I can't do that. Instead I have to change reducer to following:

const reducer = combineReducers({

    foo: handleActions({
        fetch_PENDING: (state, action) => {...},
        fetch_FULFILLED: (state, action) => {...},
        fetch_REJECTED: (state, action) => {...},
    }),

    bar: handleActions({
        fetch_PENDING: (state, action) => {...},
        fetch_FULFILLED: (state, action) => {...},
        fetch_REJECTED: (state, action) => {...},
    }),

})

@tomatau
Copy link
Collaborator

tomatau commented Jul 19, 2017

I'd suggest using explicit coupling between action creators and reducers based on constants rather than implicit coupling based on object shape and library magic. But I can understand a little benefit to your suggestion. Let's wait for @pburtchaell to share his thoughts on this

@roboslone
Copy link
Contributor Author

@pburtchaell ping

@pburtchaell
Copy link
Owner

I agree with @tomatau that explicit coupling of action creators and reducers is better and that it's best to avoid (magical 🦄 ) libraries like Redux Action, but that's my personal preference.

I'd honestly rather not add another config option to the middleware (can potentially bloat the codebase), but I understand your case, @roboslone, and it's a reasonable 3 line diff. I think we can merge this in once the PR is approved. Just add tests, please. 👍

@pburtchaell
Copy link
Owner

Released as redux-promise-middleware@4.4.1 on npm. Thanks!

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

No branches or pull requests

3 participants