-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductShowcase.tsx
69 lines (62 loc) · 2.36 KB
/
ProductShowcase.tsx
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
import React, {useEffect, useState} from "react";
import {useDispatch, useSelector} from "react-redux";
import {CircularProgress, Grid, IconButton} from "@mui/material";
import Product from "./Product";
import productInterface from "../interfaces/product";
import {RootState} from "../store";
import {productActions} from "../store/product";
import axios from "axios";
import {enqueueSnackbar} from "notistack";
import {Replay} from "@mui/icons-material";
const ProductShowcase = () => {
const dispatch = useDispatch();
const productList = useSelector((state: RootState) => state.productReducer.list);
const requestFetch = useSelector((state: RootState) => state.productReducer.requestFetch);
const [isLoading, setIsLoading] = useState(false);
const reloadHandler = () => {
dispatch(productActions.requestFetch());
}
// TODO: use redux asyncThunk? error message?
useEffect(() => {
async function fetchData() {
setIsLoading(true);
const response = await axios.get("https://vending-machine-backend-6ov3.onrender.com/api/product/getAll")
.catch((error) => {
enqueueSnackbar(error, {variant: "error"});
});
if (response) {
const productList: productInterface[] = response!.data;
dispatch(productActions.changeProductList(productList));
}
setIsLoading(false);
}
if (requestFetch) {
fetchData();
}
}, [requestFetch, dispatch]);
return (
<div className={`product-showcase ${isLoading && "justify-center"}`}>
{!isLoading && <Grid container>
{productList.map((product: productInterface, index) => {
return (
<Grid item xs={6} key={index}>
<Product product={product} key={product.name}/>
</Grid>
)
})}
</Grid>}
{isLoading &&
<CircularProgress/>
}
{!isLoading &&
<>
<div className={"grow"}/>
<IconButton onClick={reloadHandler}>
<Replay/>
</IconButton>
</>
}
</div>
);
};
export default ProductShowcase;