Skip to content

Instantly share code, notes, and snippets.

@szagi3891
Last active October 2, 2016 17:44
Show Gist options
  • Save szagi3891/b49a7583677ca651dae2618770092b12 to your computer and use it in GitHub Desktop.
Save szagi3891/b49a7583677ca651dae2618770092b12 to your computer and use it in GitHub Desktop.
//Action - typ unia, zawierają się w nim wszystkie akcje
//StateTodolist - stan modułu todolisty
//StateApp - stan aplikacji
//todolist.js
export const getOfAction(actionName: string) => ((action: Action, state: StateTodolist): StateTodolist | null) => {
//na te akcje ten reducer cokolwiek robi
if (actionName === 'TODO_ADD' || actionName === 'TODO_REMOVE') {
return (action: Action, state: StateTodolist): StateTodolist => {
switch (action.kind) {
'TODO_ADD' : {
// nowy stan
}
'TODO_REMOVE' : {
// nowy stan
}
default : {
throw Error('W to miejsce nigdy nie powinno trafić żadne wywołanie');
}
}
};
}
return null;
};
//kombinacja dwóch reducerów
import todolist from './todolist';
import playlista from './playlista';
export const getOfActions = (actionName: string): ((action: Action, state: StateApp): StateApp | null) => {
const reducerTodolist = todolist.getOfActions(actionName);
const reducerPlaylista = playlista.getOfActions(actionName);
if (reducerTodolist === null || reducerPlaylista === null) {
return null;
}
return (action: Action, state: StateApp): StateApp {
return {
todolist: reducerTodolist ? reducerTodolist(action, state.todolist) : state.todolist,
playlista: reducerPlaylista ? reducerPlaylista(action, state.playlista) : state.playlista
};
};
};
//główny reducer
export createMainReducer = (): (action: Action, state: StateApp) => StateApp {
const map = {};
return (action: Action, state: StateApp) => StateApp {
const actionName = action.kind;
if (typeof(map[actionName]) === 'undefined') {
map[actionName] = tenDrugiModul.getOfActions(actionName);
}
return map[actionName](action, state);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment