forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddProductForm.jsx
More file actions
109 lines (101 loc) · 3.51 KB
/
Copy pathAddProductForm.jsx
File metadata and controls
109 lines (101 loc) · 3.51 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
"use client";
import React, { useState } from "react";
import Toast from "../../components/Toast";
import { createProduct, uploadProductImage } from "../../lib/products";
const AddProductForm = ({ onProductAdded }) => {
const [productName, setProductName] = useState("");
const [productPrice, setProductPrice] = useState("");
const [productImage, setProductImage] = useState(null);
const [loading, setLoading] = useState(false);
const [toast, setToast] = useState({ show: false, message: "" });
const showToast = (message) => {
setToast({ show: true, message });
setTimeout(() => setToast({ show: false, message: "" }), 3000);
};
const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);
try {
if (!productImage) {
showToast("Please select an image file");
setLoading(false);
return;
}
const imageUrl = await uploadProductImage(productImage);
await createProduct({
name: productName,
price: parseFloat(productPrice),
img: imageUrl,
});
showToast("Product added successfully!");
setProductName("");
setProductPrice("");
setProductImage(null);
onProductAdded();
} catch (error) {
showToast("Error adding product: " + error.message);
} finally {
setLoading(false);
}
};
return (
<div className="max-w-2xl mx-auto mt-10 p-8 bg-white rounded-xl shadow-lg border border-purple-500">
<h1 className="text-3xl font-bold mb-6 text-center text-purple-500">
Add New Product
</h1>
<form onSubmit={handleSubmit}>
<div className="mb-6">
<label className="block text-gray-700 text-lg font-semibold">
Product Name
</label>
<input
type="text"
className="w-full p-3 mt-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-purple-500"
value={productName}
onChange={(e) => setProductName(e.target.value)}
required
/>
</div>
<div className="mb-6">
<label className="block text-gray-700 text-lg font-semibold">
Product Price
</label>
<input
type="number"
className="w-full p-3 mt-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-purple-500"
value={productPrice}
onChange={(e) => setProductPrice(e.target.value)}
required
/>
</div>
<div className="mb-6">
<label className="block text-gray-700 text-lg font-semibold">
Product Image
</label>
<input
type="file"
accept="image/*"
className="w-full p-3 mt-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-purple-500"
onChange={(e) => setProductImage(e.target.files[0])}
required
/>
</div>
<button
type="submit"
className={`w-full py-3 mt-4 text-white font-bold bg-purple-500 rounded-md hover:bg-purple-600 focus:outline-none focus:ring-2 focus:ring-purple-500 ${
loading && "opacity-50 cursor-not-allowed"
}`}
disabled={loading}
>
{loading ? "Adding Product..." : "Add Product"}
</button>
</form>
<Toast
message={toast.message}
show={toast.show}
onClose={() => setToast({ show: false, message: "" })}
/>
</div>
);
};
export default AddProductForm;