@@ -2,41 +2,38 @@ import { createContext, useReducer, useContext, useEffect } from 'react';
22import { ActionType } from './actions' ;
33import reducer from './reducer' ;
44import { customFetch } from '../util/customFetch' ;
5+ import { Fruit } from '../util/types' ;
56
67const authFetch = customFetch ( 3000 ) ;
7- // const itemFetch = customFetch(3001);
8- // const inventoryFetch = customFetch(3002);
8+ const itemFetch = customFetch ( 3001 ) ;
9+ const inventoryFetch = customFetch ( 3002 ) ;
910// const orderFetch = customFetch(3003);
1011
11- interface Item {
12+ export interface ItemInterface {
1213 id : string ;
13- seller : string ;
1414 name : string ;
15- unitPrice : number ;
16- unitsAvailable : number ;
15+ units : number ;
1716}
1817
1918export interface StateInterface {
2019 isLoading : boolean ;
2120 user : string ;
22- myItems : Item [ ] ;
23- itemsForPurchase : Item [ ] ;
21+ items : ItemInterface [ ] ;
2422}
2523
2624const initialState : StateInterface = {
2725 isLoading : false ,
2826 user : '' ,
29- myItems : [ ] ,
30- itemsForPurchase : [ ] ,
27+ items : [ ] ,
3128} ;
3229
3330interface AppContextInterface extends StateInterface {
3431 startLoading : ( ) => void ;
3532 stopLoading : ( ) => void ;
3633 loginUser : ( username : string , password : string ) => void ;
3734 logoutUser : ( ) => void ;
38- getItemsForSale : ( ) => void ;
39- getMyStoreItems : ( ) => void ;
35+ createItem : ( fruit : Fruit ) => void ;
36+ adjustInventory : ( ) => void ;
4037}
4138
4239const AppContext = createContext < AppContextInterface > ( {
@@ -45,8 +42,8 @@ const AppContext = createContext<AppContextInterface>({
4542 stopLoading : ( ) => null ,
4643 loginUser : ( ) => null ,
4744 logoutUser : ( ) => null ,
48- getItemsForSale : ( ) => null ,
49- getMyStoreItems : ( ) => null ,
45+ createItem : ( ) => null ,
46+ adjustInventory : ( ) => null ,
5047} ) ;
5148
5249type Props = {
@@ -109,14 +106,28 @@ const AppContextProvider = ({ children }: Props) => {
109106 stopLoading ( ) ;
110107 } ;
111108
112- // TODO
113- const getItemsForSale = ( ) => {
114- console . log ( 'getItemsForSale' ) ;
109+ const createItem = async ( fruit : Fruit ) => {
110+ if ( fruit !== 'bananas' && fruit !== 'strawberries' && fruit !== 'grapes' ) return ;
111+
112+ try {
113+ startLoading ( ) ;
114+ const response = await itemFetch . post ( '/items/createItem' , {
115+ itemName : fruit ,
116+ } ) ;
117+ console . log ( response . data ) ;
118+
119+ setTimeout ( async ( ) => {
120+ const allItemsResponse = await inventoryFetch ( '/inventory/getAllItems' ) ;
121+ dispatch ( { type : ActionType . RETRIEVED_ITEMS , payload : { items : allItemsResponse . data } } ) ;
122+ } , 1500 ) ;
123+ } catch ( err ) {
124+ console . log ( err ) ;
125+ }
126+ stopLoading ( ) ;
115127 } ;
116128
117- // TODO
118- const getMyStoreItems = ( ) => {
119- console . log ( 'getMyStoreItems' ) ;
129+ const adjustInventory = ( ) => {
130+ console . log ( '💥 Adjust Inventory' ) ;
120131 } ;
121132
122133 return (
@@ -127,8 +138,8 @@ const AppContextProvider = ({ children }: Props) => {
127138 stopLoading,
128139 loginUser,
129140 logoutUser,
130- getItemsForSale ,
131- getMyStoreItems ,
141+ createItem ,
142+ adjustInventory ,
132143 } }
133144 >
134145 { children } { /* <App /> */ }
0 commit comments