Last active
February 22, 2021 04:25
-
-
Save casesandberg/4ac8d02cef571ff9e3d495ee5adad3da to your computer and use it in GitHub Desktop.
Redux Typekit API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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