File tree 9 files changed +165
-38
lines changed 9 files changed +165
-38
lines changed Original file line number Diff line number Diff line change @@ -13,8 +13,9 @@ import Login from "./pages/Login";
13
13
import Signup from "./pages/Signup" ;
14
14
import Contact from "./pages/Contact" ;
15
15
import Pages from "./pages/Pages" ;
16
- import Product from "./pages/products/Product" ;
17
- import Products from "./pages/products/Product" ;
16
+ import LoadingScreen from "./components/Common/LoadingScreen" ;
17
+ import Products from "./components/home/Product" ;
18
+ import Cart from "./pages/Cart" ;
18
19
19
20
const router = createBrowserRouter ( [
20
21
{
@@ -26,17 +27,22 @@ const router = createBrowserRouter([
26
27
element : < Home /> ,
27
28
} ,
28
29
{
29
- path : "product" ,
30
- element : < Products /> ,
31
- } ,
32
- {
33
- path : ":slug" ,
34
- element : < Slug /> ,
30
+ path : "products" ,
31
+ children : [
32
+ {
33
+ path : ":slug" ,
34
+ element : < Slug /> ,
35
+ } ,
36
+ ] ,
35
37
} ,
36
38
{
37
39
path : "pages" ,
38
40
element : < Pages /> ,
39
41
} ,
42
+ {
43
+ path : "cart" ,
44
+ element : < Cart /> ,
45
+ } ,
40
46
{
41
47
path : "blogs" ,
42
48
element : < Blogs /> ,
@@ -85,18 +91,7 @@ function App() {
85
91
} , [ ] ) ;
86
92
87
93
return (
88
- < >
89
- { isLoading ? (
90
- < div className = "flex h-screen items-center justify-center" >
91
- < button class = "loader__btn" >
92
- < div class = "loader" > </ div >
93
- Loading
94
- </ button >
95
- </ div >
96
- ) : (
97
- < RouterProvider router = { router } />
98
- ) }
99
- </ >
94
+ < > { isLoading ? < LoadingScreen /> : < RouterProvider router = { router } /> } </ >
100
95
) ;
101
96
}
102
97
Original file line number Diff line number Diff line change @@ -2,8 +2,8 @@ import React from "react";
2
2
3
3
export default function Footer ( ) {
4
4
return (
5
- < >
6
- < div className = "grid grid-flow-row gap-4 bg-[#EEEFFB] p-[50px] sm:grid-cols-2 md:grid-cols-4 md:place-content-center" >
5
+ < div className = "bg-[#EEEFFB]" >
6
+ < div className = "grid grid-flow-row gap-4 container p-[50px] sm:grid-cols-2 md:grid-cols-4 md:place-content-center" >
7
7
< div className = "flex flex-col gap-4" >
8
8
< p className = "text-[20px] font-bold" > Hekto</ p >
9
9
< div className = "flex" >
@@ -56,6 +56,6 @@ export default function Footer() {
56
56
</ ul >
57
57
</ div >
58
58
</ div >
59
- </ >
59
+ </ div >
60
60
) ;
61
61
}
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ import { setLogOut } from "../../redux/slice/user";
16
16
17
17
export default function Header ( ) {
18
18
const user = useSelector ( ( store ) => store . user . value ) ;
19
+ const cartItem = useSelector ( ( store ) => store . cart . value ) ;
19
20
const dispatch = useDispatch ( ) ;
20
21
21
22
const [ isMenuOpen , setIsMenuOpen ] = useState ( false ) ;
@@ -72,7 +73,7 @@ export default function Header() {
72
73
className = "cursor-pointer"
73
74
onClick = { ( ) => {
74
75
dispatch ( setLogOut ( ) ) ;
75
- localStorage . removeItem ( "token" )
76
+ localStorage . removeItem ( "token" ) ;
76
77
} }
77
78
>
78
79
Logout{ " " }
@@ -92,9 +93,14 @@ export default function Header() {
92
93
Wishlist <FaHeart className="inline-block" />
93
94
</span> */ }
94
95
95
- < span className = "font-sans" >
96
- < FaCartArrowDown className = "inline-block" />
97
- </ span >
96
+ < Link className = "relative" to = { "/cart" } >
97
+ < span className = "absolute flex items-center font-sans" >
98
+ < FaCartArrowDown className = "inline-block" />
99
+ < div className = " items-center rounded-[50%] bg-secondary px-2 text-center font-Lato text-[16px]" >
100
+ { cartItem . length }
101
+ </ div >
102
+ </ span >
103
+ </ Link >
98
104
</ div >
99
105
</ nav >
100
106
</ div >
Original file line number Diff line number Diff line change
1
+ import React from "react" ;
2
+
3
+ export default function LoadingScreen ( ) {
4
+ return (
5
+ < div className = "flex h-screen items-center justify-center" >
6
+ < button class = "loader__btn" >
7
+ < div class = "loader" > </ div >
8
+ Loading
9
+ </ button >
10
+ </ div >
11
+ ) ;
12
+ }
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import Banner from "./Banner";
2
2
import "slick-carousel/slick/slick.css" ;
3
3
import "slick-carousel/slick/slick-theme.css" ;
4
4
import Slider from "react-slick" ;
5
- import Products from "../../pages/products /Product" ;
5
+ import Products from "./Product" ;
6
6
import axios from "axios" ;
7
7
import React , { useEffect , useState } from "react" ;
8
8
import Skeleton from "react-loading-skeleton" ;
@@ -95,7 +95,7 @@ export default function Home() {
95
95
} ) }
96
96
</ Slider >
97
97
98
- < div className = "container grid gap-4 py-[116px] sm:py-[130px] md:grid-cols-2 md:py-[148px] lg:grid-cols-4 lg:py-[166px] xl:py-[188px] xxl:py-[210px]" >
98
+ < div className = "container grid gap-4 py-[116px] sm:py-[130px] md:grid-cols-2 md:py-[148px] lg:grid-cols-4 lg:py-[166px] xl:py-[188px] xxl:py-[210px]" >
99
99
{ products . map ( ( el ) => {
100
100
return (
101
101
< >
Original file line number Diff line number Diff line change 1
1
import React from "react" ;
2
2
import { FaCartPlus } from "react-icons/fa" ;
3
- import BreadCrumb from "../../components/Common/BreadCrumb" ;
3
+ import { useDispatch } from "react-redux" ;
4
+ // import BreadCrumb from "../Common/BreadCrumb";
4
5
import { useNavigate } from "react-router-dom" ;
6
+ import { addCartItem } from "../../redux/slice/cart" ;
5
7
6
8
export default function Products ( props ) {
9
+ const dispatch = useDispatch ( ) ;
7
10
const navigate = useNavigate ( ) ;
8
11
return (
9
12
< >
10
13
< div onClick = { ( ) => {
11
14
navigate ( `/products/${ props . id } ` )
12
15
} } >
13
16
14
- < div className = " group relative shadow-[0px_0px_25px_0px_rgba(0,0,0,0.1)] " >
17
+ < div className = "hover:cursor-pointer group relative shadow-[0px_0px_25px_0px_rgba(0,0,0,0.1)] " >
15
18
< div
16
19
onClick = { ( e ) => {
17
20
e . stopPropagation ( ) ;
21
+ dispatch ( addCartItem ( props ) )
18
22
} }
19
23
className = " absolute left-[11px] top-[11px] hidden h-[30px] w-[30px] items-center justify-center rounded-full border border-primary transition-all hover:cursor-pointer !hover:text-secondary group-hover:flex"
20
24
>
21
- < FaCartPlus className = "text-primary " />
25
+ < FaCartPlus className = "text-primary hover:text-secondary " />
22
26
</ div >
23
27
< img
24
28
src = { props . image }
Original file line number Diff line number Diff line change
1
+ import axios from 'axios'
2
+ import React , { useEffect } from 'react'
3
+ import { useSelector } from 'react-redux'
4
+
5
+ export default function Cart ( ) {
6
+ const cartItem = useSelector ( ( store ) => store . cart . value )
7
+
8
+ useEffect ( ( ) => {
9
+ axios . get ( "" )
10
+
11
+ } , [ ] )
12
+ return (
13
+
14
+ < >
15
+ < div >
16
+ { JSON . stringify ( cartItem ) }
17
+
18
+
19
+
20
+ </ div >
21
+ </ >
22
+ )
23
+ }
Original file line number Diff line number Diff line change 1
- import React from 'react'
1
+ import React , { useEffect , useState } from "react" ;
2
+ import axios from "axios" ;
3
+ import { useParams } from "react-router-dom" ;
4
+
5
+ import Products from "../../components/home/Product" ;
6
+ import LoadingScreen from "../../components/Common/LoadingScreen" ;
2
7
3
8
export default function Slug ( ) {
9
+ const [ isLoading , setIsLoading ] = useState ( true ) ;
10
+ const [ relatedProduct , setRelatedProduct ] = useState ( [ ] ) ;
11
+
12
+ const [ product , setProduct ] = useState ( { } ) ;
13
+ const params = useParams ( ) ;
14
+
15
+ useEffect ( ( ) => {
16
+ axios
17
+ . get ( `https://ecommerce-sagartmg2.vercel.app/api/products/${ params . slug } ` )
18
+ . then ( ( res ) => {
19
+ setProduct ( res . data . data ) ;
20
+ setIsLoading ( false ) ;
21
+ } ) ;
22
+ } , [ ] ) ;
23
+
24
+ useEffect ( ( ) => {
25
+ axios
26
+ . get ( `https://ecommerce-sagartmg2.vercel.app/api/products/trending` )
27
+ . then ( ( res ) => {
28
+ console . log ( res . data . data ) ;
29
+ setRelatedProduct ( res . data . data ) ;
30
+ setIsLoading ( false ) ;
31
+ } ) ;
32
+ } , [ ] ) ;
33
+
4
34
return (
5
- < div > Slug</ div >
6
- )
35
+ < >
36
+ < div className = "container" >
37
+ < div >
38
+ { isLoading ? (
39
+ < LoadingScreen />
40
+ ) : (
41
+ < div >
42
+ < div > Name: { product . name } </ div >
43
+ < div > Price: ${ product . price } </ div >
44
+ < div >
45
+ Image: < img src = { product . image } />
46
+ </ div >
47
+ </ div >
48
+ ) }
49
+ </ div >
50
+
51
+ < div className = "text-center font-Lato text-[30px] underline" >
52
+ Related Products
53
+ < div className = " grid gap-4 md:grid-cols-2 lg:grid-cols-4" >
54
+ { relatedProduct . map ( ( el ) => {
55
+ return (
56
+ < >
57
+ < Products
58
+ key = { el . _id }
59
+ id = { el . _id }
60
+ name = { el . name }
61
+ price = { el . price }
62
+ image = { el . image }
63
+ />
64
+ </ >
65
+ ) ;
66
+ } ) }
67
+ </ div >
68
+ </ div >
69
+ </ div >
70
+ </ >
71
+ ) ;
7
72
}
Original file line number Diff line number Diff line change @@ -2,12 +2,34 @@ import { createSlice } from '@reduxjs/toolkit'
2
2
export const CartSlice = createSlice ( {
3
3
name : 'cart' ,
4
4
initialState : {
5
- value : null
5
+ value : [
6
+
7
+
8
+ ]
6
9
} ,
7
10
reducers : {
8
-
11
+ addCartItem : ( state , action ) => {
12
+ console . log ( "Added item in Cart" ) ;
13
+ let { name, _id, price, image } = action . payload
14
+
15
+
16
+ state . value . push (
17
+ {
18
+ _id,
19
+ name,
20
+ price,
21
+ image,
22
+ quantity : "1" ,
23
+ }
24
+ )
25
+
26
+ } ,
27
+ resetCart : ( state , action ) => {
28
+ console . log ( "Cart Item reset!" ) ;
29
+ } ,
30
+
9
31
}
10
32
} )
11
33
12
- export const { setReduxCart : setReduxCart , setLogOut } = CartSlice . actions
34
+ export const { addCartItem , resetCart } = CartSlice . actions
13
35
export default CartSlice . reducer
You can’t perform that action at this time.
0 commit comments