Skip to content

Instantly share code, notes, and snippets.

View beaucollins's full-sized avatar
🃏
Special Author

Beau Collins beaucollins

🃏
Special Author
  • Seattle, Wash.
View GitHub Profile
apiRequest(isCreate, requestData(createTeam), teamsCreateSuccess, teamsCreateFailure),
apiRequest(isUpdate, requestData(updateTeam), teamsUpdateSuccess, teamsUpdateFailure)
notify(
isTaskTypeCreate,
"The project's task type was successfully created.",
"The project's task type could not be created."
),
export type AppState = {|
ui: Ui,
auth: Auth,
entities: Entities,
|};
https://github.com/Automattic/happiness-scheduler-frontend/blob/master/src/state/index.js#L46-L50
function asyncReaction<T: ActionTypes>(detector: Detector<T>, handler: Handler<T>): AppMiddleware {
return store => next => action => {
const detected = detector(action);
if (detected !== null) {
handler(detected, store.getState()).then(store.dispatch);
}
return next(action);
}
}
const middleware: AppMiddleware = store => next => action => {
const usersCreate = isProjectsCreate(action);
if (usersCreate !== null) {
// do something with usersCreate
createProjectHTTP(usersCreate, store.getState()).then(store.dispatch);
}
return next(action);
}
type Handler<T: ActionTypes> = (T, AppState) => Promise<ActionTypes>;
const createProjectHTTP: Handler<ProjectsCreate> = async (action, state) => {
try {
const response = await createProject(action.stage.value);
return projectsCreateSuccess(response.data);
} catch (error) {
return projectsCreateFailure(action.stage.value);
}
}
type Handler<T: ActionTypes> = (T, AppState) => Promise<void> | void;
const middleware: AppMiddleware = () => next => action => {
const usersCreate = isProjectsCreate(action);
if (usersCreate !== null) {
// do something with usersCreate
}
return next(action);
}
type Detector<T: ActionTypes> = ActionTypes => ?T;
const isProjectsCreate: Detector<ProjectsCreate> = action =>
action.type === 'PROJECTS_CREATE' ? action : null;