forked from OVDR-GRP-Team07/OVDR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClothesDetail.js.html
More file actions
289 lines (243 loc) · 10.2 KB
/
Copy pathClothesDetail.js.html
File metadata and controls
289 lines (243 loc) · 10.2 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: ClothesDetail.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: ClothesDetail.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* ClothesDetail.js - Detailed view for a single clothing item in the OVDR system.
*
* @fileoverview Displays item metadata, allows adding to closet, and shows recommendations.
* Handles multiple effects such as fetching details, recording history, and showing similar items.
*
* @author
* Peini SHE
*/
import React, { useState, useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import "./ClothesDetail.css";
/**
* ClothesDetail component to view a single clothing item's information, image, and suggestions.
*
* @component
* @param {Object} props
* @param {string} props.userId - ID of the currently logged-in user.
* @returns {JSX.Element}
*/
const ClothesDetail = ({ userId }) => {
const location = useLocation();
const navigate = useNavigate();
const { item } = location.state || {};
const [clothingData, setClothingData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState("");
const [recommendations, setRecommendations] = useState([]);
const [message, setMessage] = useState("")
const [messageType,setMessageType]=useState("");
/**
* Fetch detailed clothing information when the component mounts.
* Validates item existence before sending request.
*/
useEffect(() => {
if (!item || !item.id) {
setError("Invalid item data");
setLoading(false);
return;
}
// Fetch clothing details from Flask API
const fetchClothingDetail = async () => {
try {
const response = await fetch(`http://localhost:5000/detail/${item.id}`);
const data = await response.json();
if (response.ok) {
setClothingData(data.item);
} else {
setError(data.error || "Failed to fetch item details");
}
} catch (err) {
setError("Failed to fetch item details");
} finally {
setLoading(false);
}
};
fetchClothingDetail();
}, [item]);
/**
* Send request to Flask API to store user viewing history.
*/
useEffect(() => {
if (!clothingData || !userId) return;
console.log("Recording history:", { user_id: userId, clothing_id: clothingData.id });
const recordHistory = async () => {
try {
const response = await fetch("http://localhost:5000/add-history", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ user_id: userId, clothing_id: clothingData.id })
});
const result = await response.json();
console.log("History API Response:", result);
} catch (error) {
console.error("Failed to record history:", error);
}
};
recordHistory();
}, [clothingData, userId]);
/**
* Handle adding item to user's virtual closet.
* Verifies user and item validity, sends POST request to backend.
*/
const handleAddToCloset = async () => {
if (!clothingData || !userId) {
alert("User not logged in or item missing!");
return;
}
try {
const response = await fetch("http://localhost:5000/add-to-closet", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
user_id: userId,
clothing_id: clothingData.id,
}),
});
const result = await response.json();
if (response.ok) {
setMessage("Successfully added to Try-On Closet.");
setMessageType("success");
} else {
setMessage(result.error);
setMessageType("error");
}
// Auto clear message after 1.5 seconds
setTimeout(() => {
setMessage("");
setMessageType("");
}, 2000);
} catch (error) {
setMessage("Failed to add item.");
setMessageType("error")
// Auto clear message after 1.5 seconds
setTimeout(() => {
setMessage("");
setMessageType("");
}, 1500);
}
};
/**
* Fetch similar clothing recommendations from the backend.
*/
useEffect(() => {
if (!item || !item.id) {
setError("Invalid item data");
setLoading(false);
return;
}
const getRecommendationsSimilar = async () => {
try {
const response = await fetch(`http://127.0.0.1:5000/recommend/${item.id}`);
const data = await response.json();
if (data.error) {
setError(data.error);
setRecommendations([]);
} else {
setRecommendations(data.recommendations);
console.log("rec", data.recommendations)
setError("");
}
} catch (err) {
setError("Failed to fetch recommendations");
}
};
getRecommendationsSimilar();
}, [item]);
if (!item || !item.id) return <h2>Item not found</h2>;
if (loading) return <h2>Loading...</h2>;
if (error) return <h2>{error}</h2>;
if (!clothingData) return <h2>Item not found</h2>;
return (
<div className="tryon-container">
<header className="tryon-header">
<h1 className="logo">OVDR <span className="title">Clothes Details</span></h1>
<button className="back-btn" onClick={() => navigate(-1)}>Return</button>
</header>
{/* Main content layout */}
<div className="clothes-content">
{/* Left side: Large image */}
<div className="clothes-image">
<img src={clothingData.cloth_path} alt={clothingData.title} />
</div>
{/* Right side: Outfit details */}
<div className="clothes-info">
<h2 className="clothes-name">{clothingData.title}</h2>
{/* Display tags from caption */}
<div className="clothes-tags">
{clothingData.labels && clothingData.labels.map((label, index) => (
<span key={index} className="tag">{label}</span>
))}
</div>
{/* Add to closet button */}
<button className="add-btn" onClick={handleAddToCloset}> ⭐ Add to My Closet</button>
{/* Show success or error message */}
{message && (
<div className={`message ${messageType}`}>
{message}
</div>
)}
{/* similarity recommendation */}
<div class="similar-clothes">
<h3>You may also like</h3>
<div className="similar-list">
{recommendations.map((item) => (
<div key={item.id} className="similar-item" onClick={() => {
setLoading(true);
navigate(`/detail/${item.id}`, { state: { item } })
}}>
<img src={item.url} alt="recommendations" className="similar-img"/>
</div>
))}
</div>
</div>
</div>
</div>
{/* Footer */}
<footer className="tryon-footer">
<a href="http://cslinux.nottingham.edu.cn/~Team202407/">About Us</a>
<a href="/privacy.html" target="_blank" rel="noopener noreferrer">Privacy Policy</a>
<a href="/docs/user_manual.pdf" target="_blank" rel="noopener noreferrer">Manual</a>
<a href="/contact.html">Help and Contact</a>
<p>Developed by TEAM2024.07</p>
</footer>
</div>
);
};
export default ClothesDetail;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#App">App</a></li><li><a href="global.html#ClothesDetail">ClothesDetail</a></li><li><a href="global.html#FullCloset">FullCloset</a></li><li><a href="global.html#History">History</a></li><li><a href="global.html#Home">Home</a></li><li><a href="global.html#Login">Login</a></li><li><a href="global.html#Register">Register</a></li><li><a href="global.html#SaveImage">SaveImage</a></li><li><a href="global.html#TryOn">TryOn</a></li><li><a href="global.html#fetchImageAsBase64">fetchImageAsBase64</a></li><li><a href="global.html#root">root</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Tue Apr 01 2025 12:14:54 GMT+0800 (中国标准时间)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>