Skip to content

Commit 155d17e

Browse files
committed
create item form created
1 parent c4e574d commit 155d17e

3 files changed

Lines changed: 71 additions & 50 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { FormEvent, useState } from 'react';
2+
import { GiGrapes, GiBananaBunch, GiStrawberry } from 'react-icons/gi';
3+
import { Fruit } from '../util/types';
4+
import { useAppContext } from '../context/appContext';
5+
import { nanoid } from 'nanoid';
6+
7+
const fruitOptions = ['bananas', 'strawberries', 'grapes'];
8+
9+
const CreateItemForm = () => {
10+
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]);
15+
16+
const handleSubmit = (e: FormEvent) => {
17+
e.preventDefault();
18+
19+
// TODO
20+
// SEND REQUEST TO CREATE ITEM
21+
createItem(fruit);
22+
};
23+
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+
39+
return (
40+
<form
41+
className="flex flex-col justify-center items-center bg-white/70 text-dark py-4 px-8 rounded-md"
42+
onSubmit={e => handleSubmit(e)}
43+
>
44+
<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>
46+
<select
47+
className="text-dark mt-4 p-1 w-full text-center rounded-md"
48+
defaultValue={itemOptions[0]}
49+
onChange={e => {
50+
setFruit(e.target.value as Fruit);
51+
}}
52+
>
53+
{itemOptions.map(item => {
54+
return (
55+
<option key={nanoid()} value={item} className="capitalize">
56+
{item}
57+
</option>
58+
);
59+
})}
60+
</select>
61+
<button className="bg-blue-400 py-2 px-4 rounded-md hover:shadow-blkSm hover:scale-110 mt-4 w-full">
62+
Create Item
63+
</button>
64+
</form>
65+
);
66+
};
67+
68+
export default CreateItemForm;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const inventoryFetch = customFetch(3002);
1111

1212
export interface ItemInterface {
1313
id: string;
14-
name: string;
14+
itemName: string;
1515
units: number;
1616
}
1717

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

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,9 @@
1-
import { FormEvent, useState } from 'react';
2-
import { GiGrapes, GiBananaBunch, GiStrawberry } from 'react-icons/gi';
3-
import { Fruit } from '../util/types';
1+
import CreateItemForm from '../components/CreateItemForm';
42

53
const Home = () => {
6-
const [fruit, setFruit] = useState<Fruit>('bananas');
7-
8-
const handleSubmit = (e: FormEvent) => {
9-
e.preventDefault();
10-
11-
// TODO
12-
// SEND REQUEST TO CREATE ITEM
13-
console.log('🍌 Create Item');
14-
};
15-
16-
const fruitIcon = () => {
17-
const classes = 'text-6xl';
18-
19-
switch (fruit) {
20-
case 'bananas':
21-
return <GiBananaBunch className={`text-yellow-300 ${classes}`} />;
22-
case 'strawberries':
23-
return <GiStrawberry className={`text-red-300 ${classes}`} />;
24-
case 'grapes':
25-
return <GiGrapes className={`text-purple-400 ${classes}`} />;
26-
default:
27-
return <GiBananaBunch className={`text-yellow-300 ${classes}`} />;
28-
}
29-
};
30-
314
return (
325
<div className="flex flex-col justify-center items-center">
33-
<form
34-
className="flex flex-col justify-center items-center bg-white/70 text-dark py-4 px-8 rounded-md"
35-
onSubmit={e => handleSubmit(e)}
36-
>
37-
<h1 className="text-2xl font-bold">Create an Item</h1>
38-
<div className="bg-dark rounded-md p-2 mt-4 shadow-blkSm">{fruitIcon()}</div>
39-
<select
40-
className="text-dark mt-4 p-1 w-full text-center rounded-md"
41-
value={fruit}
42-
onChange={e => {
43-
setFruit(e.target.value as Fruit);
44-
}}
45-
>
46-
<option value="bananas">Banana</option>
47-
<option value="strawberries">Strawberries</option>
48-
<option value="grapes">Grapes</option>
49-
</select>
50-
<button className="bg-blue-400 py-2 px-4 rounded-md hover:shadow-blkSm hover:scale-110 mt-4 w-full">
51-
Create Item
52-
</button>
53-
</form>
6+
<CreateItemForm />
547
</div>
558
);
569
};

0 commit comments

Comments
 (0)