forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditProductForm.jsx
More file actions
140 lines (130 loc) · 4.32 KB
/
Copy pathEditProductForm.jsx
File metadata and controls
140 lines (130 loc) · 4.32 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
"use client";
import React, { useState, useEffect } from "react";
import {
getProductById,
updateProduct,
uploadProductImage,
} from "../../lib/products";
const EditProductForm = ({ productId, onProductUpdated }) => {
const [productName, setProductName] = useState("");
const [productPrice, setProductPrice] = useState("");
const [productImage, setProductImage] = useState(null);
const [existingImageUrl, setExistingImageUrl] = useState("");
const [loading, setLoading] = useState(false);
const [successMessage, setSuccessMessage] = useState("");
const [errorMessage, setErrorMessage] = useState("");
useEffect(() => {
const fetchProduct = async () => {
setLoading(true);
try {
const product = await getProductById(productId);
if (product) {
setProductName(product.name);
setProductPrice(product.price);
setExistingImageUrl(product.img);
} else {
setErrorMessage("Product not found");
}
} catch (error) {
setErrorMessage("Error fetching product: " + error.message);
} finally {
setLoading(false);
}
};
if (productId) {
fetchProduct();
}
}, [productId]);
const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);
try {
let imageUrl = existingImageUrl;
if (productImage) {
imageUrl = await uploadProductImage(productImage);
}
await updateProduct(productId, {
name: productName,
price: parseFloat(productPrice),
img: imageUrl,
});
setSuccessMessage("Product updated successfully!");
onProductUpdated();
} catch (error) {
setErrorMessage("Error updating 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">
Edit Product
</h1>
<form onSubmit={handleSubmit}>
{successMessage && (
<div className="mb-4 p-4 text-white bg-green-500 rounded-md">
{successMessage}
</div>
)}
{errorMessage && (
<div className="mb-4 p-4 text-white bg-purple-500 rounded-md">
{errorMessage}
</div>
)}
<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])}
/>
{existingImageUrl && (
<img
src={existingImageUrl}
alt="Existing product"
className="mt-4 max-w-full h-auto rounded-md"
/>
)}
</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 ? "Updating Product..." : "Update Product"}
</button>
</form>
</div>
);
};
export default EditProductForm;