apply SplitBillChoiceBottomSheet logic
This commit is contained in:
310
src/pages/pay/components/splitBill/EqualltyChoiceBottomSheet.tsx
Normal file
310
src/pages/pay/components/splitBill/EqualltyChoiceBottomSheet.tsx
Normal file
@@ -0,0 +1,310 @@
|
||||
import { Button } from "antd";
|
||||
import { ProBottomSheet } from "components/ProBottomSheet/ProBottomSheet.tsx";
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import PeopleIcon from "components/Icons/PeopleIcon";
|
||||
import ProInputCard from "components/ProInputCard/ProInputCard";
|
||||
import ProText from "components/ProText";
|
||||
import ProTitle from "components/ProTitle";
|
||||
import {
|
||||
selectCart,
|
||||
selectGrandTotal,
|
||||
updateSplitBillAmount,
|
||||
} from "features/order/orderSlice";
|
||||
import { useAppDispatch, useAppSelector } from "redux/hooks";
|
||||
import { ProGray1 } from "ThemeConstants";
|
||||
import PayForActions from "../../../split-bill/components/PayForActions";
|
||||
import TotalPeopleActions from "../../../split-bill/components/TotalPeopleActions";
|
||||
|
||||
interface SplitBillChoiceBottomSheetProps {
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
onSave?: (value: string) => void;
|
||||
onRemoveSplitWay?: () => void;
|
||||
}
|
||||
|
||||
interface SplitBillTmp {
|
||||
totalPeople?: number;
|
||||
payFor?: number;
|
||||
}
|
||||
|
||||
export function EqualltyChoiceBottomSheet({
|
||||
isOpen,
|
||||
onClose,
|
||||
onRemoveSplitWay,
|
||||
}: SplitBillChoiceBottomSheetProps) {
|
||||
const { t } = useTranslation();
|
||||
const dispatch = useAppDispatch();
|
||||
const { tmp } = useAppSelector(selectCart);
|
||||
const grandTotal = useAppSelector(selectGrandTotal);
|
||||
|
||||
const splitBillTmp = tmp as SplitBillTmp;
|
||||
const totalPeople = splitBillTmp?.totalPeople || 1;
|
||||
const payFor = splitBillTmp?.payFor || 1;
|
||||
|
||||
// Calculate split amount
|
||||
const splitAmount = useMemo(() => {
|
||||
if (totalPeople > 0) {
|
||||
return (grandTotal / totalPeople) * payFor;
|
||||
}
|
||||
return 0;
|
||||
}, [grandTotal, totalPeople, payFor]);
|
||||
|
||||
// Update splitBillAmount when values change
|
||||
useEffect(() => {
|
||||
if (isOpen && splitAmount > 0) {
|
||||
dispatch(updateSplitBillAmount(splitAmount));
|
||||
}
|
||||
}, [isOpen, splitAmount, dispatch]);
|
||||
|
||||
const handleSave = () => {
|
||||
dispatch(updateSplitBillAmount(splitAmount));
|
||||
onClose();
|
||||
};
|
||||
|
||||
const handleRemoveSplitWay = () => {
|
||||
dispatch(updateSplitBillAmount(0));
|
||||
onRemoveSplitWay?.();
|
||||
onClose();
|
||||
};
|
||||
|
||||
return (
|
||||
<ProBottomSheet
|
||||
isOpen={isOpen}
|
||||
onClose={onClose}
|
||||
title={t("splitBill.divideTheBillEqually")}
|
||||
showCloseButton={true}
|
||||
initialSnap={1}
|
||||
height={600}
|
||||
snapPoints={[600]}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
padding: "20px 0",
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
gap: 20,
|
||||
}}
|
||||
>
|
||||
<ProInputCard title={t("checkout.splitBill")}>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
gap: "1rem",
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
flexDirection: "row",
|
||||
justifyContent: "space-between",
|
||||
gap: "1rem",
|
||||
padding: 8,
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
flexDirection: "row",
|
||||
gap: "1rem",
|
||||
}}
|
||||
>
|
||||
<Button
|
||||
type="text"
|
||||
shape="circle"
|
||||
style={{
|
||||
backgroundColor: "rgba(95, 108, 123, 0.05)",
|
||||
position: "relative",
|
||||
top: -10,
|
||||
}}
|
||||
>
|
||||
<PeopleIcon />
|
||||
</Button>
|
||||
<ProText
|
||||
style={{
|
||||
fontSize: "1rem",
|
||||
marginTop: 3,
|
||||
color: ProGray1,
|
||||
}}
|
||||
>
|
||||
{t("checkout.totalPeople")}
|
||||
</ProText>
|
||||
</div>
|
||||
|
||||
<TotalPeopleActions />
|
||||
</div>
|
||||
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
flexDirection: "row",
|
||||
justifyContent: "space-between",
|
||||
gap: "1rem",
|
||||
padding: 8,
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
flexDirection: "row",
|
||||
gap: "1rem",
|
||||
}}
|
||||
>
|
||||
<Button
|
||||
type="text"
|
||||
shape="circle"
|
||||
style={{
|
||||
backgroundColor: "rgba(95, 108, 123, 0.05)",
|
||||
position: "relative",
|
||||
top: -10,
|
||||
}}
|
||||
>
|
||||
<PeopleIcon />
|
||||
</Button>
|
||||
<ProText
|
||||
style={{
|
||||
fontSize: "1rem",
|
||||
marginTop: 2,
|
||||
color: ProGray1,
|
||||
}}
|
||||
>
|
||||
{t("checkout.payFor")}
|
||||
</ProText>
|
||||
</div>
|
||||
|
||||
<PayForActions />
|
||||
</div>
|
||||
</div>
|
||||
</ProInputCard>
|
||||
|
||||
{/* Spinner Visualization - Blank Spin Wheel */}
|
||||
{totalPeople > 0 && (
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
gap: 12,
|
||||
padding: 16,
|
||||
alignItems: "center",
|
||||
}}
|
||||
>
|
||||
<ProTitle level={5} style={{ marginBottom: 8 }}>
|
||||
{t("splitBill.divisionPreview")}
|
||||
</ProTitle>
|
||||
<div
|
||||
style={{
|
||||
position: "relative",
|
||||
width: 200,
|
||||
height: 200,
|
||||
margin: "0 auto",
|
||||
}}
|
||||
>
|
||||
<svg
|
||||
width="200"
|
||||
height="200"
|
||||
viewBox="0 0 200 200"
|
||||
style={{
|
||||
transform: "rotate(-90deg)",
|
||||
}}
|
||||
>
|
||||
{Array.from({ length: totalPeople }).map((_, index) => {
|
||||
const anglePerSlice = 360 / totalPeople;
|
||||
const startAngle = index * anglePerSlice;
|
||||
const endAngle = (index + 1) * anglePerSlice;
|
||||
const isSelected = index < payFor;
|
||||
|
||||
// Convert angles to radians
|
||||
const startAngleRad = (startAngle * Math.PI) / 180;
|
||||
const endAngleRad = (endAngle * Math.PI) / 180;
|
||||
|
||||
// Calculate path for pie slice
|
||||
const radius = 90;
|
||||
const centerX = 100;
|
||||
const centerY = 100;
|
||||
|
||||
const x1 = centerX + radius * Math.cos(startAngleRad);
|
||||
const y1 = centerY + radius * Math.sin(startAngleRad);
|
||||
const x2 = centerX + radius * Math.cos(endAngleRad);
|
||||
const y2 = centerY + radius * Math.sin(endAngleRad);
|
||||
|
||||
const largeArcFlag = anglePerSlice > 180 ? 1 : 0;
|
||||
|
||||
const pathData = [
|
||||
`M ${centerX} ${centerY}`,
|
||||
`L ${x1} ${y1}`,
|
||||
`A ${radius} ${radius} 0 ${largeArcFlag} 1 ${x2} ${y2}`,
|
||||
"Z",
|
||||
].join(" ");
|
||||
|
||||
return (
|
||||
<path
|
||||
key={index}
|
||||
d={pathData}
|
||||
fill={isSelected ? "var(--primary)" : "rgba(0, 0, 0, 0.1)"}
|
||||
stroke="#fff"
|
||||
strokeWidth="2"
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</svg>
|
||||
{/* Center circle to make it look like a blank wheel */}
|
||||
<div
|
||||
style={{
|
||||
position: "absolute",
|
||||
top: "50%",
|
||||
left: "50%",
|
||||
transform: "translate(-50%, -50%)",
|
||||
width: 60,
|
||||
height: 60,
|
||||
borderRadius: "50%",
|
||||
backgroundColor: "var(--background)",
|
||||
border: "3px solid var(--primary)",
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
fontSize: 18,
|
||||
fontWeight: 600,
|
||||
color: "var(--primary)",
|
||||
zIndex: 10,
|
||||
}}
|
||||
>
|
||||
{payFor}
|
||||
</div>
|
||||
</div>
|
||||
<ProText
|
||||
style={{
|
||||
textAlign: "center",
|
||||
marginTop: 12,
|
||||
fontSize: 16,
|
||||
fontWeight: 600,
|
||||
}}
|
||||
>
|
||||
{t("splitBill.yourAmount")}: {splitAmount.toFixed(2)}
|
||||
</ProText>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
gap: 12,
|
||||
marginTop: 20,
|
||||
}}
|
||||
>
|
||||
<Button style={{ flex: 1 }} onClick={handleRemoveSplitWay}>
|
||||
{t("splitBill.removeSplitWay")}
|
||||
</Button>
|
||||
<Button
|
||||
type="primary"
|
||||
style={{ flex: 1, boxShadow: "none" }}
|
||||
onClick={handleSave}
|
||||
>
|
||||
{t("cart.save")}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</ProBottomSheet>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user