Skip to content

Commit 0f7baab

Browse files
Merge pull request #37 from bhagirathpaliyal/ConfirmationDialog
Added: ConfirmationDialog for AddToCart and Purchase
2 parents fad5a23 + 225960a commit 0f7baab

File tree

2 files changed

+81
-48
lines changed

2 files changed

+81
-48
lines changed

src/components/ConfirmationDialog.jsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from "react";
2+
import { Dialog, DialogActions, DialogContent, DialogTitle, Button } from "@mui/material";
3+
4+
function ConfirmationDialog({ open, title, content, onConfirm, onCancel }) {
5+
return (
6+
<Dialog open={open} onClose={onCancel}>
7+
<DialogTitle>{title}</DialogTitle>
8+
<DialogContent>{content}</DialogContent>
9+
<DialogActions>
10+
<Button onClick={onCancel}>Cancel</Button>
11+
<Button variant="contained" color="primary" onClick={onConfirm}>
12+
Confirm
13+
</Button>
14+
</DialogActions>
15+
</Dialog>
16+
);
17+
}
18+
19+
export default ConfirmationDialog;

src/components/Item.jsx

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,57 @@
1-
import { useEffect, useMemo, useState } from "react";
1+
import { useEffect, useState } from "react";
22
import { useDispatch, useSelector } from "react-redux";
33
import { addToCart } from "./../store/feature/cartSlice";
4+
import { createOrder } from "./../store/feature/orderSlice";
45
import { LazyLoadImage } from "react-lazy-load-image-component";
56
import { Button } from "@mui/material";
6-
import { createOrder } from "./../store/feature/orderSlice";
77
import { getDoc } from "firebase/firestore";
8+
import ConfirmationDialog from "./ConfirmationDialog";
89

910
function Item(prop) {
1011
const [status, setStatus] = useState("Ordered");
1112
const [merchant, setMerchant] = useState(null);
12-
useEffect(() => {
13-
load()
13+
const [dialogType, setDialogType] = useState(null);
14+
const dispatch = useDispatch();
1415

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);
2225
}
26+
}
2327

24-
}, [prop.data.merchantIdRef]);
28+
load();
29+
}, [prop.data?.merchantId]);
2530

26-
const user = useSelector((state) => {
27-
return state.auth.user;
28-
});
31+
const handleAction = (action) => {
32+
setDialogType(action);
33+
};
2934

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+
};
3155

3256
return (
3357
<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) {
3963
: `https://picsum.photos/id/${prop.index}/200/300`
4064
}
4165
alt="Image"
42-
className="bg-cover w-full h-[200px] rounded-[6px]"
66+
className="bg-cover w-full h-[200px] rounded-[6px]"
4367
/>
4468
</div>
45-
<div className=" flex flex-col h-[100%] justify-between">
69+
<div className="flex flex-col h-[100%] justify-between">
4670
<h2 className="text-[12px] md:text-[15px] font-medium">
4771
{prop.data?.name}
4872
</h2>
4973
<p className="text-[12px] md:text-[15px] font-medium">{prop?.name}</p>
5074
<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>
5279
</h2>
5380

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")}>
6783
Add To Cart
6884
</Button>
69-
) : (
70-
""
7185
)}
7286

7387
{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")}>
8889
Buy Now
8990
</Button>
9091
)}
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+
/>
91105
</div>
92106
</div>
93107
);

0 commit comments

Comments
 (0)