api ulandi
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
'use client';
|
||||
|
||||
import { cart_api } from '@/features/cart/lib/api';
|
||||
import { Link, useRouter } from '@/shared/config/i18n/navigation';
|
||||
import { useCartId } from '@/shared/hooks/cartId';
|
||||
import formatPhone from '@/shared/lib/formatPhone';
|
||||
import { getToken } from '@/shared/lib/token';
|
||||
import { Badge } from '@/shared/ui/badge';
|
||||
import { Button } from '@/shared/ui/button';
|
||||
import { Input } from '@/shared/ui/input';
|
||||
import { Popover, PopoverContent } from '@/shared/ui/popover';
|
||||
@@ -15,12 +19,12 @@ import {
|
||||
} from '@/shared/ui/sheet';
|
||||
import { categoryList } from '@/widgets/welcome/lib/data';
|
||||
import { PopoverTrigger } from '@radix-ui/react-popover';
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
import {
|
||||
ChevronRight,
|
||||
Facebook,
|
||||
Heart,
|
||||
Instagram,
|
||||
LayoutGrid,
|
||||
Mail,
|
||||
MenuIcon,
|
||||
Phone,
|
||||
@@ -29,8 +33,8 @@ import {
|
||||
ShoppingCart,
|
||||
Twitter,
|
||||
User,
|
||||
XIcon,
|
||||
} from 'lucide-react';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import Image from 'next/image';
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
import { useEffect, useState } from 'react';
|
||||
@@ -44,19 +48,43 @@ const Navbar = () => {
|
||||
const [isSticky, setIsSticky] = useState(false);
|
||||
const [query, setQuery] = useState('');
|
||||
const searchParams = useSearchParams();
|
||||
const [user, setUser] = useState<boolean>(false);
|
||||
const users = localStorage.getItem('user');
|
||||
const token = getToken();
|
||||
const t = useTranslations();
|
||||
const { cart_id } = useCartId();
|
||||
const [cartQuenty, setCartQuenty] = useState<number>(0);
|
||||
const { setCartId } = useCartId();
|
||||
|
||||
const { mutate: cart } = useMutation({
|
||||
mutationFn: () => cart_api.create(),
|
||||
onSuccess: (data) => {
|
||||
setCartId(data.data.cart_id);
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (users && users === 'true') {
|
||||
setUser(true);
|
||||
} else {
|
||||
setUser(false);
|
||||
if (token) {
|
||||
cart();
|
||||
}
|
||||
}, [users]);
|
||||
}, [token]);
|
||||
|
||||
const queryFromUrl = searchParams.get('q') ?? '';
|
||||
|
||||
const { data: cartItems } = useQuery({
|
||||
queryKey: ['cart_items', cart_id],
|
||||
queryFn: () => cart_api.get_cart_items(cart_id!),
|
||||
enabled: !!cart_id,
|
||||
select(data) {
|
||||
return data.data.cart_item;
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (cartItems) {
|
||||
const total = cartItems.reduce((sum, item) => sum + item.quantity, 0);
|
||||
setCartQuenty(total > 9 ? 9 : total);
|
||||
}
|
||||
}, [cartItems]);
|
||||
|
||||
useEffect(() => {
|
||||
setQuery(queryFromUrl);
|
||||
}, [queryFromUrl]);
|
||||
@@ -89,7 +117,7 @@ const Navbar = () => {
|
||||
href="/about"
|
||||
className="flex items-center gap-1.5 font-medium"
|
||||
>
|
||||
Biz haqimizda
|
||||
{t('Biz haqimizda')}
|
||||
</Link>
|
||||
</li>
|
||||
<li className="text-white hover:text-white/80 transition-colors cursor-pointer">
|
||||
@@ -97,7 +125,7 @@ const Navbar = () => {
|
||||
href="/privacy-policy"
|
||||
className="flex items-center gap-1.5 font-medium"
|
||||
>
|
||||
Maxfiylik siyosati
|
||||
{t('Maxfiylik siyosati')}
|
||||
</Link>
|
||||
</li>
|
||||
<li className="text-white hover:text-white/80 transition-colors cursor-pointer">
|
||||
@@ -105,7 +133,7 @@ const Navbar = () => {
|
||||
href="/faq"
|
||||
className="flex items-center gap-1.5 font-medium"
|
||||
>
|
||||
Savol-javob
|
||||
{t('Savol-javob')}
|
||||
</Link>
|
||||
</li>
|
||||
|
||||
@@ -205,7 +233,7 @@ const Navbar = () => {
|
||||
{/* Asosiy Sahifalar */}
|
||||
<div>
|
||||
<h3 className="text-xs font-semibold text-slate-500 uppercase tracking-wider mb-3 px-2">
|
||||
Sahifalar
|
||||
{t('Sahifalar')}
|
||||
</h3>
|
||||
<nav className="space-y-1">
|
||||
<SheetClose asChild>
|
||||
@@ -215,7 +243,7 @@ const Navbar = () => {
|
||||
>
|
||||
<div className="w-1 h-1 rounded-full bg-slate-400 group-hover:bg-[#57A595]" />
|
||||
<span className="text-sm font-medium">
|
||||
Biz haqimizda
|
||||
{t('Biz haqimizda')}
|
||||
</span>
|
||||
<ChevronRight className="size-4 ml-auto opacity-0 group-hover:opacity-100 transition-opacity" />
|
||||
</Link>
|
||||
@@ -227,7 +255,7 @@ const Navbar = () => {
|
||||
>
|
||||
<div className="w-1 h-1 rounded-full bg-slate-400 group-hover:bg-[#57A595]" />
|
||||
<span className="text-sm font-medium">
|
||||
Maxfiylik siyosati
|
||||
{t('Maxfiylik siyosati')}
|
||||
</span>
|
||||
<ChevronRight className="size-4 ml-auto opacity-0 group-hover:opacity-100 transition-opacity" />
|
||||
</Link>
|
||||
@@ -239,7 +267,7 @@ const Navbar = () => {
|
||||
>
|
||||
<div className="w-1 h-1 rounded-full bg-slate-400 group-hover:bg-[#57A595]" />
|
||||
<span className="text-sm font-medium">
|
||||
Savol va javoblar
|
||||
{t('Savol-javob')}
|
||||
</span>
|
||||
<ChevronRight className="size-4 ml-auto opacity-0 group-hover:opacity-100 transition-opacity" />
|
||||
</Link>
|
||||
@@ -253,7 +281,7 @@ const Navbar = () => {
|
||||
{/* Aloqa */}
|
||||
<div>
|
||||
<h3 className="text-xs font-semibold text-slate-500 uppercase tracking-wider mb-3 px-2">
|
||||
{"Biz bilan bog'laning"}
|
||||
{t("Biz bilan bog'laning")}
|
||||
</h3>
|
||||
<nav className="space-y-1">
|
||||
<a
|
||||
@@ -322,7 +350,7 @@ const Navbar = () => {
|
||||
</Sheet>
|
||||
</div>
|
||||
<div className="flex-1 flex gap-3">
|
||||
<Button
|
||||
{/* <Button
|
||||
variant={'outline'}
|
||||
className="h-10 max-lg:hidden cursor-pointer"
|
||||
onClick={() => {
|
||||
@@ -339,11 +367,11 @@ const Navbar = () => {
|
||||
<LayoutGrid className="size-4 text-foreground" />
|
||||
)}
|
||||
<p className="text-foreground">Kataloglar</p>
|
||||
</Button>
|
||||
</Button> */}
|
||||
|
||||
<div className="relative w-full max-lg:hidden">
|
||||
<Input
|
||||
placeholder="Mahsulot nomi"
|
||||
placeholder={t('Mahsulot nomi')}
|
||||
value={query}
|
||||
onFocus={() => setSearchOpen(true)}
|
||||
onBlur={() => setTimeout(() => setSearchOpen(false), 200)}
|
||||
@@ -379,15 +407,19 @@ const Navbar = () => {
|
||||
</Button>
|
||||
<Button
|
||||
variant={'ghost'}
|
||||
id="cart-icon"
|
||||
onClick={() => router.push('/cart')}
|
||||
className="h-10 max-lg:hidden cursor-pointer border border-slate-200"
|
||||
className="h-10 relative max-lg:hidden cursor-pointer border border-slate-200"
|
||||
>
|
||||
<ShoppingCart className="size-4 text-foreground" />
|
||||
<Badge className="absolute -top-2 -right-2 line-clamp-1 w-6 flex justify-center items-center">
|
||||
{cartQuenty === 9 ? cartQuenty + '+' : cartQuenty}
|
||||
</Badge>
|
||||
</Button>
|
||||
<Button
|
||||
variant={'ghost'}
|
||||
onClick={() => {
|
||||
if (user) {
|
||||
if (token) {
|
||||
router.push('/profile');
|
||||
} else {
|
||||
router.push('/auth');
|
||||
|
||||
Reference in New Issue
Block a user