got add, remove, and edit actions working

This commit is contained in:
Ken
2019-02-01 09:47:04 -08:00
parent 260b6177fe
commit 0e0ea2ef06
9 changed files with 860 additions and 108 deletions

View File

@@ -1,7 +1,12 @@
import { action, GenericActionTypes, GenericAction, GenericActionLookup } from '../redux-utils/action';
import { Dispatch } from 'redux';
import { Store } from '../store';
import * as todosService from '../service/todosService';
let counter = 0;
export const actions = {
add: (label: string) => action('add', { label }),
add: (label: string) => action('add', { id: String(counter++), label }),
remove: (id: string) => action('remove', { id }),
edit: (id: string, label: string) => action('edit', { id, label }),
complete: (id: string) => action('complete', { id }),
@@ -9,6 +14,46 @@ export const actions = {
filter: (filterTypes: string) => action('filter', { filter: filterTypes })
};
export const actionsWithService = {
add: (label: string) => {
return async (dispatch: Dispatch<TodoAction>, getState: () => Store) => {
const addAction = actions.add(label);
const id = addAction.id;
dispatch(addAction);
await todosService.add(id, getState().todos[id]);
};
},
edit: (id: string, label: string) => {
return async (dispatch: Dispatch<TodoAction>, getState: () => Store) => {
dispatch(actions.edit(id, label));
await todosService.edit(id, getState().todos[id]);
};
},
remove: (id: string) => {
return async (dispatch: Dispatch<TodoAction>, getState: () => Store) => {
dispatch(actions.remove(id));
await todosService.remove(id);
};
},
complete: (id: string) => {
return async (dispatch: Dispatch<TodoAction>, getState: () => Store) => {
dispatch(actions.complete(id));
await todosService.edit(id, getState().todos[id]);
};
},
clear: () => {
return async (dispatch: Dispatch<TodoAction>, getState: () => Store) => {
dispatch(actions.clear());
await todosService.editBulk(getState().todos);
};
}
};
export type ActionTypes = GenericActionTypes<typeof actions>;
export type TodoAction = GenericAction<typeof actions>;
export type TodoActionWithService = GenericAction<typeof actionsWithService>;
export type TodoActionLookup = GenericActionLookup<typeof actions>;