1- import React from 'react'
2- import { useSelector } from 'react-redux'
1+ import CheckCircleIcon from '@mui/icons-material/CheckCircle' ;
2+ import React , { useEffect , useState } from 'react'
3+ import { useDispatch , useSelector } from 'react-redux'
4+ import { deleteItem , resetCart , decrementQuantity , incrementQuantity } from '../redux/amazonSlice'
35
46const Cart = ( ) => {
7+ const dispatch = useDispatch ( ) ;
58 const products = useSelector ( ( state ) => state . amazonReducer . products )
9+ const [ totalPrice , setTotalPrice ] = useState ( "" )
10+
11+ useEffect ( ( ) => {
12+ let Total = 0 ;
13+ products . map ( ( item ) => {
14+ Total += item . price * item . quantity ;
15+ return setTotalPrice ( Total . toFixed ( 2 ) )
16+ } )
17+ } , [ products ] )
618 return (
719 < div className = 'w-full bg-gray-100 p-4' >
820 < div className = 'container mx-auto h-96 grid grid-cols-5 gap-8' >
@@ -17,19 +29,76 @@ const Cart = () => {
1729 {
1830 products . map ( ( item ) => (
1931 < div className = 'w-full border-b-[1px] border-b-gray-300 p-4 flex
20- items-center gap-6' >
21- < div >
32+ items-center gap-6' >
33+ < div className = 'w-full flex items-center gap-6' >
34+ < div key = { item . id }
35+ className = 'w-1/5' >
2236 < img
2337 src = { item . image }
2438 className = 'w-full h-44 object-contain'
2539 alt = 'ProductsImgs' />
2640 </ div >
41+ < div className = 'w-3/5' >
42+ < h2 className = 'font-semibold text-lg' > { item . title } </ h2 >
43+ < p className = 'text-sm' > { item . description . subString ( 0 , 100 ) } </ p >
44+ < p className = 'text-base' > Unit Price
45+ < span className = 'font-semibold' >
46+ ${ item . price }
47+ </ span > </ p >
48+ < div className = 'bg-[#F0F2F2] flex justify-center items-center gap-1 w-24
49+ py-1 text-center drop-shadow-lg rounded-md' >
50+ < p > Qty:</ p >
51+ < p onClick = { ( ) => dispatch ( decrementQuantity ( item . id ) ) }
52+ className = 'cursor-pointer bg-gray-200 px-1 rounded-md
53+ hover:bg-gray-300' >
54+ -
55+ </ p >
56+ < p > { item . quantity } </ p >
57+ < p onClick = { ( ) => dispatch ( incrementQuantity ( item . id ) ) }
58+ className = 'cursor-pointer bg-gray-200 px-1 rounded-md
59+ hover:bg-gray-300' >
60+ +
61+ </ p >
62+ </ div >
63+ < button onClick = { ( ) => dispatch ( deleteItem ( item . id ) ) }
64+ className = 'bg-red-500 w-36 py-1 rounded-lg
65+ text-white mt-2 hover:bg-red-700
66+ active:bg-red-900 duration-300' >
67+ Delete Item
68+ </ button >
69+ </ div >
70+ < div >
71+ < p className = 'text-lg font-bodyFont font-semibold' >
72+ ${ item . price * item . quantity }
73+ </ p >
74+ </ div >
75+ </ div >
2776 </ div >
28- ) )
29- }
77+ ) ) }
78+ </ div >
79+ < div className = 'w-ful py-2' >
80+ < button onclick = { ( ) => dispatch ( resetCart ( ) ) } className = 'bg-red-500 w-36 py-2 rounded-lg text-white mt-2
81+ hover:bg-red-700 active:bg-red-900 duration-300' > Clear Cart</ button >
82+ </ div >
83+ </ div >
84+ < div className = 'w-full h-52 bg-white col-span-1 flex items-center p-4' >
85+ < div >
86+ < p className = 'flex gap-2 items-start text-sm' >
87+ < span >
88+ < CheckCircleIcon className = 'bg-white text-green-500 rounded-full' />
89+ </ span > { " " }
90+ Your order qualifies for FREE Shipping Choose this option at checkout. See details....
91+ </ p >
92+ </ div >
93+ < div >
94+ < p className = 'font-semibold px-10 py-1 flex flex-col gap-2 items-cente justify-center' >
95+ Total: < span className = 'text-lg font-bold' > ${ totalPrice } </ span > </ p >
3096 </ div >
97+ < button className = 'w-full font-medium text-base bg-gradient-to-tr
98+ from-yellow-400 to yellow-500 border hover:from-yellow-300 hover:to-yellow-600
99+ active:from-yellow-400 active:to-yellow-500 duration-200 py-1.5 rounded-md
100+ mt-3' > Proceed to Pay</ button >
31101 </ div >
32- < div className = 'w-full h-full bg-white col-span-1' > </ div >
33102 </ div >
34103 </ div >
35104 )
0 commit comments