@@ -7,8 +7,7 @@ import React, {
77
88import uuid from 'uuid/v4' ;
99
10- const TodoContext = createContext ( null ) ;
11- const FilterContext = createContext ( null ) ;
10+ const DispatchContext = createContext ( null ) ;
1211
1312const initalTodos = [
1413 {
@@ -53,7 +52,7 @@ const todoReducer = (state, action) => {
5352 complete : false ,
5453 } ) ;
5554 default :
56- throw new Error ( ) ;
55+ return state ;
5756 }
5857} ;
5958
@@ -66,13 +65,17 @@ const filterReducer = (state, action) => {
6665 case 'SHOW_INCOMPLETE' :
6766 return 'INCOMPLETE' ;
6867 default :
69- throw new Error ( ) ;
68+ return state ;
7069 }
7170} ;
7271
7372const App = ( ) => {
74- const [ filter , filterDispatch ] = useReducer ( filterReducer , 'ALL' ) ;
7573 const [ todos , todosDispatch ] = useReducer ( todoReducer , initalTodos ) ;
74+ const [ filter , filterDispatch ] = useReducer ( filterReducer , 'ALL' ) ;
75+
76+ // Global Dispatch Function
77+ const dispatch = action =>
78+ [ todosDispatch , filterDispatch ] . forEach ( fn => fn ( action ) ) ;
7679
7780 const filteredTodos = todos . filter ( todo => {
7881 if ( filter === 'ALL' ) {
@@ -91,26 +94,22 @@ const App = () => {
9194 } ) ;
9295
9396 return (
94- < TodoContext . Provider
95- value = { { todos : filteredTodos , todosDispatch } }
96- >
97- < FilterContext . Provider value = { filterDispatch } >
98- < Filter />
99- < TodoList />
100- < AddTodo />
101- </ FilterContext . Provider >
102- </ TodoContext . Provider >
97+ < DispatchContext . Provider value = { dispatch } >
98+ < Filter />
99+ < TodoList todos = { filteredTodos } />
100+ < AddTodo />
101+ </ DispatchContext . Provider >
103102 ) ;
104103} ;
105104
106105const AddTodo = ( ) => {
107106 const [ task , setTask ] = useState ( '' ) ;
108107
109- const { todosDispatch } = useContext ( TodoContext ) ;
108+ const dispatch = useContext ( DispatchContext ) ;
110109
111110 const handleSubmit = event => {
112111 if ( task ) {
113- todosDispatch ( { type : 'ADD_TODO' , task } ) ;
112+ dispatch ( { type : 'ADD_TODO' , task } ) ;
114113 }
115114
116115 setTask ( '' ) ;
@@ -128,23 +127,19 @@ const AddTodo = () => {
128127 ) ;
129128} ;
130129
131- const TodoList = ( ) => {
132- const { todos } = useContext ( TodoContext ) ;
133-
134- return (
135- < ul >
136- { todos . map ( todo => (
137- < TodoItem key = { todo . id } todo = { todo } />
138- ) ) }
139- </ ul >
140- ) ;
141- } ;
130+ const TodoList = ( { todos } ) => (
131+ < ul >
132+ { todos . map ( todo => (
133+ < TodoItem key = { todo . id } todo = { todo } />
134+ ) ) }
135+ </ ul >
136+ ) ;
142137
143138const TodoItem = ( { todo } ) => {
144- const { todosDispatch } = useContext ( TodoContext ) ;
139+ const dispatch = useContext ( DispatchContext ) ;
145140
146141 const handleChange = ( ) =>
147- todosDispatch ( {
142+ dispatch ( {
148143 type : todo . complete ? 'UNDO_TODO' : 'DO_TODO' ,
149144 id : todo . id ,
150145 } ) ;
@@ -164,18 +159,18 @@ const TodoItem = ({ todo }) => {
164159} ;
165160
166161const Filter = ( ) => {
167- const filterDispatch = useContext ( FilterContext ) ;
162+ const dispatch = useContext ( DispatchContext ) ;
168163
169164 const handleShowAll = ( ) => {
170- filterDispatch ( { type : 'SHOW_ALL' } ) ;
165+ dispatch ( { type : 'SHOW_ALL' } ) ;
171166 } ;
172167
173168 const handleShowComplete = ( ) => {
174- filterDispatch ( { type : 'SHOW_COMPLETE' } ) ;
169+ dispatch ( { type : 'SHOW_COMPLETE' } ) ;
175170 } ;
176171
177172 const handleShowIncomplete = ( ) => {
178- filterDispatch ( { type : 'SHOW_INCOMPLETE' } ) ;
173+ dispatch ( { type : 'SHOW_INCOMPLETE' } ) ;
179174 } ;
180175
181176 return (
0 commit comments