"use client"; import React from "react"; import { useState } from "react"; import { Mail, Phone, MapPin, Instagram, Send } from "lucide-react"; import { useLocale, useTranslations } from "next-intl"; import Image from "next/image"; import Link from "next/link"; import { useMutation } from "@tanstack/react-query"; import { toast } from "react-toastify"; import httpClient from "@/request/api"; import { endPoints } from "@/request/links"; export function Footer() { const locale = useLocale(); const [errors, setErrors] = useState({}); const t = useTranslations(); const [email, setEmail] = useState(""); const [subscribed, setSubscribed] = useState(false); const formRequest = useMutation({ mutationKey: [], mutationFn: (data: any) => httpClient.post(endPoints.post.sendNumber, data), onSuccess: () => { toast.success(t("succes")); setSubscribed(true); setEmail(""); setTimeout(() => setSubscribed(false), 3000); }, onError: (error) => { console.log("error: ", error); toast.error(t("error")); }, }); const formatPhoneNumber = (value: string) => { const numbers = value.replace(/\D/g, ""); if (!numbers.startsWith("998")) { return "+998 "; } let formatted = "+998 "; const rest = numbers.slice(3); if (rest.length > 0) formatted += rest.slice(0, 2); if (rest.length > 2) formatted += " " + rest.slice(2, 5); if (rest.length > 5) formatted += " " + rest.slice(5, 7); if (rest.length > 7) formatted += " " + rest.slice(7, 9); return formatted; }; const handlePhoneChange = (e: React.ChangeEvent) => { const formatted = formatPhoneNumber(e.target.value); setEmail(formatted); if (errors.address) { setErrors({ ...errors, address: "" }); } }; const handleSubscribe = (e: React.FormEvent) => { e.preventDefault(); if (email) { // Telefon raqamni tozalash (faqat raqamlar) const cleanPhone = email.replace(/\D/g, ""); console.log("without 998: ",cleanPhone.slice(3)) formRequest.mutate({ number: Number(cleanPhone.slice(3))}); } }; return ( ); }