fix(cart): hide prices, discount coupon, and totals in cart when prices are disabled in catalog mode
This commit is contained in:
1 parent
ef0731ffac
commit
e7ded849f6
13 files changed
+11243
-10585
No files matched your search
@@ -9,6 +9,7 @@ import { productService } from "../lib/services/productService";
|
||||
import SafeImage from "./SafeImage";
|
||||
import { useUserStore } from "../lib/store/userStore";
|
||||
import { useSettingsStore } from "../lib/store/settingsStore";
|
||||
import { useCatalogMode } from "../lib/useCatalogMode";
|
||||
import AuthModal from "./AuthModal";
|
||||
import DeleteConfirmModal from "./DeleteConfirmModal";
|
||||
import { toast } from "sonner";
|
||||
@@ -21,6 +22,7 @@ export default function CartDrawer({ isOpen, onClose, onCheckout, onShopNavigate
|
||||
}) {
|
||||
const { items, updateQuantity, removeItem, isSubscribed, toggleSubscription, getTotal, getSubtotal, getDiscount, coupon, applyCoupon, addItem } = useCartStore();
|
||||
const { isLoggedIn } = useUserStore();
|
||||
const { showPrices } = useCatalogMode();
|
||||
const isRefillEnabled = useSettingsStore((state) => state.getText('REFILL_SUBSCRIPTION_ENABLED', 'false') === 'true');
|
||||
const refillPercent = useSettingsStore((state) => state.getText('REFILL_REWARD_PERCENT', '5'));
|
||||
const [isAuthModalOpen, setIsAuthModalOpen] = useState(false);
|
||||
@@ -168,10 +170,12 @@ export default function CartDrawer({ isOpen, onClose, onCheckout, onShopNavigate
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-left flex flex-col justify-between items-end">
|
||||
<span className="text-xs font-black text-medical-gray-900 font-vazir">{toPersian((item.product.priceValue * item.quantity).toLocaleString())}</span>
|
||||
<span className="text-[10px] text-medical-gray-400 font-bold">تومان</span>
|
||||
</div>
|
||||
{showPrices && (
|
||||
<div className="text-left flex flex-col justify-between items-end">
|
||||
<span className="text-xs font-black text-medical-gray-900 font-vazir">{toPersian((item.product.priceValue * item.quantity).toLocaleString())}</span>
|
||||
<span className="text-[10px] text-medical-gray-400 font-bold">تومان</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Delete Confirmation Overlay */}
|
||||
<AnimatePresence>
|
||||
@@ -248,48 +252,50 @@ export default function CartDrawer({ isOpen, onClose, onCheckout, onShopNavigate
|
||||
{/* Footer */}
|
||||
{items.length > 0 && (
|
||||
<div className="p-6 bg-medical-gray-50 border-t border-medical-gray-200 space-y-6">
|
||||
{/* Discount Code */}
|
||||
<div className="space-y-3">
|
||||
<div className="flex items-center gap-2 text-[10px] font-black text-medical-gray-400 uppercase tracking-widest px-2">
|
||||
<Ticket className="w-3 h-3" />
|
||||
کد تخفیف اختصاصی
|
||||
</div>
|
||||
<div className="relative">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="CANINO2024"
|
||||
value={couponCode}
|
||||
onChange={(e) => setCouponCode(e.target.value)}
|
||||
className={`w-full bg-white border ${couponStatus === 'error' ? 'border-red-300' : 'border-medical-gray-200'} rounded-2xl py-3 px-4 pr-10 text-sm font-bold focus:outline-none focus:ring-2 focus:ring-canina-blue/20 transition-all`}
|
||||
/>
|
||||
<button
|
||||
onClick={handleApplyCoupon}
|
||||
className={`absolute left-2 top-1/2 -translate-y-1/2 px-4 py-1.5 rounded-xl text-[10px] font-black transition-all ${couponStatus === 'success' ? 'bg-green-600 text-white' : 'bg-medical-gray-900 text-white hover:bg-canina-blue'}`}
|
||||
>
|
||||
{couponStatus === 'success' ? <CheckCircle2 className="w-3 h-3" /> : 'اعمال'}
|
||||
</button>
|
||||
</div>
|
||||
{coupon && (
|
||||
<div className="flex items-center justify-between p-3 bg-green-50 border border-green-100 rounded-2xl">
|
||||
<div className="flex items-center gap-2 text-[10px] font-bold text-green-600">
|
||||
<div className="w-1.5 h-1.5 rounded-full bg-green-600 animate-pulse" />
|
||||
کد {coupon.code} ({(coupon.discount < 1 ? coupon.discount * 100 : coupon.discount)} {coupon.discount < 1 ? '٪' : 'تومان'}) اعمال شد
|
||||
</div>
|
||||
{/* Discount Code (Only when prices are active) */}
|
||||
{showPrices && (
|
||||
<div className="space-y-3">
|
||||
<div className="flex items-center gap-2 text-[10px] font-black text-medical-gray-400 uppercase tracking-widest px-2">
|
||||
<Ticket className="w-3 h-3" />
|
||||
کد تخفیف اختصاصی
|
||||
</div>
|
||||
<div className="relative">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="CANINO2024"
|
||||
value={couponCode}
|
||||
onChange={(e) => setCouponCode(e.target.value)}
|
||||
className={`w-full bg-white border ${couponStatus === 'error' ? 'border-red-300' : 'border-medical-gray-200'} rounded-2xl py-3 px-4 pr-10 text-sm font-bold focus:outline-none focus:ring-2 focus:ring-canina-blue/20 transition-all`}
|
||||
/>
|
||||
<button
|
||||
onClick={() => {
|
||||
useCartStore.getState().removeCoupon();
|
||||
setCouponCode("");
|
||||
}}
|
||||
className="p-1 hover:bg-red-50 hover:text-red-500 text-medical-gray-400 rounded-lg transition-all"
|
||||
onClick={handleApplyCoupon}
|
||||
className={`absolute left-2 top-1/2 -translate-y-1/2 px-4 py-1.5 rounded-xl text-[10px] font-black transition-all ${couponStatus === 'success' ? 'bg-green-600 text-white' : 'bg-medical-gray-900 text-white hover:bg-canina-blue'}`}
|
||||
>
|
||||
<Trash2 className="w-3.5 h-3.5" />
|
||||
{couponStatus === 'success' ? <CheckCircle2 className="w-3 h-3" /> : 'اعمال'}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{coupon && (
|
||||
<div className="flex items-center justify-between p-3 bg-green-50 border border-green-100 rounded-2xl">
|
||||
<div className="flex items-center gap-2 text-[10px] font-bold text-green-600">
|
||||
<div className="w-1.5 h-1.5 rounded-full bg-green-600 animate-pulse" />
|
||||
کد {coupon.code} ({(coupon.discount < 1 ? coupon.discount * 100 : coupon.discount)} {coupon.discount < 1 ? '٪' : 'تومان'}) اعمال شد
|
||||
</div>
|
||||
<button
|
||||
onClick={() => {
|
||||
useCartStore.getState().removeCoupon();
|
||||
setCouponCode("");
|
||||
}}
|
||||
className="p-1 hover:bg-red-50 hover:text-red-500 text-medical-gray-400 rounded-lg transition-all"
|
||||
>
|
||||
<Trash2 className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Subscription Toggle */}
|
||||
{isRefillEnabled && (
|
||||
{showPrices && isRefillEnabled && (
|
||||
<div
|
||||
onClick={() => {
|
||||
if (!isLoggedIn && !isSubscribed) {
|
||||
@@ -336,25 +342,34 @@ export default function CartDrawer({ isOpen, onClose, onCheckout, onShopNavigate
|
||||
|
||||
<AuthModal isOpen={isAuthModalOpen} onClose={() => setIsAuthModalOpen(false)} />
|
||||
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between text-sm px-2">
|
||||
<span className="text-medical-gray-500 font-bold">مجموع اقلام</span>
|
||||
<span className="text-medical-gray-900 font-black font-vazir">{toPersian(getSubtotal().toLocaleString())} تومان</span>
|
||||
</div>
|
||||
{getDiscount() > 0 && (
|
||||
<div className="flex items-center justify-between text-sm text-green-600 px-2 font-black">
|
||||
<span className="font-bold">مجموع تخفیفها</span>
|
||||
<span className="font-black font-vazir">-{toPersian(getDiscount().toLocaleString())} تومان</span>
|
||||
{showPrices ? (
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between text-sm px-2">
|
||||
<span className="text-medical-gray-500 font-bold">مجموع اقلام</span>
|
||||
<span className="text-medical-gray-900 font-black font-vazir">{toPersian(getSubtotal().toLocaleString())} تومان</span>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex items-center justify-between border-t border-medical-gray-200 pt-4 px-2">
|
||||
<span className="text-lg font-black text-medical-gray-900 italic">مجموع قابل پرداخت</span>
|
||||
<div className="text-left">
|
||||
<p data-testid="cart-total-price" className="text-2xl font-black text-canina-blue tracking-tighter leading-none font-vazir">{toPersian(getTotal().toLocaleString())}</p>
|
||||
<p className="text-[10px] text-medical-gray-400 font-bold">تومان</p>
|
||||
{getDiscount() > 0 && (
|
||||
<div className="flex items-center justify-between text-sm text-green-600 px-2 font-black">
|
||||
<span className="font-bold">مجموع تخفیفها</span>
|
||||
<span className="font-black font-vazir">-{toPersian(getDiscount().toLocaleString())} تومان</span>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex items-center justify-between border-t border-medical-gray-200 pt-4 px-2">
|
||||
<span className="text-lg font-black text-medical-gray-900 italic">مجموع قابل پرداخت</span>
|
||||
<div className="text-left">
|
||||
<p data-testid="cart-total-price" className="text-2xl font-black text-canina-blue tracking-tighter leading-none font-vazir">{toPersian(getTotal().toLocaleString())}</p>
|
||||
<p className="text-[10px] text-medical-gray-400 font-bold">تومان</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="bg-white border border-medical-gray-200 rounded-2xl p-4 flex items-center justify-between">
|
||||
<span className="text-xs font-black text-medical-gray-700 font-vazir">تعداد کل اقلام انتخابی</span>
|
||||
<span className="text-sm font-black text-canina-blue font-vazir">
|
||||
{toPersian(items.reduce((acc, curr) => acc + curr.quantity, 0))} قلم کالا
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
onClick={onCheckout}
|
||||
|
||||
@@ -16,6 +16,18 @@ vi.mock('../../lib/services/productService', () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../../lib/useCatalogMode', () => ({
|
||||
useCatalogMode: vi.fn(() => ({
|
||||
isCatalogOnly: false,
|
||||
showPrices: true,
|
||||
allowCart: true,
|
||||
allowCheckout: true,
|
||||
showPreorderBtn: false,
|
||||
})),
|
||||
}));
|
||||
|
||||
import { useCatalogMode } from '../../lib/useCatalogMode';
|
||||
|
||||
const mockProduct = {
|
||||
id: 'canhydrox-gag',
|
||||
name: 'Canhydrox GAG',
|
||||
@@ -29,6 +41,18 @@ describe('CartDrawer', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(productService.getProducts).mockResolvedValue({ data: [], total: 0 });
|
||||
vi.mocked(useCatalogMode).mockReturnValue({
|
||||
isCatalogOnly: false,
|
||||
showPrices: true,
|
||||
allowCart: true,
|
||||
allowCheckout: true,
|
||||
showPreorderBtn: false,
|
||||
showOrders: true,
|
||||
showWallet: true,
|
||||
showCharity: true,
|
||||
orderDisabledTitle: '',
|
||||
orderDisabledMessage: '',
|
||||
});
|
||||
});
|
||||
|
||||
it('renders empty cart state when no items in cart', () => {
|
||||
@@ -99,4 +123,41 @@ describe('CartDrawer', () => {
|
||||
fireEvent.click(minusBtn);
|
||||
expect(mockUpdateQuantity).toHaveBeenCalledWith('canhydrox-gag', 1);
|
||||
});
|
||||
|
||||
it('hides prices, total and coupon code when showPrices is false in catalog mode', () => {
|
||||
vi.mocked(useCatalogMode).mockReturnValue({
|
||||
isCatalogOnly: true,
|
||||
showPrices: false,
|
||||
allowCart: true,
|
||||
allowCheckout: true,
|
||||
showPreorderBtn: false,
|
||||
showOrders: false,
|
||||
showWallet: false,
|
||||
showCharity: false,
|
||||
orderDisabledTitle: '',
|
||||
orderDisabledMessage: '',
|
||||
});
|
||||
|
||||
vi.mocked(useCartStore).mockReturnValue({
|
||||
items: [{ product: mockProduct, quantity: 2 }],
|
||||
updateQuantity: vi.fn(),
|
||||
removeItem: vi.fn(),
|
||||
isSubscribed: false,
|
||||
toggleSubscription: vi.fn(),
|
||||
getTotal: () => 2000,
|
||||
getSubtotal: () => 2000,
|
||||
getDiscount: () => 0,
|
||||
coupon: null,
|
||||
applyCoupon: vi.fn(),
|
||||
addItem: vi.fn(),
|
||||
} as unknown as ReturnType<typeof useCartStore>);
|
||||
|
||||
render(<CartDrawer isOpen={true} onClose={vi.fn()} onCheckout={vi.fn()} />);
|
||||
|
||||
expect(screen.getByText('Canhydrox GAG')).toBeInTheDocument();
|
||||
expect(screen.queryByText('کد تخفیف اختصاصی')).not.toBeInTheDocument();
|
||||
expect(screen.queryByText('مجموع قابل پرداخت')).not.toBeInTheDocument();
|
||||
expect(screen.getByText('تعداد کل اقلام انتخابی')).toBeInTheDocument();
|
||||
expect(screen.getByText('۲ قلم کالا')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user