erp-platform/ui/src/components/orders/Cart.tsx

178 lines
6.8 KiB
TypeScript
Raw Normal View History

2025-08-17 19:07:04 +00:00
import React from 'react'
import { FaTimes, FaMinus, FaPlus, FaShoppingBag, FaTrash } from 'react-icons/fa'
import { BillingCycle, BasketItem } from '@/proxy/order/models'
import { useLocalization } from '@/utils/hooks/useLocalization'
2025-08-11 06:34:44 +00:00
interface CartState {
2025-08-17 19:07:04 +00:00
items: BasketItem[]
total: number
globalBillingCycle: BillingCycle
globalPeriod: number
2025-08-11 06:34:44 +00:00
}
interface CartProps {
2025-08-17 19:07:04 +00:00
isOpen: boolean
onClose: () => void
onCheckout: () => void
cartState: CartState
updateQuantity: (id: string, quantity: number) => void
removeItem: (id: string) => void
clearCart: () => void
2025-08-11 06:34:44 +00:00
}
2025-08-17 19:07:04 +00:00
export const Cart: React.FC<CartProps> = ({
isOpen,
onClose,
onCheckout,
cartState,
updateQuantity,
2025-08-11 06:34:44 +00:00
removeItem,
2025-08-17 19:07:04 +00:00
clearCart,
2025-08-11 06:34:44 +00:00
}) => {
2025-08-17 19:07:04 +00:00
const { translate } = useLocalization()
2025-08-11 06:34:44 +00:00
const handleClearCart = () => {
if (window.confirm('Sepetteki tüm ürünleri silmek istediğinizden emin misiniz?')) {
2025-08-17 19:07:04 +00:00
clearCart()
2025-08-11 06:34:44 +00:00
}
2025-08-17 19:07:04 +00:00
}
2025-08-11 06:34:44 +00:00
const formatPrice = (price: number) => {
return new Intl.NumberFormat('tr-TR', {
style: 'currency',
currency: 'TRY',
2025-08-17 19:07:04 +00:00
minimumFractionDigits: 0,
}).format(price)
}
2025-08-11 06:34:44 +00:00
2025-08-17 19:07:04 +00:00
if (!isOpen) return null
2025-08-11 06:34:44 +00:00
return (
<div className="fixed inset-0 z-50 overflow-hidden">
<div className="absolute inset-0 bg-black bg-opacity-50" onClick={onClose} />
2025-08-17 19:07:04 +00:00
2025-08-11 06:34:44 +00:00
<div className="absolute right-0 top-0 h-full w-full max-w-md bg-white shadow-xl">
<div className="flex flex-col h-full">
<div className="flex items-center justify-between p-6 border-b border-gray-200">
2025-08-17 19:07:04 +00:00
<h2 className="text-lg font-semibold text-gray-900">
{translate('::Public.cart.title')}
</h2>
2025-08-11 06:34:44 +00:00
<div className="flex items-center space-x-2">
{cartState.items.length > 0 && (
<button
onClick={handleClearCart}
className="p-2 text-red-500 hover:bg-red-50 rounded-lg transition-colors"
2025-08-17 19:07:04 +00:00
title={translate('::Public.cart.clear')}
2025-08-11 06:34:44 +00:00
>
2025-08-16 19:47:24 +00:00
<FaTrash className="w-5 h-5" />
2025-08-11 06:34:44 +00:00
</button>
)}
<button
onClick={onClose}
className="p-2 hover:bg-gray-100 rounded-lg transition-colors"
>
2025-08-16 19:47:24 +00:00
<FaTimes className="w-5 h-5" />
2025-08-11 06:34:44 +00:00
</button>
</div>
</div>
<div className="flex-1 overflow-y-auto p-6">
{cartState.items.length === 0 ? (
<div className="flex flex-col items-center justify-center h-full text-gray-500">
2025-08-16 19:47:24 +00:00
<FaShoppingBag className="w-16 h-16 mb-4" />
2025-08-17 19:07:04 +00:00
<p className="text-lg font-medium">{translate('::Public.cart.empty.title')}</p>
<p className="text-sm">{translate('::Public.cart.empty.subtitle')}</p>
2025-08-11 06:34:44 +00:00
</div>
) : (
<div className="space-y-4">
{cartState.items.map((item: BasketItem, index: number) => (
2025-08-17 19:07:04 +00:00
<div
key={`${item.product.id}-${item.billingCycle}-${index}`}
className="border border-gray-200 rounded-lg p-4"
>
2025-08-11 06:34:44 +00:00
<div className="flex justify-between items-start mb-2">
<h4 className="font-medium text-gray-900 text-sm leading-tight">
2025-08-17 19:07:04 +00:00
{translate('::' + item.product.name)}
2025-08-11 06:34:44 +00:00
</h4>
<button
onClick={() => removeItem(item.product.id)}
className="text-red-500 hover:text-red-700 ml-2"
>
2025-08-16 19:47:24 +00:00
<FaTimes className="w-4 h-4" />
2025-08-11 06:34:44 +00:00
</button>
</div>
2025-08-17 19:07:04 +00:00
2025-08-11 06:34:44 +00:00
<div className="flex items-center justify-between">
<div className="text-sm text-gray-600">
{item.product.isQuantityBased && item.quantity > 1 && (
<span className="font-medium text-gray-900">{item.quantity}x - </span>
)}
2025-08-17 19:07:04 +00:00
{item.billingCycle === 'monthly'
? translate('::Public.cart.monthly')
: item.billingCycle === 'yearly'
? translate('::Public.cart.yearly')
: translate('::Public.cart.monthly')}
2025-08-11 06:34:44 +00:00
{cartState.globalPeriod > 1 && item.product.isQuantityBased && (
<span className="text-xs text-gray-500">
2025-08-17 19:07:04 +00:00
({cartState.globalPeriod}{' '}
{item.billingCycle === 'monthly'
? translate('::Public.cart.month')
: translate('::Public.cart.year')}
)
2025-08-11 06:34:44 +00:00
</span>
)}
</div>
2025-08-17 19:07:04 +00:00
2025-08-11 06:34:44 +00:00
{item.product.isQuantityBased && (
<div className="flex items-center space-x-2">
<button
onClick={() => updateQuantity(item.product.id, item.quantity - 1)}
className="p-1 rounded border border-gray-300 hover:bg-gray-50"
>
2025-08-16 19:47:24 +00:00
<FaMinus className="w-3 h-3" />
2025-08-11 06:34:44 +00:00
</button>
<span className="w-8 text-center text-sm">{item.quantity}</span>
<button
onClick={() => updateQuantity(item.product.id, item.quantity + 1)}
className="p-1 rounded border border-gray-300 hover:bg-gray-50"
>
2025-08-16 19:47:24 +00:00
<FaPlus className="w-3 h-3" />
2025-08-11 06:34:44 +00:00
</button>
</div>
)}
</div>
2025-08-17 19:07:04 +00:00
2025-08-11 06:34:44 +00:00
<div className="mt-2 font-semibold text-blue-600">
{formatPrice(item.totalPrice)}
</div>
</div>
))}
</div>
)}
</div>
{cartState.items.length > 0 && (
<div className="border-t border-gray-200 p-6">
<div className="flex justify-between items-center mb-4">
2025-08-17 19:07:04 +00:00
<span className="text-lg font-semibold text-gray-900">
{translate('::Public.cart.total')}
</span>
2025-08-11 06:34:44 +00:00
<span className="text-xl font-bold text-blue-600">
{formatPrice(cartState.total)}
</span>
</div>
2025-08-17 19:07:04 +00:00
2025-08-11 06:34:44 +00:00
<button
onClick={onCheckout}
className="w-full bg-blue-600 hover:bg-blue-700 text-white font-medium py-3 px-4 rounded-lg transition-colors duration-200"
>
2025-08-17 19:07:04 +00:00
{translate('::Public.cart.checkout')}
2025-08-11 06:34:44 +00:00
</button>
</div>
)}
</div>
</div>
</div>
2025-08-17 19:07:04 +00:00
)
}