-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseller.jsx
More file actions
180 lines (171 loc) · 5.33 KB
/
seller.jsx
File metadata and controls
180 lines (171 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import React, { useState } from 'react';
import './pro.css';
import tvImg from './tv1.png';
import lapImg from './lap1.png';
import mobImg from './mob1.png';
import washImg from './wash.png';
import othersImg from './others.png';
const Seller = () => {
const [form, setForm] = useState({
tvBrand: '',
tvQty: '',
laptopBrand: '',
laptopQty: '',
mobileBrand: '',
mobileQty: '',
washBrand: '',
washQty: '',
otherBrand: '',
otherQty: '',
description: ''
});
const handleChange = (e) => {
setForm({ ...form, [e.target.name]: e.target.value });
};
const handleSubmit = async (e) => {
e.preventDefault();
const user = JSON.parse(localStorage.getItem('user'));
const username = user?.username;
// Build products array
const products = [
{ type: 'tv', brand: form.tvBrand, quantity: form.tvQty, description: form.description, username },
{ type: 'laptop', brand: form.laptopBrand, quantity: form.laptopQty, description: form.description, username },
{ type: 'mobile', brand: form.mobileBrand, quantity: form.mobileQty, description: form.description, username },
{ type: 'washingMachine', brand: form.washBrand, quantity: form.washQty, description: form.description, username },
{ type: 'others', brand: form.otherBrand, quantity: form.otherQty, description: form.description, username }
];
// Filter out products with empty brand, quantity, or username
const filteredProducts = products.filter(
p => p.brand && p.quantity && p.username
);
await fetch('http://localhost:3000/add-products', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ products: filteredProducts })
});
alert('Form submitted!\n' + JSON.stringify(filteredProducts, null, 2));
};
return (
<div className="container">
<h1 className="heading">Sell Your Old Products</h1>
<form id="sellForm" onSubmit={handleSubmit}>
<div className="tv">
<h1>T.V.</h1>
<img src={tvImg} alt="TV" />
<p>
Enter Brand Name:
<input
type="text"
name="tvBrand"
placeholder="BrandName"
value={form.tvBrand}
onChange={handleChange}
/><br />
Enter Quantity:
<input
type="number"
name="tvQty"
value={form.tvQty}
onChange={handleChange}
/>
</p>
</div>
<div className="lap">
<h1>Laptop</h1>
<img src={lapImg} alt="Laptop" />
<p>
Enter Brand Name:
<input
type="text"
name="laptopBrand"
placeholder="BrandName"
value={form.laptopBrand}
onChange={handleChange}
/><br />
Enter Quantity:
<input
type="number"
name="laptopQty"
value={form.laptopQty}
onChange={handleChange}
/>
</p>
</div>
<div className="mob">
<h1>SmartPhone</h1>
<img src={mobImg} alt="SmartPhone" />
<p>
Enter Brand Name:
<input
type="text"
name="mobileBrand"
placeholder="BrandName"
value={form.mobileBrand}
onChange={handleChange}
/><br />
Enter Quantity:
<input
type="number"
name="mobileQty"
value={form.mobileQty}
onChange={handleChange}
/>
</p>
</div>
<div className="washing-machine">
<h1>Washing Machine</h1>
<img src={washImg} alt="Washing Machine" />
<p>
Enter Brand Name:
<input
type="text"
name="washBrand"
placeholder="BrandName"
value={form.washBrand}
onChange={handleChange}
/><br />
Enter Quantity:
<input
type="number"
name="washQty"
value={form.washQty}
onChange={handleChange}
/>
</p>
</div>
<div className="others">
<h1>Others</h1>
<img src={othersImg} alt="Others" />
<p>
Enter Brand Name:
<input
type="text"
name="otherBrand"
placeholder="BrandName"
value={form.otherBrand}
onChange={handleChange}
/><br />
Enter Quantity:
<input
type="number"
name="otherQty"
value={form.otherQty}
onChange={handleChange}
/>
</p>
</div>
<p>Description...</p>
<textarea
name="description"
id="description"
placeholder="Enter description"
value={form.description}
onChange={handleChange}
/>
<br /><br />
<button type="submit">Submit</button>
</form>
</div>
);
};
export default Seller;