Skip to content

Instantly share code, notes, and snippets.

@peatiscoding
Created October 10, 2018 01:33
Show Gist options
  • Save peatiscoding/d91e1c4b26e2ff44acc84343ac67f89d to your computer and use it in GitHub Desktop.
Save peatiscoding/d91e1c4b26e2ff44acc84343ac67f89d to your computer and use it in GitHub Desktop.
// store/session/actions.ts
import { AnyAction } from 'redux';
export interface SetAction {
type: 'SET'
accessToken: string
}
export interface Fetching {
type: 'SET_FETCHING'
isFetching: boolean
}
export type Action = SetAction | Fetching
export const set = (accessToken: string): SetAction => {
return { type: 'SET', accessToken }
}
export const isFetching = (isFetching: boolean): Fetching => {
return { type: 'SET_FETCHING', isFetching }
}
// store/index.ts
import { createStore, combineReducers, applyMiddleware } from 'redux'
import sessions, { State as SessionsState } from './session/reducers'
export interface RootState {
sessions: SessionsState
}
export default createStore(combineReducers({
sessions
}))
// store/session/reducers.ts
import { combineReducers } from 'redux'
import { Action } from './actions'
export interface AccessToken {
isFetching: boolean
accessToken?: string
}
export interface State {
accessToken: AccessToken
}
const accessToken = (state = { isFetching: false }, action: Action): AccessToken => {
switch (action.type) {
case 'SET':
return { ...state, accessToken: action.accessToken }
case 'SET_FETCHING':
return { ...state, isFetching: action.isFetching }
}
return state
}
export default combineReducers<State>({
accessToken
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment