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,10 @@
import { BASE_URL } from '@/shared/config/api/URLs';
import { useCartId } from '@/shared/hooks/cartId';
import formatPhone from '@/shared/lib/formatPhone';
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 +15,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,17 +34,15 @@ import {
} from '@pbe/react-yandex-maps';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { AxiosError } from 'axios';
import { format } from 'date-fns';
import { uz } from 'date-fns/locale';
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';
@@ -53,16 +59,24 @@ interface CoordsData {
polygon: [number, number][][];
}
// Yetkazib berish vaqt oraliqlar
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' },
];
const OrderPage = () => {
const t = useTranslations();
const form = useForm<z.infer<typeof orderForm>>({
resolver: zodResolver(orderForm),
defaultValues: {
username: '',
comment: '',
lat: '41.311081',
long: '69.240562',
phone: '+998',
},
});
const [cart, setCart] = useState<number | string | null>(null);
@@ -103,26 +117,15 @@ const OrderPage = () => {
select: (data) => data.data.cart_item,
});
const [paymentMethod, setPaymentMethod] = useState<'CASH' | 'ACCOUNT_NUMBER'>(
'CASH',
);
const [deliveryMethod, setDeliveryMethod] = useState<
'YandexGo' | 'DELIVERY_COURIES' | 'PICKUP'
>('DELIVERY_COURIES');
// Yetkazib berish vaqti uchun state
const [deliveryDate, setDeliveryDate] = useState<Date>();
const [selectedTimeSlot, setSelectedTimeSlot] = useState<string>('');
const subtotal = cartItems?.reduce(
(sum, item) => sum + item.product_price * item.quantity,
0,
);
const deliveryFee =
deliveryMethod === 'DELIVERY_COURIES'
? 25000
: subtotal && subtotal > 50000
? 0
: 15000;
const total = subtotal;
const [coords, setCoords] = useState({
latitude: 41.311081,
longitude: 69.240562,
@@ -175,7 +178,7 @@ const OrderPage = () => {
const handleShowMyLocation = () => {
if (!navigator.geolocation) {
alert('Sizning brauzeringiz geolokatsiyani qollab-quvvatlamaydi');
alert("Sizning brauzeringiz geolokatsiyani qo'llab-quvvatlamaydi");
return;
}
navigator.geolocation.getCurrentPosition(
@@ -212,14 +215,31 @@ const OrderPage = () => {
form.setValue('lat', result.lat.toString(), { shouldDirty: true });
form.setValue('long', result.lon.toString(), { shouldDirty: true });
}, 700); // debounce
}, 700);
return () => clearTimeout(timeout);
}, [cityValue]);
function onSubmit(value: z.infer<typeof orderForm>) {
if (!cartItems || cartItems.length === 0) {
toast.error('Savatcha bosh', {
toast.error("Savatcha bo'sh", {
richColors: true,
position: 'top-center',
});
return;
}
// Yetkazib berish vaqtini tekshirish
if (!deliveryDate) {
toast.error('Yetkazib berish sanasini tanlang', {
richColors: true,
position: 'top-center',
});
return;
}
if (!selectedTimeSlot) {
toast.error('Yetkazib berish vaqtini tanlang', {
richColors: true,
position: 'top-center',
});
@@ -227,19 +247,18 @@ const OrderPage = () => {
}
const items = cartItems.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),
// delivery_date: format(deliveryDate, 'yyyy-MM-dd'),
// delivery_time_start: timeSlot?.start,
// delivery_time_end: timeSlot?.end,
});
}
@@ -269,8 +288,7 @@ const OrderPage = () => {
return (
<div className="custom-container mb-5">
<>
{/* Header */}
<div>
<div className="mb-6">
<h1 className="text-3xl font-bold text-gray-800 mb-2">
{t('Buyurtmani rasmiylashtirish')}
@@ -278,62 +296,11 @@ const OrderPage = () => {
<p className="text-gray-600">{t("Ma'lumotlaringizni to'ldiring")}</p>
</div>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<div onSubmit={form.handleSubmit(onSubmit)} className="contents">
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
{/* Left Column - Forms */}
<div className="lg:col-span-2 space-y-6">
{/* Contact Information */}
<div className="bg-white rounded-lg shadow-md p-6">
<div className="flex items-center gap-2 mb-4">
<User className="w-5 h-5 text-blue-600" />
<h2 className="text-xl font-semibold">
{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('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"
@@ -433,122 +400,105 @@ const OrderPage = () => {
</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 ? (
format(deliveryDate, 'dd MMMM yyyy', {
locale: uz,
})
) : (
<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">
{format(deliveryDate, 'dd MMMM yyyy', {
locale: uz,
})}{' '}
| {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>
@@ -593,18 +543,6 @@ const OrderPage = () => {
<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">
@@ -613,7 +551,7 @@ const OrderPage = () => {
{t('Jami')}:
</span>
<span className="text-2xl font-bold text-blue-600">
{total && formatPrice(total, true)}
{subtotal && formatPrice(subtotal, true)}
</span>
</div>
</div>
@@ -621,6 +559,7 @@ const OrderPage = () => {
<button
type="submit"
disabled={isPending}
onClick={form.handleSubmit(onSubmit)}
className="w-full bg-blue-600 text-white py-4 rounded-lg font-semibold hover:bg-blue-700 transition disabled:bg-gray-400 disabled:cursor-not-allowed"
>
{isPending ? (
@@ -634,9 +573,9 @@ const OrderPage = () => {
</div>
</div>
</div>
</form>
</div>
</Form>
</>
</div>
</div>
);
};