mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
step 9 exercise complete!
This commit is contained in:
51
step2-09/exercise/src/actions/index.ts
Normal file
51
step2-09/exercise/src/actions/index.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import uuid from 'uuid/v4';
|
||||
import { Store } from '../store';
|
||||
import * as service from '../service';
|
||||
|
||||
export const actions = {
|
||||
addTodo: (label: string) => ({ type: 'addTodo', id: uuid(), label }),
|
||||
remove: (id: string) => ({ type: 'remove', id }),
|
||||
complete: (id: string) => ({ type: 'complete', id }),
|
||||
clear: () => ({ type: 'clear' }),
|
||||
setFilter: (filter: string) => ({ type: 'setFilter', filter })
|
||||
};
|
||||
|
||||
export const actionsWithService = {
|
||||
addTodo: (label: string) => {
|
||||
return async (dispatch: any, getState: () => Store) => {
|
||||
// Replace the return true with:
|
||||
// 1. first call the actions.addTodo() function
|
||||
// 2. store the resultant id
|
||||
// 3. dispatch the action message generated by that call
|
||||
// 4. pass the id and the todo from the state into the service call service.add()
|
||||
return true;
|
||||
};
|
||||
},
|
||||
|
||||
remove: (id: string) => {
|
||||
return async (dispatch: any, getState: () => Store) => {
|
||||
// Replace the return true with:
|
||||
// 1. dispatch a remove action with the id
|
||||
// 2. await on the call to the service.remove()
|
||||
return true;
|
||||
};
|
||||
},
|
||||
|
||||
complete: (id: string) => {
|
||||
// ** Now it's your turn to write the thunk! **
|
||||
// Replace the return Promise.resolve(true) with:
|
||||
// 1. return an async function with the arguments of dispatch and getState
|
||||
// 2. dispatch a remove action with the id
|
||||
// 3. await on the call to the service.update()
|
||||
return Promise.resolve(true);
|
||||
},
|
||||
|
||||
clear: () => {
|
||||
// ** Write your own thunk again! **
|
||||
// Replace the return Promise.resolve(true) with:
|
||||
// 1. return an async function with the arguments of dispatch and getState
|
||||
// 2. dispatch a clear action
|
||||
// 3. await on the call to the service.updateAll()
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user