1
- import { useEffect , useMemo , useState } from "react" ;
1
+ import { useEffect , useState } from "react" ;
2
2
import { useDispatch , useSelector } from "react-redux" ;
3
3
import { addToCart } from "./../store/feature/cartSlice" ;
4
+ import { createOrder } from "./../store/feature/orderSlice" ;
4
5
import { LazyLoadImage } from "react-lazy-load-image-component" ;
5
6
import { Button } from "@mui/material" ;
6
- import { createOrder } from "./../store/feature/orderSlice" ;
7
7
import { getDoc } from "firebase/firestore" ;
8
+ import ConfirmationDialog from "./ConfirmationDialog" ;
8
9
9
10
function Item ( prop ) {
10
11
const [ status , setStatus ] = useState ( "Ordered" ) ;
11
12
const [ merchant , setMerchant ] = useState ( null ) ;
12
- useEffect ( ( ) => {
13
- load ( )
13
+ const [ dialogType , setDialogType ] = useState ( null ) ;
14
+ const dispatch = useDispatch ( ) ;
14
15
15
- async function load ( ) {
16
- if ( prop . data . merchantId ) {
17
- const merchantData = await getDoc ( prop . data . merchantId ) ;
18
- setMerchant ( await merchantData . data ( ) ) ;
19
- } else {
20
- setMerchant ( null )
21
- }
16
+ const user = useSelector ( ( state ) => state . auth . user ) ;
17
+
18
+ useEffect ( ( ) => {
19
+ async function load ( ) {
20
+ if ( prop . data . merchantId ) {
21
+ const merchantData = await getDoc ( prop . data . merchantId ) ;
22
+ setMerchant ( merchantData . data ( ) ) ;
23
+ } else {
24
+ setMerchant ( null ) ;
22
25
}
26
+ }
23
27
24
- } , [ prop . data . merchantIdRef ] ) ;
28
+ load ( ) ;
29
+ } , [ prop . data ?. merchantId ] ) ;
25
30
26
- const user = useSelector ( ( state ) => {
27
- return state . auth . user ;
28
- } ) ;
31
+ const handleAction = ( action ) => {
32
+ setDialogType ( action ) ;
33
+ } ;
29
34
30
- const dispatch = useDispatch ( ) ;
35
+ const handleConfirmAction = ( ) => {
36
+ if ( dialogType === "addToCart" ) {
37
+ dispatch (
38
+ addToCart ( {
39
+ userId : user . uid ,
40
+ productRef : prop . reference ,
41
+ } )
42
+ ) ;
43
+ } else if ( dialogType === "buyNow" ) {
44
+ dispatch (
45
+ createOrder ( {
46
+ userId : user . uid ,
47
+ merchantId : prop . data . merchantId ,
48
+ productRef : prop . data . productRef ,
49
+ status,
50
+ } )
51
+ ) ;
52
+ }
53
+ setDialogType ( null ) ;
54
+ } ;
31
55
32
56
return (
33
57
< div className = "flex flex-col gap-[10px] border-[2px] border-solid border-brown-900 w-[200px] bg-secondary rounded-[6px] overflow-hidden p-[10px] relative" >
@@ -39,55 +63,45 @@ function Item(prop) {
39
63
: `https://picsum.photos/id/${ prop . index } /200/300`
40
64
}
41
65
alt = "Image"
42
- className = "bg-cover w-full h-[200px] rounded-[6px]"
66
+ className = "bg-cover w-full h-[200px] rounded-[6px]"
43
67
/>
44
68
</ div >
45
- < div className = " flex flex-col h-[100%] justify-between" >
69
+ < div className = "flex flex-col h-[100%] justify-between" >
46
70
< h2 className = "text-[12px] md:text-[15px] font-medium" >
47
71
{ prop . data ?. name }
48
72
</ h2 >
49
73
< p className = "text-[12px] md:text-[15px] font-medium" > { prop ?. name } </ p >
50
74
< h2 className = "text-[12px] md:text-[15px] " >
51
- Price : < span className = "font-medium " > { prop . data ?. price } </ span > { " " }
75
+ Price: < span className = "font-medium" > { prop . data ?. price } </ span >
76
+ </ h2 >
77
+ < h2 className = "text-[12px] md:text-[15px] " >
78
+ Merchant: < span className = "font-medium" > { merchant ? merchant . name : "Loading" } </ span >
52
79
</ h2 >
53
80
54
- { ( < h2 className = "text-[12px] md:text-[15px] " >
55
- Merchant : < span className = "font-medium " > { merchant ? merchant . name : "Loading" } </ span > { " " }
56
- </ h2 > ) }
57
-
58
- { ! user ?. isMerchant && ! prop . isCart ? (
59
- < Button
60
- variant = "contained"
61
- onClick = { ( ) => {
62
- dispatch (
63
- addToCart ( { userId : user . uid , productRef : prop . reference } )
64
- ) ;
65
- } }
66
- >
81
+ { ! user ?. isMerchant && ! prop . isCart && (
82
+ < Button variant = "contained" onClick = { ( ) => handleAction ( "addToCart" ) } >
67
83
Add To Cart
68
84
</ Button >
69
- ) : (
70
- ""
71
85
) }
72
86
73
87
{ prop . isCart && (
74
- < Button
75
- variant = "contained"
76
- onClick = { ( ) => {
77
- console . log ( prop . data . ref ) ;
78
- dispatch (
79
- createOrder ( {
80
- userId : user . uid ,
81
- merchantId : prop . data . merchantId ,
82
- productRef : prop . data . productRef ,
83
- status,
84
- } )
85
- ) ;
86
- } }
87
- >
88
+ < Button variant = "contained" onClick = { ( ) => handleAction ( "buyNow" ) } >
88
89
Buy Now
89
90
</ Button >
90
91
) }
92
+
93
+
94
+ < ConfirmationDialog
95
+ open = { ! ! dialogType }
96
+ title = { dialogType === "addToCart" ? "Confirm Add to Cart" : "Confirm Purchase" }
97
+ content = {
98
+ dialogType === "addToCart"
99
+ ? "Are you sure you want to add this item to your cart?"
100
+ : "Are you sure you want to purchase this item?"
101
+ }
102
+ onConfirm = { handleConfirmAction }
103
+ onCancel = { ( ) => setDialogType ( null ) }
104
+ />
91
105
</ div >
92
106
</ div >
93
107
) ;
0 commit comments