Skip to content

Commit f6dd1eb

Browse files
authored
Merge pull request #23 from oslabs-beta/hook-it-up
services hooked - chronos configured but not working
2 parents cf92032 + 5a431fd commit f6dd1eb

40 files changed

Lines changed: 9980 additions & 553 deletions

examples_new/microservices/auth/package-lock.json

Lines changed: 1813 additions & 98 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples_new/microservices/auth/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"author": "",
1919
"license": "ISC",
2020
"dependencies": {
21+
"@chronosmicro/tracker": "^12.0.2",
2122
"@chronosrx/common": "^1.0.4",
2223
"axios": "^1.6.2",
2324
"bcryptjs": "^2.4.3",

examples_new/microservices/auth/src/app.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1+
import path from 'path';
12
import express from 'express';
23
import 'express-async-errors';
34
import dotenv from 'dotenv';
4-
dotenv.config();
5+
dotenv.config({ path: path.resolve(__dirname + '../../.env') });
56
import { NotFoundError, errorHandler } from '@chronosrx/common';
67
import authRouter from './routes/auth-router';
78
import eventRouter from './routes/event-router';
89
import cookieParser from 'cookie-parser';
910
import cors from 'cors';
1011

12+
import chronosConfig from './chronos-config';
13+
const Chronos = require('@chronosmicro/tracker');
14+
const chronos = new Chronos(chronosConfig);
15+
16+
chronos.propagate();
17+
1118
const app = express();
1219

1320
app.use(
1421
cors({
1522
credentials: true,
16-
origin: 'http://localhost:8080',
23+
origin: 'http://localhost:5000',
1724
})
1825
);
1926
app.use(express.json());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const path = require('path');
2+
require('dotenv').config({
3+
path: path.resolve(__dirname, '../../.env'),
4+
});
5+
6+
const chronosConfig = {
7+
// General configuration
8+
microservice: 'auth',
9+
interval: 5000,
10+
11+
// Mode Specific
12+
mode: 'microservices',
13+
dockerized: false,
14+
15+
database: {
16+
connection: 'REST',
17+
type: process.env.CHRONOS_DB,
18+
URI: process.env.CHRONOS_URI,
19+
},
20+
21+
notifications: [],
22+
};
23+
export default chronosConfig;

examples_new/microservices/auth/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { app } from './app';
33
import mongoose from 'mongoose';
44
import { User } from './models/user';
55

6-
const PORT = process.env.PORT || 3000;
6+
const PORT = 3000;
77

88
const start = async () => {
99
if (!process.env.MONGO_URI) throw new Error('MONGO_URI must be defined');
@@ -19,7 +19,7 @@ const start = async () => {
1919
username: 'ScrumLord',
2020
password: 'McKenzie',
2121
});
22-
await testUser.save()
22+
await testUser.save();
2323
} catch (err) {
2424
throw new DbConnectionError();
2525
}

examples_new/microservices/client/src/components/CreateItemForm.tsx

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import { FormEvent, useState } from 'react';
2-
import { GiGrapes, GiBananaBunch, GiStrawberry } from 'react-icons/gi';
32
import { Fruit } from '../util/types';
43
import { useAppContext } from '../context/appContext';
54
import { nanoid } from 'nanoid';
5+
import FruitIcon from './FruitIcon';
66

7-
const fruitOptions = ['bananas', 'strawberries', 'grapes'];
7+
const itemOptions = ['bananas', 'strawberries', 'grapes'];
88

99
const CreateItemForm = () => {
1010
const { items, createItem } = useAppContext();
11-
const itemOptions = fruitOptions.filter(fruit => {
12-
return items.findIndex(item => item.itemName === fruit) < 0;
13-
});
14-
const [fruit, setFruit] = useState<Fruit>(itemOptions[0]);
11+
const [fruit, setFruit] = useState<Fruit>('bananas');
12+
13+
console.log(items);
1514

1615
const handleSubmit = (e: FormEvent) => {
1716
e.preventDefault();
@@ -21,31 +20,18 @@ const CreateItemForm = () => {
2120
createItem(fruit);
2221
};
2322

24-
const fruitIcon = () => {
25-
const classes = 'text-6xl';
26-
27-
switch (fruit) {
28-
case 'bananas':
29-
return <GiBananaBunch className={`text-yellow-300 ${classes}`} />;
30-
case 'strawberries':
31-
return <GiStrawberry className={`text-red-300 ${classes}`} />;
32-
case 'grapes':
33-
return <GiGrapes className={`text-purple-400 ${classes}`} />;
34-
default:
35-
return <GiBananaBunch className={`text-yellow-300 ${classes}`} />;
36-
}
37-
};
38-
3923
return (
4024
<form
4125
className="flex flex-col justify-center items-center bg-white/70 text-dark py-4 px-8 rounded-md"
4226
onSubmit={e => handleSubmit(e)}
4327
>
4428
<h1 className="text-2xl font-bold">Create an Item</h1>
45-
<div className="bg-dark rounded-md p-2 mt-4 shadow-blkSm">{fruitIcon()}</div>
29+
<div className="bg-dark rounded-md p-2 mt-4 shadow-blkSm">
30+
<FruitIcon fruit={fruit} />
31+
</div>
4632
<select
4733
className="text-dark mt-4 p-1 w-full text-center rounded-md"
48-
defaultValue={itemOptions[0]}
34+
value={fruit}
4935
onChange={e => {
5036
setFruit(e.target.value as Fruit);
5137
}}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { GiBananaBunch, GiGrapes, GiStrawberry } from 'react-icons/gi';
2+
import { Fruit } from '../util/types';
3+
4+
type Props = {
5+
fruit: Fruit;
6+
};
7+
8+
const FruitIcon = ({ fruit }: Props) => {
9+
const fruitIcon = (fruit: Fruit) => {
10+
const classes = 'text-6xl';
11+
12+
switch (fruit) {
13+
case 'bananas':
14+
return <GiBananaBunch className={`text-yellow-300 ${classes}`} />;
15+
case 'strawberries':
16+
return <GiStrawberry className={`text-red-300 ${classes}`} />;
17+
case 'grapes':
18+
return <GiGrapes className={`text-purple-400 ${classes}`} />;
19+
default:
20+
return <GiBananaBunch className={`text-yellow-300 ${classes}`} />;
21+
}
22+
};
23+
24+
return <>{fruitIcon(fruit)}</>;
25+
};
26+
27+
export default FruitIcon;

examples_new/microservices/client/src/components/Item.tsx

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { useAppContext } from '../context/appContext';
2+
import { nanoid } from 'nanoid';
3+
import FruitIcon from './FruitIcon';
4+
import { Fruit } from '../util/types';
5+
6+
const ItemList = () => {
7+
const { items, adjustInventory } = useAppContext();
8+
9+
const changeInventoryAmount = (increment: boolean, itemId: string, units: number) => {
10+
if (!increment && units === 1) return;
11+
12+
adjustInventory(itemId, increment ? units + 1 : units - 1);
13+
};
14+
15+
return (
16+
<div className="w-full flex flex-col justify-center items-center mt-4">
17+
<h1 className="text-3xl font-bold">Created Items</h1>
18+
<div className="flex flex-wrap justify-center w-[85%]">
19+
{items.map(item => (
20+
<div
21+
key={nanoid()}
22+
className="bg-white/70 rounded-md m-2 p-4 min-w-[150px] flex flex-col justify-center items-center text-center"
23+
>
24+
<FruitIcon fruit={item.itemName as Fruit} />
25+
<p className="capitalize">{item.itemName}</p>
26+
<div className="my-2 border-[1px] w-full border-dark"></div>
27+
<div>
28+
<p className="my-2">
29+
In stock:{' '}
30+
<span className="text-light bg-dark p-2 rounded-md text-center">{item.units}</span>
31+
</p>
32+
<div className="flex justify-center items-center">
33+
<button
34+
className="bg-blue-400 p-2 rounded-md shadow-blkSm"
35+
onClick={() => changeInventoryAmount(false, item.id, item.units)}
36+
>
37+
-
38+
</button>
39+
<p className="mx-2"> Adjust Stock</p>
40+
<button
41+
className="bg-blue-400 p-2 rounded-md shadow-blkSm"
42+
onClick={() => changeInventoryAmount(true, item.id, item.units)}
43+
>
44+
+
45+
</button>
46+
</div>
47+
</div>
48+
</div>
49+
))}
50+
</div>
51+
</div>
52+
);
53+
};
54+
55+
export default ItemList;

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ActionType } from './actions';
33
import reducer from './reducer';
44
import { customFetch } from '../util/customFetch';
55
import { Fruit } from '../util/types';
6+
import { AxiosError } from 'axios';
67

78
const authFetch = customFetch(3000);
89
const itemFetch = customFetch(3001);
@@ -33,7 +34,7 @@ interface AppContextInterface extends StateInterface {
3334
loginUser: (username: string, password: string) => void;
3435
logoutUser: () => void;
3536
createItem: (fruit: Fruit) => void;
36-
adjustInventory: () => void;
37+
adjustInventory: (itemId: string, newUnits: number) => void;
3738
}
3839

3940
const AppContext = createContext<AppContextInterface>({
@@ -121,13 +122,31 @@ const AppContextProvider = ({ children }: Props) => {
121122
dispatch({ type: ActionType.RETRIEVED_ITEMS, payload: { items: allItemsResponse.data } });
122123
}, 1500);
123124
} catch (err) {
125+
if (err instanceof AxiosError) {
126+
window.alert(err.message);
127+
}
124128
console.log(err);
125129
}
126130
stopLoading();
127131
};
128132

129-
const adjustInventory = () => {
133+
const adjustInventory = async (itemId: string, newUnits: number) => {
130134
console.log('💥 Adjust Inventory');
135+
136+
try {
137+
const response = await inventoryFetch.patch('/inventory/updateItemInventory', {
138+
id: itemId,
139+
units: newUnits,
140+
});
141+
dispatch({
142+
type: ActionType.RETRIEVED_ITEMS,
143+
payload: {
144+
items: response.data,
145+
},
146+
});
147+
} catch (err) {
148+
console.log(err);
149+
}
131150
};
132151

133152
return (

0 commit comments

Comments
 (0)