Skip to content

Instantly share code, notes, and snippets.

@emyann
Last active January 5, 2019 21:01
Show Gist options
  • Save emyann/785f123a5c3e3e63ce19834f61529dc6 to your computer and use it in GitHub Desktop.
Save emyann/785f123a5c3e3e63ce19834f61529dc6 to your computer and use it in GitHub Desktop.
// 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