update order

This commit is contained in:
Samandar Turgunboyev
2025-12-25 14:40:25 +05:00
parent af7b9fd590
commit 9c89570c4a
12 changed files with 768 additions and 510 deletions

View File

@@ -2,10 +2,11 @@
import { cart_api, OrderCreateBody } from '@/features/cart/lib/api';
import { orderForm } from '@/features/cart/lib/form';
import formatPhone from '@/shared/lib/formatPhone';
import formatDate from '@/shared/lib/formatDate';
import formatPrice from '@/shared/lib/formatPrice';
import onlyNumber from '@/shared/lib/onlyNumber';
import { cn } from '@/shared/lib/utils';
import { Button } from '@/shared/ui/button';
import { Calendar } from '@/shared/ui/calendar';
import {
Form,
FormControl,
@@ -15,6 +16,14 @@ import {
} from '@/shared/ui/form';
import { Input } from '@/shared/ui/input';
import { Label } from '@/shared/ui/label';
import { Popover, PopoverContent, PopoverTrigger } from '@/shared/ui/popover';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/shared/ui/select';
import { Textarea } from '@/shared/ui/textarea';
import { zodResolver } from '@hookform/resolvers/zod';
import {
@@ -26,16 +35,13 @@ import {
} from '@pbe/react-yandex-maps';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import {
Calendar as CalIcon,
CheckCircle2,
Clock,
CreditCard,
Loader2,
LocateFixed,
MapPin,
Package,
Truck,
User,
Wallet,
} from 'lucide-react';
import { useTranslations } from 'next-intl';
import Image from 'next/image';
@@ -45,6 +51,15 @@ import { toast } from 'sonner';
import z from 'zod';
import useOrderStore from '../lib/order';
const deliveryTimeSlots = [
{ id: 1, label: '09:00 - 11:00', start: '09:00', end: '11:00' },
{ id: 2, label: '11:00 - 13:00', start: '11:00', end: '13:00' },
{ id: 3, label: '13:00 - 15:00', start: '13:00', end: '15:00' },
{ id: 4, label: '15:00 - 17:00', start: '15:00', end: '17:00' },
{ id: 5, label: '17:00 - 19:00', start: '17:00', end: '19:00' },
{ id: 6, label: '19:00 - 21:00', start: '19:00', end: '21:00' },
];
interface CoordsData {
lat: number;
lon: number;
@@ -52,6 +67,8 @@ interface CoordsData {
}
const RefreshOrder = () => {
const [deliveryDate, setDeliveryDate] = useState<Date>();
const [selectedTimeSlot, setSelectedTimeSlot] = useState<string>('');
const { order: initialValues } = useOrderStore();
const t = useTranslations();
const queryClient = useQueryClient();
@@ -73,28 +90,13 @@ const RefreshOrder = () => {
const form = useForm<z.infer<typeof orderForm>>({
resolver: zodResolver(orderForm),
defaultValues: {
username: initialValues?.name,
comment: initialValues?.comment,
lat: '41.311081',
long: '69.240562',
phone: initialValues?.contact_number,
},
});
const [orderSuccess, setOrderSuccess] = useState(false);
const [paymentMethod, setPaymentMethod] = useState<'CASH' | 'ACCOUNT_NUMBER'>(
'CASH',
);
const [deliveryMethod, setDeliveryMethod] = useState<
'YandexGo' | 'DELIVERY_COURIES' | 'PICKUP'
>('DELIVERY_COURIES');
useEffect(() => {
if (initialValues) {
setPaymentMethod(initialValues.payment_type);
setDeliveryMethod(initialValues.delivery_type);
}
}, [initialValues]);
const subtotal = cartItems
? cartItems.reduce(
@@ -103,16 +105,7 @@ const RefreshOrder = () => {
)
: 0;
const deliveryFee =
deliveryMethod === 'DELIVERY_COURIES'
? subtotal > 50000
? 0
: 15000
: deliveryMethod === 'YandexGo'
? 25000
: 0;
const total = subtotal + deliveryFee;
const total = subtotal;
const { mutate, isPending } = useMutation({
mutationFn: (body: OrderCreateBody) => cart_api.createOrder(body),
@@ -220,16 +213,12 @@ const RefreshOrder = () => {
}
const items = initialValues.items.map((item) => ({
product_id: item.product.id,
product_id: Number(item.product.id),
quantity: item.quantity,
}));
mutate({
comment: value.comment,
contact_number: onlyNumber(value.phone),
delivery_type: deliveryMethod,
name: value.username,
payment_type: paymentMethod,
items: items,
long: Number(value.long),
lat: Number(value.lat),
@@ -283,50 +272,7 @@ const RefreshOrder = () => {
{t("Shaxsiy ma'lumotlar")}
</h2>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem className="flex justify-start flex-col">
<Label className="block text-sm font-medium text-gray-700">
{t('Ism Familiya')}
</Label>
<FormControl>
<Input
{...field}
className="w-full border h-12 border-gray-300 rounded-lg px-4 py-3 focus:outline-none focus:border-blue-500"
placeholder={t('Ism Familiyangiz')}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="phone"
render={({ field }) => (
<FormItem>
<Label className="block text-sm font-medium text-gray-700">
{t('Telefon raqam')}
</Label>
<FormControl>
<Input
{...field}
value={formatPhone(field.value ?? '')}
onChange={(e) => field.onChange(e.target.value)}
type="tel"
className="w-full h-12 border border-gray-300 rounded-lg px-4 py-3 focus:outline-none focus:border-blue-500"
placeholder="+998 90 123 45 67"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<FormField
control={form.control}
name="comment"
@@ -348,7 +294,6 @@ const RefreshOrder = () => {
/>
</div>
{/* Delivery Address */}
<div className="bg-white rounded-lg shadow-md p-6">
<div className="flex items-center gap-2 mb-4">
<MapPin className="w-5 h-5 text-blue-600" />
@@ -426,122 +371,101 @@ const RefreshOrder = () => {
</div>
</div>
{/* Yetkazib berish vaqti - Yangilangan versiya */}
<div className="bg-white rounded-lg shadow-md p-6">
<div className="flex items-center gap-2 mb-4">
<Truck className="w-5 h-5 text-blue-600" />
<Clock className="w-5 h-5 text-blue-600" />
<h2 className="text-xl font-semibold">
{t('Yetkazib berish usuli')}
{t('Yetkazib berish vaqti')}
</h2>
</div>
<div className="space-y-3">
<label className="flex items-center p-4 border-2 rounded-lg cursor-pointer transition hover:bg-gray-50">
<Input
type="radio"
name="delivery"
value="standard"
checked={deliveryMethod === 'DELIVERY_COURIES'}
onChange={() => setDeliveryMethod('DELIVERY_COURIES')}
className="w-4 h-4 text-blue-600"
/>
<div className="ml-4 flex-1">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<Clock className="w-5 h-5 text-gray-600" />
<span className="font-semibold">
{t('Standart yetkazib berish')}
</span>
</div>
<span className="font-bold text-blue-600">
{subtotal && subtotal > 50000
? 'Bepul'
: "15,000 so'm"}
</span>
</div>
<p className="text-sm text-gray-500 mt-1">
{t('2-3 kun ichida')}
</p>
</div>
</label>
<label className="flex items-center p-4 border-2 rounded-lg cursor-pointer transition hover:bg-gray-50">
<Input
type="radio"
name="delivery"
value="express"
checked={deliveryMethod === 'YandexGo'}
onChange={() => setDeliveryMethod('YandexGo')}
className="w-4 h-4 text-blue-600"
/>
<div className="ml-4 flex-1">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<Package className="w-5 h-5 text-gray-600" />
<span className="font-semibold">
{t('Tez yetkazib berish')}
</span>
</div>
<span className="font-bold text-blue-600">
{"25,000 so'm"}
</span>
</div>
<p className="text-sm text-gray-500 mt-1">
{t('1 kun ichida')}
</p>
</div>
</label>
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{/* Sana tanlash */}
<div className="space-y-2">
<Label className="block text-sm font-medium text-gray-700">
{t('Yetkazib berish sanasi')}
</Label>
<Popover>
<PopoverTrigger asChild>
<Button
type="button"
variant="outline"
className={cn(
'w-full h-12 justify-start text-left font-normal',
!deliveryDate && 'text-muted-foreground',
)}
>
<CalIcon className="mr-2 h-4 w-4" />
{deliveryDate ? (
formatDate.format(deliveryDate, 'DD-MM-YYYY')
) : (
<span>{t('Sanani tanlang')}</span>
)}
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
<Calendar
mode="single"
selected={deliveryDate}
captionLayout="dropdown"
toYear={new Date().getFullYear() + 10}
onSelect={setDeliveryDate}
disabled={(date) =>
date < new Date() ||
date < new Date(new Date().setHours(0, 0, 0, 0))
}
initialFocus
/>
</PopoverContent>
</Popover>
</div>
<div className="bg-white rounded-lg shadow-md p-6">
<div className="flex items-center gap-2 mb-4">
<CreditCard className="w-5 h-5 text-blue-600" />
<h2 className="text-xl font-semibold">
{t("To'lov usuli")}
</h2>
{/* Vaqt tanlash */}
<div className="space-y-2">
<Label className="block text-sm font-medium text-gray-700">
{t("Vaqt oralig'i")}
</Label>
<Select
value={selectedTimeSlot}
onValueChange={setSelectedTimeSlot}
>
<SelectTrigger className="w-full !h-12">
<SelectValue placeholder={t('Vaqtni tanlang')} />
</SelectTrigger>
<SelectContent>
{deliveryTimeSlots.map((slot) => (
<SelectItem key={slot.id} value={slot.label}>
<div className="flex items-center gap-2">
<Clock className="w-4 h-4 text-gray-500" />
{slot.label}
</div>
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
<div className="space-y-3">
<Label className="flex items-center p-4 border-2 rounded-lg cursor-pointer transition hover:bg-gray-50">
<Input
type="radio"
name="payment"
value="cash"
checked={paymentMethod === 'CASH'}
onChange={() => setPaymentMethod('CASH')}
className="w-4 h-4 text-blue-600"
/>
<div className="ml-4 flex items-center gap-3">
<Wallet className="w-6 h-6 text-green-600" />
{/* Tanlangan vaqt ko'rinishi */}
{deliveryDate && selectedTimeSlot && (
<div className="mt-4 p-4 bg-blue-50 border border-blue-200 rounded-lg">
<div className="flex items-start gap-3">
<div className="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center flex-shrink-0">
<CalIcon className="w-5 h-5 text-blue-600" />
</div>
<div>
<span className="font-semibold">{t('Naqd pul')}</span>
<p className="text-sm text-gray-500">
{t("Yetkazib berishda to'lash")}
<p className="font-semibold text-gray-800">
{t('Tanlangan yetkazib berish vaqti')}
</p>
<p className="text-sm text-gray-600 mt-1">
{formatDate.format(deliveryDate, 'DD-MM-YYYY')} |{' '}
{selectedTimeSlot}
</p>
</div>
</div>
</Label>
<Label className="flex items-center p-4 border-2 rounded-lg cursor-pointer transition hover:bg-gray-50">
<Input
type="radio"
name="payment"
value="card"
checked={paymentMethod === 'ACCOUNT_NUMBER'}
onChange={() => setPaymentMethod('ACCOUNT_NUMBER')}
className="w-4 h-4 text-blue-600"
/>
<div className="ml-4 flex items-center gap-3">
<CreditCard className="w-6 h-6 text-blue-600" />
<div>
<span className="font-semibold">
{t('Plastik karta')}
</span>
<p className="text-sm text-gray-500">
{t("Online to'lov")}
</p>
</div>
</div>
</Label>
</div>
</div>
)}
</div>
</div>
@@ -586,18 +510,6 @@ const RefreshOrder = () => {
<span>{t('Mahsulotlar')}:</span>
<span>{subtotal && formatPrice(subtotal, true)}</span>
</div>
<div className="flex justify-between text-gray-600">
<span>{t('Yetkazib berish')}:</span>
<span>
{deliveryFee === 0 ? (
<span className="text-green-600 font-semibold">
{t('Bepul')}
</span>
) : (
`${deliveryFee.toLocaleString()} so'm`
)}
</span>
</div>
</div>
<div className="border-t pt-4 mb-6">