connected api with profile, order
This commit is contained in:
@@ -3,18 +3,24 @@
|
||||
import { useState, useEffect } from "react"
|
||||
import Link from "next/link"
|
||||
import Image from "next/image"
|
||||
import { X, Menu, Search, Store } from "lucide-react"
|
||||
import { X, Menu, Search, Store, LogOut, User as UserIcon } from "lucide-react"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu"
|
||||
import Logo from "@/public/logo.png"
|
||||
import CategoryMenu from "./ui/CategoryMenu"
|
||||
import SearchBar from "./ui/SearchBar"
|
||||
import AuthDialog from "./ui/AuthDialog"
|
||||
import ActionButtons from "./ui/ActionButtons"
|
||||
import LanguageSelector from "./ui/LanguageSelector"
|
||||
import { useAuthStatus, useLogout } from "@/lib/hooks/useAuth"
|
||||
|
||||
interface HeaderProps {
|
||||
locale?: string
|
||||
isAuthenticated?: boolean
|
||||
translations?: {
|
||||
catalog: string
|
||||
search: string
|
||||
@@ -27,8 +33,17 @@ interface HeaderProps {
|
||||
phone: string
|
||||
code: string
|
||||
send: string
|
||||
verify: string
|
||||
sending: string
|
||||
verifying: string
|
||||
enterPhone: string
|
||||
weWillSendCode: string
|
||||
invalidPhone: string
|
||||
invalidCode: string
|
||||
loginSuccess: string
|
||||
codeSent: string
|
||||
logout: string
|
||||
loggingOut: string
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,17 +59,29 @@ const DEFAULT_TRANSLATIONS = {
|
||||
phone: "Номер телефона",
|
||||
code: "Код",
|
||||
send: "Отправить",
|
||||
verify: "Подтвердить",
|
||||
sending: "Отправка...",
|
||||
verifying: "Проверка...",
|
||||
enterPhone: "Введите свой номер телефона",
|
||||
weWillSendCode: "Мы вышлем вам код",
|
||||
invalidPhone: "Неверный номер телефона",
|
||||
invalidCode: "Неверный код",
|
||||
loginSuccess: "Вход выполнен успешно",
|
||||
codeSent: "Код отправлен на ваш номер",
|
||||
logout: "Выйти",
|
||||
loggingOut: "Выход...",
|
||||
}
|
||||
|
||||
export default function Header({ locale = "ru", isAuthenticated = false, translations }: HeaderProps) {
|
||||
export default function Header({ locale = "ru", translations }: HeaderProps) {
|
||||
const [isClient, setIsClient] = useState(false)
|
||||
const [isCategoryOpen, setIsCategoryOpen] = useState(false)
|
||||
const [isMobileSearchOpen, setIsMobileSearchOpen] = useState(false)
|
||||
const [isLoginOpen, setIsLoginOpen] = useState(false)
|
||||
|
||||
const t = translations || DEFAULT_TRANSLATIONS
|
||||
const t = { ...DEFAULT_TRANSLATIONS, ...translations }
|
||||
|
||||
const { isAuthenticated, isLoading } = useAuthStatus()
|
||||
const { mutate: logout, isPending: isLoggingOut } = useLogout()
|
||||
|
||||
useEffect(() => {
|
||||
setIsClient(true)
|
||||
@@ -68,6 +95,10 @@ export default function Header({ locale = "ru", isAuthenticated = false, transla
|
||||
}
|
||||
}
|
||||
|
||||
const handleLogout = () => {
|
||||
logout()
|
||||
}
|
||||
|
||||
const toggleCategoryMenu = () => setIsCategoryOpen(!isCategoryOpen)
|
||||
const closeCategoryMenu = () => setIsCategoryOpen(false)
|
||||
|
||||
@@ -80,7 +111,7 @@ export default function Header({ locale = "ru", isAuthenticated = false, transla
|
||||
<div className="flex h-16 items-center justify-between gap-4">
|
||||
<Link href="/" className="shrink-0">
|
||||
<div className="relative h-8 w-[180px]">
|
||||
<Image src={Logo || "/placeholder.svg"} alt="Logo" fill className="object-contain" priority />
|
||||
<Image src={Logo} alt="Logo" fill className="object-contain" priority />
|
||||
</div>
|
||||
</Link>
|
||||
|
||||
@@ -106,6 +137,36 @@ export default function Header({ locale = "ru", isAuthenticated = false, transla
|
||||
|
||||
<SearchBar isMobile={false} searchPlaceholder={t.search} className="hidden flex-1 md:flex" />
|
||||
|
||||
<div className="hidden md:flex items-center gap-2">
|
||||
{isLoading ? (
|
||||
<div className="h-10 w-24 animate-pulse bg-gray-200 rounded" />
|
||||
) : isAuthenticated ? (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="ghost" size="sm" className="flex-col gap-0.5 h-auto px-2 py-2">
|
||||
<UserIcon className="h-5 w-5 text-gray-600" />
|
||||
<span className="text-xs text-gray-700">{t.profile}</span>
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onClick={() => (window.location.href = `/${locale}/me`)}>
|
||||
<UserIcon className="mr-2 h-4 w-4" />
|
||||
{t.profile}
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={handleLogout} disabled={isLoggingOut}>
|
||||
<LogOut className="mr-2 h-4 w-4" />
|
||||
{isLoggingOut ? t.loggingOut : t.logout}
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
) : (
|
||||
<Button variant="ghost" size="sm" className="flex-col gap-0.5 h-auto px-2 py-2" onClick={handleAuthClick}>
|
||||
<UserIcon className="h-5 w-5 text-gray-600" />
|
||||
<span className="text-xs text-gray-700">{t.login}</span>
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<ActionButtons
|
||||
isAuthenticated={isAuthenticated}
|
||||
onAuthClick={handleAuthClick}
|
||||
@@ -146,8 +207,15 @@ export default function Header({ locale = "ru", isAuthenticated = false, transla
|
||||
phone: t.phone,
|
||||
code: t.code,
|
||||
send: t.send,
|
||||
verify: t.verify,
|
||||
sending: t.sending,
|
||||
verifying: t.verifying,
|
||||
invalidPhone: t.invalidPhone,
|
||||
invalidCode: t.invalidCode,
|
||||
loginSuccess: t.loginSuccess,
|
||||
codeSent: t.codeSent,
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,59 +1,112 @@
|
||||
import React, { useState } from "react";
|
||||
import Image from "next/image";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import Logo from "@/public/logo.png";
|
||||
"use client"
|
||||
|
||||
import React, { useState } from "react"
|
||||
import Image from "next/image"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"
|
||||
import { toast } from "sonner"
|
||||
import Logo from "@/public/logo.png"
|
||||
import { useLogin, useVerifyToken } from "@/lib/hooks/useAuth"
|
||||
|
||||
interface AuthDialogProps {
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
isOpen: boolean
|
||||
onClose: () => void
|
||||
translations: {
|
||||
enterPhone: string;
|
||||
weWillSendCode: string;
|
||||
phone: string;
|
||||
code: string;
|
||||
send: string;
|
||||
};
|
||||
enterPhone: string
|
||||
weWillSendCode: string
|
||||
phone: string
|
||||
code: string
|
||||
send: string
|
||||
verify: string
|
||||
sending: string
|
||||
verifying: string
|
||||
invalidPhone: string
|
||||
invalidCode: string
|
||||
loginSuccess: string
|
||||
codeSent: string
|
||||
}
|
||||
}
|
||||
|
||||
export default function AuthDialog({
|
||||
isOpen,
|
||||
onClose,
|
||||
translations: t,
|
||||
}: AuthDialogProps) {
|
||||
const [phone, setPhone] = useState("993");
|
||||
const [otp, setOtp] = useState("");
|
||||
const [otpSent, setOtpSent] = useState(false);
|
||||
export default function AuthDialog({ isOpen, onClose, translations: t }: AuthDialogProps) {
|
||||
const [phone, setPhone] = useState("993")
|
||||
const [otp, setOtp] = useState("")
|
||||
const [otpSent, setOtpSent] = useState(false)
|
||||
const [rawPhone, setRawPhone] = useState("")
|
||||
|
||||
const handleSendOtp = () => {
|
||||
if (phone.length > 3) {
|
||||
setOtpSent(true);
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogin = () => {
|
||||
// Here you can add authentication logic
|
||||
resetDialog();
|
||||
};
|
||||
const { mutate: login, isPending: isLoginLoading } = useLogin()
|
||||
const { mutate: verifyToken, isPending: isVerifyLoading } = useVerifyToken()
|
||||
|
||||
const resetDialog = () => {
|
||||
onClose();
|
||||
setOtpSent(false);
|
||||
setPhone("993");
|
||||
setOtp("");
|
||||
};
|
||||
setOtpSent(false)
|
||||
setPhone("993")
|
||||
setOtp("")
|
||||
setRawPhone("")
|
||||
onClose()
|
||||
}
|
||||
|
||||
const handleSendOtp = () => {
|
||||
const cleanPhone = phone.replace(/\D/g, "")
|
||||
|
||||
if (cleanPhone.length !== 11 || !cleanPhone.startsWith("993")) {
|
||||
toast.error(t.invalidPhone)
|
||||
return
|
||||
}
|
||||
|
||||
const phoneNumber = cleanPhone.substring(3)
|
||||
setRawPhone(phoneNumber)
|
||||
|
||||
login(
|
||||
{ phone_number: phoneNumber },
|
||||
{
|
||||
onSuccess: () => {
|
||||
toast.success(t.codeSent)
|
||||
setOtpSent(true)
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error?.response?.data?.message || "Hata oluştu")
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const handleLogin = () => {
|
||||
if (otp.length < 4) {
|
||||
toast.error(t.invalidCode)
|
||||
return
|
||||
}
|
||||
|
||||
verifyToken(
|
||||
{
|
||||
phone_number: rawPhone,
|
||||
code: otp,
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
toast.success(t.loginSuccess)
|
||||
resetDialog()
|
||||
window.location.reload()
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error?.response?.data?.message || "Kod yanlış")
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const handleKeyPress = (e: React.KeyboardEvent, action: () => void) => {
|
||||
if (e.key === "Enter") {
|
||||
action();
|
||||
action()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const formatPhoneInput = (value: string) => {
|
||||
const cleaned = value.replace(/\D/g, "")
|
||||
if (!cleaned.startsWith("993")) {
|
||||
return "993"
|
||||
}
|
||||
return cleaned.substring(0, 11)
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={isOpen} onOpenChange={resetDialog}>
|
||||
@@ -64,34 +117,36 @@ export default function AuthDialog({
|
||||
<Image src={Logo} alt="Logo" fill className="object-contain" />
|
||||
</div>
|
||||
</div>
|
||||
<DialogTitle className="text-2xl text-center">
|
||||
{t.enterPhone}
|
||||
</DialogTitle>
|
||||
<p className="text-center text-sm text-gray-600">
|
||||
{t.weWillSendCode}
|
||||
</p>
|
||||
<DialogTitle className="text-2xl text-center">{t.enterPhone}</DialogTitle>
|
||||
<p className="text-center text-sm text-gray-600">{t.weWillSendCode}</p>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4 mt-4">
|
||||
<Input
|
||||
type="tel"
|
||||
placeholder={t.phone}
|
||||
value={phone}
|
||||
onChange={(e) => setPhone(e.target.value)}
|
||||
className="h-12 rounded-xl"
|
||||
onKeyDown={(e) => handleKeyPress(e, handleSendOtp)}
|
||||
disabled={otpSent}
|
||||
/>
|
||||
<div>
|
||||
<Input
|
||||
type="tel"
|
||||
placeholder={t.phone}
|
||||
value={phone}
|
||||
onChange={(e) => setPhone(formatPhoneInput(e.target.value))}
|
||||
className="h-12 rounded-xl"
|
||||
onKeyDown={(e) => handleKeyPress(e, handleSendOtp)}
|
||||
disabled={otpSent || isLoginLoading}
|
||||
maxLength={11}
|
||||
/>
|
||||
<p className="text-xs text-gray-500 mt-1">Format: 99365123456</p>
|
||||
</div>
|
||||
|
||||
{otpSent && (
|
||||
<Input
|
||||
type="text"
|
||||
placeholder={t.code}
|
||||
value={otp}
|
||||
onChange={(e) => setOtp(e.target.value)}
|
||||
onChange={(e) => setOtp(e.target.value.replace(/\D/g, "").substring(0, 6))}
|
||||
className="h-12 rounded-xl"
|
||||
onKeyDown={(e) => handleKeyPress(e, handleLogin)}
|
||||
disabled={isVerifyLoading}
|
||||
autoFocus
|
||||
maxLength={6}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -99,11 +154,18 @@ export default function AuthDialog({
|
||||
onClick={otpSent ? handleLogin : handleSendOtp}
|
||||
className="w-full h-12 rounded-xl font-bold text-base"
|
||||
size="lg"
|
||||
disabled={isLoginLoading || isVerifyLoading}
|
||||
>
|
||||
{t.send}
|
||||
{isLoginLoading
|
||||
? t.sending
|
||||
: isVerifyLoading
|
||||
? t.verifying
|
||||
: otpSent
|
||||
? t.verify
|
||||
: t.send}
|
||||
</Button>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user