Skip to content

Commit c7e8e71

Browse files
authored
Merge pull request #21 from oslabs-beta/client
implementing createItem functionality
2 parents 2bca0d4 + d8b7622 commit c7e8e71

5 files changed

Lines changed: 49 additions & 25 deletions

File tree

examples_new/microservices/client/src/context/actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export enum ActionType {
33
STOP_LOADING = 'STOP_LOADING',
44
LOGIN_USER = 'LOGIN_USER',
55
LOGOUT_USER = 'LOGOUT_USER',
6+
RETRIEVED_ITEMS = 'RETRIEVED_ITEMS',
67
}

examples_new/microservices/client/src/context/appContext.tsx

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,38 @@ import { createContext, useReducer, useContext, useEffect } from 'react';
22
import { ActionType } from './actions';
33
import reducer from './reducer';
44
import { customFetch } from '../util/customFetch';
5+
import { Fruit } from '../util/types';
56

67
const 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

1918
export interface StateInterface {
2019
isLoading: boolean;
2120
user: string;
22-
myItems: Item[];
23-
itemsForPurchase: Item[];
21+
items: ItemInterface[];
2422
}
2523

2624
const initialState: StateInterface = {
2725
isLoading: false,
2826
user: '',
29-
myItems: [],
30-
itemsForPurchase: [],
27+
items: [],
3128
};
3229

3330
interface 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

4239
const 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

5249
type 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 /> */}

examples_new/microservices/client/src/context/reducer.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ActionType } from './actions';
2-
import { StateInterface } from './appContext';
2+
import { StateInterface, ItemInterface } from './appContext';
33

44
type Action =
55
| {
@@ -14,6 +14,12 @@ type Action =
1414
}
1515
| {
1616
type: ActionType.LOGOUT_USER;
17+
}
18+
| {
19+
type: ActionType.RETRIEVED_ITEMS;
20+
payload: {
21+
items: ItemInterface[];
22+
};
1723
};
1824

1925
const reducer = (state: StateInterface, action: Action) => {
@@ -40,6 +46,12 @@ const reducer = (state: StateInterface, action: Action) => {
4046
user: '',
4147
isLoading: false,
4248
};
49+
case ActionType.RETRIEVED_ITEMS:
50+
return {
51+
...state,
52+
items: action.payload.items,
53+
isLoading: false,
54+
};
4355
default:
4456
return state;
4557
}

examples_new/microservices/client/src/pages/Home.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { FormEvent, useState } from 'react';
22
import { GiGrapes, GiBananaBunch, GiStrawberry } from 'react-icons/gi';
3-
4-
type Fruit = 'bananas' | 'strawberries' | 'grapes';
3+
import { Fruit } from '../util/types';
54

65
const Home = () => {
76
const [fruit, setFruit] = useState<Fruit>('bananas');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Fruit = 'bananas' | 'strawberries' | 'grapes';

0 commit comments

Comments
 (0)