source: frontend/src/screens/ProductScreen.js@ ee05663

Last change on this file since ee05663 was b612ab1, checked in by Nace Gjorgjievski <nace.gorgievski123@…>, 22 months ago

Basic functions added

  • Property mode set to 100644
File size: 3.3 KB
Line 
1import axios from "axios";
2import React, { useEffect, useReducer } from "react";
3import { Helmet } from "react-helmet-async";
4import { useParams } from "react-router-dom";
5import Footer from "../components/Footer";
6import "bootstrap/dist/css/bootstrap.min.css";
7import Header from "../components/Header";
8import icon1 from "../Images/icon3.svg";
9import ShoppingBasketIcon from "@mui/icons-material/ShoppingCart";
10import "../styles/ProductScreen.css";
11import Container from "react-bootstrap/Container";
12import Row from "react-bootstrap/Row";
13import Col from "react-bootstrap/Col";
14
15const reducer = (state, action) => {
16 switch (action.type) {
17 case "FETCH_REQUEST":
18 return { ...state, loading: true };
19 case "FETCH_SUCCESS":
20 return { ...state, product: action.payload, loading: false };
21 case "FETCH_FAIL":
22 return { ...state, loading: false, error: action.payload };
23 default:
24 return state;
25 }
26};
27
28function ProductScreen() {
29 const params = useParams();
30 const { slug } = params;
31
32 const [{ loading, error, product }, dispatch] = useReducer(reducer, {
33 product: [],
34 loading: true,
35 error: "",
36 });
37
38 useEffect(() => {
39 const fetchData = async () => {
40 dispatch({ type: "FETCH_REQUEST" });
41 try {
42 const result = await axios.get(`/api/products/slug/${slug}`);
43 dispatch({ type: "FETCH_SUCCESS", payload: result.data });
44 } catch (err) {
45 dispatch({ type: "FETCH_FAIL", payload: err.message });
46 }
47 };
48 fetchData();
49 }, [slug]);
50
51 return loading ? (
52 <div>Loading...</div>
53 ) : error ? (
54 <div>{error}</div>
55 ) : (
56 <div>
57 <Helmet>
58 <title>{product.name}</title>
59 </Helmet>
60 <div>
61 <Header />
62 <Container>
63 <div className="left-side">
64 <div className="mainImg">
65 <img src={product.image} alt={product.name}></img>
66 </div>
67 <div className="sideImgContainer">
68 <div className="sideImg">
69 <img src={product.image} alt={product.name}></img>
70 </div>
71 <div className="sideImg">
72 <img src={product.sideImage} alt={product.name}></img>
73 </div>
74 </div>
75 </div>
76 <div className="right-side">
77 <div className="name-container">
78 <h2>{product.name}</h2>
79 </div>
80 <div className="price-container">
81 <div className="price-left">
82 <h2>{product.price} ден</h2>
83 <span>
84 <img src={icon1} alt="10-day-delivery"></img>Бесплатна
85 достава: 10 дена
86 </span>
87 </div>
88 <div className="price-right">
89 <button className="addToCartBtn">
90 <ShoppingBasketIcon />
91 ДОДАЈ ВО КОШНИЧКА
92 </button>
93 </div>
94 </div>
95
96 <div className="informationContainer">
97 <div className="table">
98 <div>Опис</div>
99 <div>Димензии</div>
100 <div>Монтажа</div>
101 <div>Шема за монтажа</div>
102 </div>
103 </div>
104 </div>
105 </Container>
106 <Footer />
107 </div>
108 </div>
109 );
110}
111
112export default ProductScreen;
Note: See TracBrowser for help on using the repository browser.