source: components/BuildDetailsDialog.tsx@ 4f2900a

main
Last change on this file since 4f2900a was d07d68c, checked in by Mihail <mihail2.naumov@…>, 5 months ago

Fixed clone auth errors and component names in the component dialogs.

  • Property mode set to 100644
File size: 20.9 KB
Line 
1import React, {useEffect, useState} from 'react';
2import {
3 Dialog, DialogTitle, DialogContent, DialogActions, Button, Box, Typography,
4 IconButton, Tab, Tabs, Table, TableBody, TableCell, TableRow, Rating, TextField, Avatar, Chip, Alert, Snackbar
5} from '@mui/material';
6import CloseIcon from '@mui/icons-material/Close';
7import AutoFixHighIcon from '@mui/icons-material/AutoFixHigh';
8import FavoriteIcon from '@mui/icons-material/Favorite';
9import FavoriteBorderIcon from '@mui/icons-material/FavoriteBorder';
10import PersonIcon from "@mui/icons-material/Person";
11import {onGetBuildDetails, onSetReview, onToggleFavorite, onCloneBuild, onSetRating} from '../pages/+Layout.telefunc';
12import {onGetBuildState} from '../pages/forge/forge.telefunc';
13
14const formatPrice = (price: any) => new Intl.NumberFormat('en-US', {
15 style: 'currency',
16 currency: 'USD'
17}).format(Number(price) || 0);
18
19export default function BuildDetailsDialog({open, buildId, onClose, currentUser, isDashboardView = false}: {
20 open: boolean;
21 buildId: number | null;
22 onClose: () => void;
23 currentUser: any;
24 isDashboardView?: boolean;
25}) {
26 const [details, setDetails] = useState<any>(null);
27 const [loading, setLoading] = useState(false);
28 const [tabIndex, setTabIndex] = useState(0);
29 const [cloneDialogOpen, setCloneDialogOpen] = useState(false);
30 const [cloningBuildId, setCloningBuildId] = useState<number | null>(null);
31 const [isOwner, setIsOwner] = useState(false);
32
33 const [reviewText, setReviewText] = useState("");
34 const [ratingVal, setRatingVal] = useState(5);
35
36 const [snackbar, setSnackbar] = useState<{
37 open: boolean;
38 message: string;
39 severity: 'error' | 'warning' | 'info' | 'success';
40 }>({
41 open: false,
42 message: '',
43 severity: 'warning'
44 });
45
46 useEffect(() => {
47 if (open && buildId !== null) {
48 setLoading(true);
49 setReviewText("");
50 setRatingVal(5);
51
52 onGetBuildDetails({buildId})
53 .then(data => {
54 setDetails(data);
55 if (data.userRating) setRatingVal(data.userRating);
56 if (data.userReview) setReviewText(data.userReview);
57 })
58 .finally(() => setLoading(false));
59 }
60 }, [open, buildId]);
61
62 useEffect(() => {
63 if (open && buildId !== null) {
64 onGetBuildState({buildId})
65 .then(state => {
66 setIsOwner(!!state);
67 })
68 .catch(() => setIsOwner(false));
69 } else {
70 setIsOwner(false);
71 }
72 }, [open, buildId]);
73
74 useEffect(() => {
75 if (open) {
76 setTabIndex(0);
77 }
78 }, [open, buildId]);
79
80
81 const handleFavorite = async () => {
82 if (!currentUser || buildId === null) return alert("Please login to favorite builds.");
83 const res = await onToggleFavorite({buildId});
84 setDetails((prev: any) => ({...prev, isFavorite: res}));
85 };
86
87 const handleSubmitReview = async () => {
88 if (!currentUser || buildId === null) return alert("Please login to review.");
89
90 await onSetReview({
91 buildId,
92 content: reviewText,
93 });
94
95 await onSetRating({
96 buildId,
97 value: ratingVal
98 });
99
100 const refreshed = await onGetBuildDetails({buildId});
101 setDetails(refreshed);
102 };
103
104 const handleCloneConfirm = async () => {
105 if (!cloningBuildId) return;
106
107 if(!currentUser){
108 window.location.href="/auth/login";
109 return;
110 }
111
112 try {
113 const newBuildId = await onCloneBuild({buildId: cloningBuildId});
114 window.location.href = `/forge?buildId=${newBuildId}`;
115 setCloneDialogOpen(false);
116 setCloningBuildId(null);
117 } catch (e) {
118 // alert("Failed to clone build. Please try again.");
119 setCloneDialogOpen(false);
120 setSnackbar({
121 open: true,
122 message: 'Failed to clone build. Please try again!',
123 severity: 'error'
124 })
125 }
126 };
127
128 const handleCloneClick = () => {
129 if(!currentUser){
130 window.location.href="/auth/login";
131 return;
132 }
133
134 setCloningBuildId(details.id);
135 setCloneDialogOpen(true);
136 }
137
138 if (!open) return null;
139
140 return (
141 <>
142 <Dialog open={open} onClose={onClose} maxWidth="md" fullWidth scroll="body">
143 {loading || !details ? (
144 <Box sx={{p: 5, textAlign: 'center'}}>Loading Forge Schematics...</Box>
145 ) : (
146 <>
147 <DialogTitle sx={{
148 display: 'flex',
149 justifyContent: 'space-between',
150 alignItems: 'center',
151 bgcolor: '#ff8201'
152 }}>
153 <Box>
154 <Typography variant="h5" fontWeight="bold">{details.name}</Typography>
155 <Box sx={{display: 'flex', alignItems: 'center', gap: 1}}>
156 <PersonIcon sx={{fontSize: 16}}/>
157 <Typography variant="subtitle2" color="text.secondary" fontWeight="bold">
158 by {details.creator}
159 </Typography>
160 <Chip label={formatPrice(details.totalPrice)} size="small" color="primary"
161 variant="outlined"/>
162 </Box>
163 </Box>
164 <IconButton onClick={onClose}><CloseIcon/></IconButton>
165 </DialogTitle>
166
167 <DialogContent sx={{p: 0}}>
168 <Box sx={{
169 borderBottom: 1,
170 borderColor: 'divider',
171 px: 2,
172 bgcolor: 'primary',
173 position: 'sticky',
174 top: 0,
175 zIndex: 1
176 }}>
177 <Tabs value={tabIndex} onChange={(_, v) => setTabIndex(v)}>
178 <Tab label="Specs"/>
179 <Tab label={`Reviews (${details.ratingStatistics.ratingCount})`}/>
180 </Tabs>
181 </Box>
182
183 <Box sx={{p: 3}}>
184 {tabIndex === 0 && (
185 <Box
186 sx={{
187 display: 'grid',
188 gridTemplateColumns: {
189 xs: '1fr',
190 md: '2fr 1fr'
191 },
192 gap: 2,
193 width: '100%'
194 }}
195 >
196 <Box>
197 <Table size="small">
198 <TableBody>
199 {details.components.map((comp: any) => (
200 <TableRow key={comp.id}>
201 <TableCell sx={{width: 50}}>
202 <Avatar
203 src={comp.imgUrl || undefined}
204 variant="rounded"
205 sx={{width: 50, height: 50, bgcolor: '#ff8201'}}
206 >
207 {comp.type?.substring(0, 3)?.toUpperCase()}
208 </Avatar>
209 </TableCell>
210 <TableCell>
211 <Typography variant="body2" color="text.secondary" sx={{
212 fontSize: '0.75rem',
213 textTransform: 'uppercase'
214 }}>
215 {comp.type}
216 </Typography>
217 <Typography variant="body1" fontWeight="500">
218 {comp.brand} {comp.name}
219 </Typography>
220 </TableCell>
221 <TableCell align="right"
222 sx={{fontWeight: 'bold', color: '#ff8201'}}>
223 {formatPrice(comp.price)}
224 </TableCell>
225 </TableRow>
226 ))}
227 <TableRow sx={{bgcolor: '#424343'}}>
228 <TableCell colSpan={2} sx={{
229 fontWeight: 'bold',
230 color: '#ff8201'
231 }}>TOTAL</TableCell>
232 <TableCell align="right" sx={{
233 fontWeight: 'bold',
234 fontSize: '1.1rem',
235 color: 'primary.main'
236 }}>
237 {formatPrice(details.totalPrice)}
238 </TableCell>
239 </TableRow>
240 </TableBody>
241 </Table>
242 </Box>
243
244 <Box>
245 <Box sx={{bgcolor: '#424343', p: 2, borderRadius: 2, mb: 2}}>
246 <Typography color="primary.main" gutterBottom fontWeight="bold">
247 Builder's Notes
248 </Typography>
249 <Typography color="primary.main" variant="body2"
250 sx={{fontStyle: 'italic'}}>
251 "{details.description || "No notes provided."}"
252 </Typography>
253 </Box>
254
255 <Box sx={{display: 'flex', flexDirection: 'column', gap: 1}}>
256 {isDashboardView && isOwner ? (
257 <Button
258 variant="contained"
259 color="primary"
260 size="large"
261 startIcon={<AutoFixHighIcon/>}
262 onClick={() => {
263 window.location.href = `/forge?buildId=${details.id}`;
264 onClose();
265 }}
266 >
267 Edit Build
268 </Button>
269 ) : (
270 <Button
271 variant="contained"
272 color="primary"
273 size="large"
274 startIcon={<AutoFixHighIcon/>}
275 onClick={handleCloneClick}
276 >
277 Clone & Edit
278 </Button>
279 )}
280 <Button
281 variant={details.isFavorite ? "contained" : "outlined"}
282 color={details.isFavorite ? "error" : "primary"}
283 startIcon={details.isFavorite ? <FavoriteIcon/> :
284 <FavoriteBorderIcon/>}
285 onClick={handleFavorite}
286 >
287 {details.isFavorite ? "Favorited" : "Add to Favorites"}
288 </Button>
289 </Box>
290 </Box>
291 </Box>
292 )}
293
294 {tabIndex === 1 && (
295 <Box>
296 <Box sx={{
297 display: 'flex',
298 alignItems: 'center',
299 gap: 2,
300 mb: 4,
301 p: 2,
302 bgcolor: '#5e5e5e',
303 borderRadius: 2
304 }}>
305 <Typography variant="h3"
306 fontWeight="bold">{details.ratingStatistics.averageRating.toFixed(1)}</Typography>
307 <Box>
308 <Rating value={details.ratingStatistics.averageRating} readOnly
309 precision={0.5}/>
310 <Typography variant="body2"
311 color="text.secondary">{details.ratingStatistics.ratingCount} ratings</Typography>
312 </Box>
313 </Box>
314
315 {currentUser && details.userId !== currentUser.id && (
316 <Box sx={{mb: 4, p: 2, border: '1px solid #ddd', borderRadius: 2}}>
317 <Typography variant="subtitle2" gutterBottom>Your Review</Typography>
318 <Box sx={{display: 'flex', alignItems: 'center', mb: 1}}>
319 <Rating value={ratingVal}
320 onChange={(_, v) => setRatingVal(v || 5)}/>
321 </Box>
322 <TextField
323 fullWidth
324 multiline
325 rows={2}
326 placeholder="Share your thoughts on this build..."
327 value={reviewText}
328 onChange={(e) => setReviewText(e.target.value)}
329 sx={{mb: 1}}
330 />
331 <Button size="small" variant="contained" onClick={handleSubmitReview}>
332 Submit Review
333 </Button>
334 </Box>
335 )}
336
337 {currentUser && details.userId === currentUser.id && (
338 <Alert severity="info" sx={{mb: 4}}>
339 You cannot rate your own builds.
340 </Alert>
341 )}
342
343 <Box sx={{display: 'flex', flexDirection: 'column', gap: 2}}>
344 {details.reviews.map((rev: any, i: number) => (
345 <Box key={i} sx={{pb: 2, borderBottom: '1px solid #eee'}}>
346 <Box sx={{
347 display: 'flex',
348 justifyContent: 'space-between',
349 mb: 0.5
350 }}>
351 <Typography fontWeight="bold"
352 variant="body2">{rev.username}</Typography>
353 <Typography variant="caption"
354 color="text.secondary">{rev.createdAt}</Typography>
355 </Box>
356 <Typography variant="body2">{rev.content}</Typography>
357 </Box>
358 ))}
359 {details.reviews.length === 0 && (
360 <Typography color="text.secondary" align="center">
361 No reviews yet. Be the first!
362 </Typography>
363 )}
364 </Box>
365 </Box>
366 )}
367 </Box>
368 </DialogContent>
369 </>
370 )}
371 </Dialog>
372
373 <Dialog open={cloneDialogOpen} onClose={() => setCloneDialogOpen(false)}>
374 <DialogTitle>Clone Build</DialogTitle>
375 <DialogContent>
376 <Typography>
377 This will create a copy of "{details?.name}" in your Forge so you can edit it.
378 All components will be copied over.
379 </Typography>
380 </DialogContent>
381 <DialogActions>
382 <Button onClick={() => setCloneDialogOpen(false)}>Cancel</Button>
383 <Button
384 variant="contained"
385 onClick={handleCloneConfirm}
386 disabled={!cloningBuildId}
387 >
388 Clone Build
389 </Button>
390 </DialogActions>
391 </Dialog>
392 <Snackbar
393 open={snackbar.open}
394 autoHideDuration={5000}
395 onClose={() => setSnackbar(prev => ({...prev, open: false}))}
396 anchorOrigin={{vertical: 'bottom', horizontal: 'center'}}
397 >
398 <Alert
399 onClose={() => setSnackbar(prev => ({...prev, open: false}))}
400 severity={snackbar.severity}
401 variant="filled"
402 sx={{width: '100%'}}
403 >
404 {snackbar.message}
405 </Alert>
406 </Snackbar>
407 </>
408 );
409}
Note: See TracBrowser for help on using the repository browser.