Last active
January 5, 2019 21:01
-
-
Save emyann/785f123a5c3e3e63ce19834f61529dc6 to your computer and use it in GitHub Desktop.
This file contains 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
// actionCreators.ts | |
import { Action } from 'redux'; | |
import { ThunkAction } from 'redux-thunk'; | |
import { AppState } from './rootReducer'; | |
export type AppAction<A = Action, P = any> = A & { payload: P }; | |
export function makeActionCreator<P, T extends string = string>(type: T) { | |
function actionCreator(payload?: P): AppAction<Action<typeof type>, P> { | |
return { | |
type, | |
payload: payload ? payload : ({} as any), | |
}; | |
} | |
return actionCreator; | |
} | |
export function makeAsyncActionCreator<P, T>(callback: ThunkAction<any, AppState, any, AppAction<Action<T>, P>>) { | |
return () => callback; | |
} | |
// action.ts | |
import { makeActionCreator } from './actionCreators'; | |
import { Call } from '@jive/realtime-events'; | |
export enum ActionsType { | |
START_INCOMING_CALL = 'callEvents/startIncomingCall', | |
START_INCOMING_CONVERSATION = 'callEvents/startIncomingConversation', | |
} | |
type ActionPayload = { call: Call } | |
export const startIncomingCall = makeActionCreator<ActionPayload, ActionsType.START_INCOMING_CALL>( | |
ActionsType.START_INCOMING_CALL, | |
); | |
export const startIncomingConversation = makeActionCreator<ActionPayload, ActionsType.START_INCOMING_CONVERSATION>( | |
ActionsType.START_INCOMING_CONVERSATION, | |
); | |
export type CallEventsActions = | |
| ReturnType<typeof startIncomingCall> | |
| ReturnType<typeof startIncomingConversation> | |
// reducer.ts | |
import { CallEventsActions } from './actions'; | |
export interface CallState { | |
latestCall: string | null; | |
byId: { [id: string]: Call }; | |
allIds: string[]; | |
} | |
export const CALLS_INITIAL_STATE = { | |
latestCall: null, | |
byId: {}, | |
allIds: [], | |
}; | |
export const callEventsReducer: Reducer<CallState, CallEventsActions> = (state = CALLS_INITIAL_STATE, action) => { | |
switch (action.type) { | |
case START_INCOMING_CALL:{} | |
} | |
} | |
export default callEventsReducer; | |
// rootReducer.ts | |
import { combineReducers } from 'redux'; | |
import { default as calls } from './reducer'; | |
const rootReducer = combineReducers({ | |
calls | |
}); | |
export type AppState = ReturnType<typeof rootReducer>; | |
export default rootReducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment