Skip to content

Instantly share code, notes, and snippets.

@casesandberg
Last active February 22, 2021 04:25
Show Gist options
  • Save casesandberg/4ac8d02cef571ff9e3d495ee5adad3da to your computer and use it in GitHub Desktop.
Save casesandberg/4ac8d02cef571ff9e3d495ee5adad3da to your computer and use it in GitHub Desktop.
Redux Typekit API
import { createAction, createAsyncAction, createReducer, ActionReturnTypes, createSelector } from 'redux-typekit'
import { Issue } from '@mm-backend/issues/model'
export const selector = 'issues'
export const actions = {
add: createAction<'issues/ADD', { issue: Issue }>('issues/ADD'),
}
export type actions = ActionReturnTypes<typeof actions>
interface State {
byId: { [id: string]: Issue }
}
const initialState: State = {
byId: {},
}
export const reducer = createReducer<State, actions>(initialState, (state, action) => {
switch (action.type) {
case actions.add.type: {
state.byId[action.issue.id] = action.issue
break
}
}
})
const storeSelector = (state: Record<typeof selector, State>) => state[selector]
export const selectors = {
getById: (id: string) => createSelector(storeSelector, (store) => store.byId[id]),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment