jumpsuit / jumpstate

Jumpstate is a simple and powerful state management utility for Redux.
MIT License
425 stars 22 forks source link

Added action types to the reducer and typescript typings. #26

Closed nathanvale closed 7 years ago

nathanvale commented 7 years ago

I love how you have abstracted the boilerplate required to setup a redux reducers!

I would like to request to have action types available on the reducer as well has action creators.

It is handy to have the full sandboxed types available on the reducer for dispatching and also so typescript can dynamically create types for the action creators.

By passing in an array of ActionCreator types, typescript will map the types back onto the action creators for the reducer so the IDE will show you what you have available and of course the compiler will complain if you try and call an action creator that doesnt exist on the reducer!

`type ActionCreators = { login(payload: ILogin); loginSuccess(payload: { user: Parse.User }) loginPending(); loginReject(payload: { error: AxiosError }); loginAborted(); logout(); logoutSuccess(); logoutPending() logoutReject(payload: { error: AxiosError }); logoutAborted(); };

const reducer = State(NAME, {

// Initial State initial: { authenticated: false, loginPending: false, logoutPending: false, user: null },

// LOGIN

login(state, payload: ILogin): string { return { ...state }; },

loginSuccess(state, payload: { user: any }) { const user = payload.user; return { ...state, user: { ACL: user.ACL, createdAt: user.createdAt, email: user.email, sessionToken: user.sessionToken, updatedAt: user.updatedAt, username: user.username }, authenticated: true, loginPending: false }; },

loginPending(state) { return { ...state, loginPending: true }; },

loginReject(state, payload: { error: Parse.Error }) { return { ...state, loginPending: false }; },

loginAborted(state) { return { ...state, loginPending: false }; },

//LOGOUT

logout(state, payload: ILogin) { return { ...state }; },

logoutSuccess(state) { return { ...state, user: null, authenticated: false, logoutPending: false }; },

logoutPending(state) { return { ...state, logoutPending: true }; },

logoutReject(state, payload: { error: Parse.Error }) { return { ...state, logoutPending: false }; },

logoutAborted(state) { return { ...state, logoutPending: false }; }

});

`

tannerlinsley commented 7 years ago

Wonderful!