@@ -34,6 +34,7 @@ interface AppContextInterface extends StateInterface {
3434 loginUser : ( username : string , password : string ) => void ;
3535 logoutUser : ( ) => void ;
3636 createItem : ( fruit : Fruit ) => void ;
37+ getAllItems : ( ) => void ;
3738 adjustInventory : ( itemId : string , newUnits : number ) => void ;
3839}
3940
@@ -44,6 +45,7 @@ const AppContext = createContext<AppContextInterface>({
4445 loginUser : ( ) => null ,
4546 logoutUser : ( ) => null ,
4647 createItem : ( ) => null ,
48+ getAllItems : ( ) => null ,
4749 adjustInventory : ( ) => null ,
4850} ) ;
4951
@@ -84,8 +86,7 @@ const AppContextProvider = ({ children }: Props) => {
8486 const logoutUser = async ( ) => {
8587 startLoading ( ) ;
8688 try {
87- const response = await authFetch . post ( '/auth/logout' ) ;
88- console . log ( response ) ;
89+ await authFetch . post ( '/auth/logout' ) ;
8990 dispatch ( { type : ActionType . LOGOUT_USER } ) ;
9091 } catch ( err ) {
9192 console . log ( err ) ;
@@ -109,23 +110,31 @@ const AppContextProvider = ({ children }: Props) => {
109110
110111 const createItem = async ( fruit : Fruit ) => {
111112 if ( fruit !== 'bananas' && fruit !== 'strawberries' && fruit !== 'grapes' ) return ;
112-
113113 try {
114114 startLoading ( ) ;
115- const response = await itemFetch . post ( '/items/createItem' , {
115+ await itemFetch . post ( '/items/createItem' , {
116116 itemName : fruit ,
117117 } ) ;
118- console . log ( response . data ) ;
119-
120- setTimeout ( async ( ) => {
121- const allItemsResponse = await inventoryFetch ( '/inventory/getAllItems' ) ;
122- dispatch ( { type : ActionType . RETRIEVED_ITEMS , payload : { items : allItemsResponse . data } } ) ;
123- } , 1500 ) ;
118+ stopLoading ( ) ;
124119 } catch ( err ) {
125120 if ( err instanceof AxiosError ) {
126121 window . alert ( err . message ) ;
127122 }
128123 console . log ( err ) ;
124+ stopLoading ( ) ;
125+ }
126+ } ;
127+
128+ const getAllItems = async ( ) => {
129+ try {
130+ startLoading ( ) ;
131+ const allItemsResponse = await inventoryFetch ( '/inventory/getAllItems' ) ;
132+ console . log ( 'All Item Response' , allItemsResponse . data ) ;
133+ dispatch ( { type : ActionType . RETRIEVED_ITEMS , payload : { items : allItemsResponse . data } } ) ;
134+ stopLoading ( ) ;
135+ } catch ( err ) {
136+ console . log ( err ) ;
137+ stopLoading ( ) ;
129138 }
130139 stopLoading ( ) ;
131140 } ;
@@ -158,6 +167,7 @@ const AppContextProvider = ({ children }: Props) => {
158167 loginUser,
159168 logoutUser,
160169 createItem,
170+ getAllItems,
161171 adjustInventory,
162172 } }
163173 >
0 commit comments