-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOilPaintings.js
More file actions
461 lines (416 loc) · 14.8 KB
/
Copy pathOilPaintings.js
File metadata and controls
461 lines (416 loc) · 14.8 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import { useState, useEffect } from 'react';
import { useSessionId } from '../utils/session';
import Card from '@mui/material/Card';
import CardActions from '@mui/material/CardActions';
import CardContent from '@mui/material/CardContent';
import CardMedia from '@mui/material/CardMedia';
import Button from '@mui/material/Button';
import Typography from '@mui/material/Typography';
import Grid from '@mui/material/Unstable_Grid2';
import NextLink from 'next/link';
import FavoriteBorderIcon from '@mui/icons-material/FavoriteBorder';
import FavoriteIcon from '@mui/icons-material/Favorite';
import CloseIcon from '@mui/icons-material/Close';
import AddShoppingCartIcon from '@mui/icons-material/AddShoppingCart';
import DialogTitle from '@mui/material/DialogTitle';
import DialogActions from '@mui/material/DialogActions';
import Dialog from '@mui/material/Dialog';
import DialogContentText from '@mui/material/DialogContentText';
import Rating from '@mui/material/Rating';
import { styled } from '@mui/system';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import OpenInFullIcon from '@mui/icons-material/OpenInFull';
import Paginate from './Pagination';
import { averageRating } from 'utils/rating';
import { useSearchState } from 'utils/search';
import formatPrice from 'utils/formatPrice';
import { Montserrat } from 'next/font/google';
import Image from 'material-ui-image';
const montserrat = Montserrat({
weight: '600',
subsets: ['latin']
});
const OilPaintings = () => {
const [oilPs, setOilPs] = useState([]);
const [openId, setOpenId] = useState(false);
const [clicked, setClicked] = useState(false);
const [currentPage, setCurrentPage] = useState(1);
const [productsPerPage] = useState(21);
const [totalProducts, setTotalProducts] = useState(0);
const [loading, setLoading] = useState(true);
const { searchResults, setSearchResults } = useSearchState();
//handles scrolling to top when changing pages
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: 'smooth',
});
}
const userId = useSessionId();
useEffect(() => {
const getOilPs = async () => {
try {
const response = await fetch('/api/listOilPaintings');
if (response.ok) {
const oils = await response.json();
const oilData = () => oils.map((oil) => {
setOilPs(oil.products);
// Find total amount of products
setTotalProducts(oil.products.length);
})
oilData();
}
} catch (error) {
console.error('Error fetching products', error);
}
finally {
setLoading(false);
}
};
getOilPs();
}, [currentPage]);
useEffect(() => {
if (searchResults !== null && searchResults.length > 0) {
setOilPs(searchResults);
setTotalProducts(searchResults.length);
}
else {
// Set products to empty array if no search results
setOilPs([]);
setTotalProducts(0);
}
}, [searchResults]);
// Find Products to display per page
const lastProductOfPage = currentPage * productsPerPage;
const firstProductOfPage = lastProductOfPage - productsPerPage;
const pageOilPs = oilPs.slice(firstProductOfPage, lastProductOfPage);
//add logic to add to wishlist
const handleHeartClick = (productId) => {
if (clicked === productId) {
setClicked(false)
} else {
setClicked(productId)
}
};
//Dialog fns
const handleClickOpen = (productId) => {
setOpenId(productId);
};
const handleClose = () => {
setOpenId(null);
};
const theme = createTheme({
palette: {
primary: {
main: '#324E4B'
// light: will be calculated from palette.primary.main,
// dark: will be calculated from palette.primary.main,
// contrastText: will be calculated to contrast with palette.primary.main
},
secondary: {
main: '#F5C9C6'
},
warning: {
main: '#893F04'
},
info: {
main: '#fff'
}
},
});
const ExpandIconStyled = styled(Button)`
width: fit-content;
height: 40px;
visibility: hidden;
color: ${theme.palette.primary.main}
`;
const HeartIconStyled = styled(Button)`
width: fit-content;
visibility: hidden;
color: ${theme.palette.primary.main}
`;
const CartIconStyled = styled(Button)`
width: fit-content;
height: 40px;
visibility: hidden;
color: ${theme.palette.primary.main}
`;
const ContainerStyled = styled("div")`
&:hover {
.icon-button {
visibility: visible;
}
}
}
`;
const DivStyled = styled("div")`
display: flex;
justify-content: space-evenly;
`;
const oilList = () => (pageOilPs.map((product) =>
<Grid item="true" xs={12} sm={6} md={4}
sx={{ maxWidth: '100%', margin: 'auto', color: theme.palette.primary.main }}
key={product.id}
className={montserrat.className}
>
<ContainerStyled>
<Card sx={{ boxShadow: 1 }}
key={product.id}
variant='outlined'
>
<CardMedia
component="img"
image={`./uploads/${product.image}`}
alt="work portfolio"
sx={{ display: 'block', marginBottom: "-1em", objectFit: "contain", width: 300, height: 300 }}
/>
<CardContent>
<Grid item="true" p={1} m={0}>
<Grid container style={{ height: '100%' }} justifyContent="center">
<CardActions>
<DivStyled>
{/* Always display the favorite icon */}
<div>
<HeartIconStyled
sx={{
width: 'fit-content',
visibility: 'hidden',
color: '#324E4B' // Set the color here
}}
variant="text"
type="button"
className="icon-button"
onClick={() => handleAddToWishlist(userId, product, textToastFav)}
>
{isInWishlist(product.id) ? <FavoriteIcon /> : <FavoriteBorderIcon />}
</HeartIconStyled>
{/* Render the ToastContainer */}
<ToastContainer position="top-right" autoClose={2000} />
</div>
<CartIconStyled
variant="text"
className="icon-button"
onClick={() => handleAddToCartToast(product.id, userId, textToastCart)}
>
<AddShoppingCartIcon />
</CartIconStyled>
<ExpandIconStyled
variant="text"
className="icon-button"
onClick={() => handleClickOpen(product.id)}
>
<OpenInFullIcon />
</ExpandIconStyled>
<Dialog
className={montserrat.className}
open={openId === product.id}
onClose={handleClose}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
align="center"
PaperProps={{
sx: {
maxWidth: "md",
minHeight: 200
}
}}
>
<Button
variant="text"
onClick={handleClose}
id={product.id}
>
<CloseIcon />
</Button>
<DialogTitle id="alert-dialog-title" sx={{ textAlign: "center" }}>
<NextLink
sx={{ color: theme.palette.primary.main }}
href={{
pathname: "/products/[productId]",
query: { productId: product.id },
}}
passHref
overlay="true"
underline="none"
>
<Typography gutterBottom variant="h7" align="center" sx={{ color: theme.palette.primary.dark }}>
{product.name}
</Typography>
</NextLink>
</DialogTitle>
<Typography variant="h7" sx={{ color: theme.palette.secondary.dark }}>
{product.dimensions}
</Typography>
<Typography variant="h7" sx={{ color: theme.palette.primary.dark, padding: "0.5em" }}>
{product.discount && product.discount.length > 0 &&
new Date(product.discount[0].startDate) <= now &&
new Date(product.discount[0].endDate) >= now ? (
<span>
<span style={{ textDecoration: 'line-through', color: theme.palette.warning.dark }}>
${formatPrice(product.price)}
</span>
{' '}
${formatPrice(product.price - (product.price * (product.discount[0].discount / 100)))}
</span>
) : (
`$${formatPrice(product.price)}`
)}
</Typography>
<div style={{ display: "flex", padding: "1.5em" }}>
<CardMedia
component="img"
image={`./uploads/${product.image}`}
alt="work portfolio"
sx={{ display: 'block', marginBottom: "-1em", objectFit: "contain", width: 300, height: 300, paddingRight: "1em" }}
/>
<DialogContentText id="alert-dialog-description" sx={{ display: "flex", height: 300, alignItems: "center" }}>
<Typography variant="h7" sx={{ color: theme.palette.primary.dark }}>
{product.description}
</Typography>
</DialogContentText>
</div>
<DialogActions>
<Button
style={{
width: 'fit-content',
// visibility: 'hidden',
color: '#324E4B' // Set the color here
}}
variant="text"
type="button"
className="icon-button"
onClick={() => handleAddToWishlist(userId, product, textToastFav)}
>
{isInWishlist(product.id) ? <FavoriteIcon /> : <FavoriteBorderIcon />}
</Button>
{/* Render the ToastContainer */}
<ToastContainer position="top-right" autoClose={2000} />
<Button
sx={{ color: theme.palette.primary.main }}
variant="text"
className="icon-button"
onClick={() => handleAddToCartToast(product.id, userId, textToastCart)}
>
<AddShoppingCartIcon />
</Button>
</DialogActions>
</Dialog>
</DivStyled>
{/* // adjust backdrop to be transparent */}
</CardActions>
<Typography gutterBottom variant="h7" align="center" sx={{ color: theme.palette.primary.dark }}>
<NextLink
href={{
pathname: "/products/[productId]",
query: { productId: product.id },
}}
passHref
overlay="true"
underline="none"
>
{product.name}
</NextLink>
</Typography>
<Typography variant="h8" align="center" sx={{ color: theme.palette.secondary.dark }}>
{product.dimensions}
</Typography>
</Grid>
</Grid>
<Grid item="true">
<NextLink
href={{
pathname: "/products/[productId]",
query: { productId: product.id },
}}
passHref
overlay="true"
underline="none"
>
<Typography sx={{ margin: "-1em", overflow: "hidden" }}>
<Rating
id={product.id}
name="read-only"
readOnly
precision={0.1}
// how to access nested select value and average it?
value={averageRating(product.feedback)}
>
</Rating>
</Typography>
</NextLink>
</Grid>
<Grid item="true">
<Grid container sx={{ height: '100%' }} justifyContent="center">
<Typography
variant="h8"
align="center"
sx={{ margin: "-1em", color: theme.palette.primary.dark }}
>
{product.discount && product.discount.length > 0 &&
new Date(product.discount[0].startDate) <= now &&
new Date(product.discount[0].endDate) >= now ? (
<span>
<span style={{ textDecoration: 'line-through', color: theme.palette.warning.dark }}>
${formatPrice(product.price)}
</span>
{' '}
${formatPrice(product.price - (product.price * (product.discount[0].discount / 100)))}
</span>
) : (
`$${formatPrice(product.price)}`
)}
</Typography>
</Grid>
</Grid>
</CardContent>
</Card >
</ContainerStyled>
</Grid >
));
if (loading) {
return (
<ThemeProvider theme={theme}>
<Grid container
align="center"
justify-content="center"
maxWidth="90%"
paddingTop="5em"
paddingLeft="10em"
paddingRight="10em"
paddingBottom="1em"
margin="auto"
spacing={6}
>
<div>Loading...</div>
</Grid>
</ThemeProvider>
);
}
return (
<ThemeProvider theme={theme}>
<Grid container
align="center"
justify-content="center"
maxWidth="90%"
paddingTop="5em"
paddingLeft="10em"
paddingRight="10em"
paddingBottom="1em"
margin="auto"
spacing={6}
>
{totalProducts === 0 ? (<div>No results found </div>) : (oilList())}
<Paginate
count={Math.ceil(totalProducts / productsPerPage)}
page={currentPage}
onChange={(e, value) => {
setCurrentPage(value);
scrollToTop();
}}
sx={{ shape: "rounded", marginTop: "1.5em" }}
/>
</Grid>
</ThemeProvider>
);
};
export default OilPaintings;