"use client"; import { createSeo, deleteSeo, getAllSeo, getDetailSeo, updateSeo, } from "@/pages/seo/lib/api"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { AlertCircle, CheckCircle, FileText, Image as ImageIcon, Loader2, Pencil, Trash2, TrendingUp, } from "lucide-react"; import { useEffect, useState, type ChangeEvent } from "react"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; type SeoData = { title: string; description: string; keywords: string; ogTitle: string; ogDescription: string; ogImage: File | null | string; }; export default function Seo() { const [formData, setFormData] = useState({ title: "", description: "", keywords: "", ogTitle: "", ogDescription: "", ogImage: null, }); const { t } = useTranslation(); const [edit, setEdit] = useState(null); const queryClient = useQueryClient(); const { mutate, isPending } = useMutation({ mutationFn: async (body: FormData) => createSeo(body), onSuccess: () => { toast.success(t("Ma’lumotlar muvaffaqiyatli saqlandi"), { position: "top-center", }); setFormData({ description: "", keywords: "", ogDescription: "", ogImage: null, ogTitle: "", title: "", }); queryClient.refetchQueries({ queryKey: ["seo_all"] }); queryClient.refetchQueries({ queryKey: ["seo_detail"] }); setImagePreview(null); }, onError: () => { toast.error(t("Xatolik yuz berdi"), { position: "top-center", richColors: true, }); }, }); const { mutate: editSeo, isPending: editPending } = useMutation({ mutationFn: async ({ body, id }: { body: FormData; id: number }) => updateSeo({ body, id }), onSuccess: () => { toast.success(t("Ma’lumotlar muvaffaqiyatli saqlandi"), { position: "top-center", }); setEdit(null); setFormData({ description: "", keywords: "", ogDescription: "", ogImage: null, ogTitle: "", title: "", }); queryClient.refetchQueries({ queryKey: ["seo_all"] }); queryClient.refetchQueries({ queryKey: ["seo_detail"] }); setImagePreview(null); }, onError: () => { toast.error(t("Xatolik yuz berdi"), { position: "top-center", richColors: true, }); }, }); const { mutate: deleteSeoo } = useMutation({ mutationFn: (id: number) => deleteSeo({ id }), onSuccess: () => { setEdit(null); setFormData({ description: "", keywords: "", ogDescription: "", ogImage: null, ogTitle: "", title: "", }); queryClient.refetchQueries({ queryKey: ["seo_all"] }); queryClient.refetchQueries({ queryKey: ["seo_detail"] }); setImagePreview(null); }, onError: () => { toast.error(t("Xatolik yuz berdi"), { position: "top-center", richColors: true, }); }, }); const { data: allSeo, isLoading } = useQuery({ queryKey: ["seo_all"], queryFn: () => getAllSeo({ page: 1, page_size: 99 }), select(data) { return data.data.data.results; }, }); const { data: detailSeo } = useQuery({ queryKey: ["seo_detail", edit], queryFn: () => getDetailSeo(edit!), select(data) { return data.data.data; }, enabled: !!edit, }); const [imagePreview, setImagePreview] = useState(null); const handleChange = ( e: ChangeEvent, ) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value, })); }; const handleImageUpload = (e: ChangeEvent) => { const file = e.target.files?.[0]; if (file) { setFormData((prev) => ({ ...prev, ogImage: file })); setImagePreview(URL.createObjectURL(file)); } }; const handleEdit = (id: number) => { setEdit(id); }; const handleDelete = (id: number) => { deleteSeoo(id); }; useEffect(() => { if (detailSeo) { setFormData({ description: detailSeo.description, keywords: detailSeo.keywords, ogDescription: detailSeo.og_description, ogImage: detailSeo.og_image, ogTitle: detailSeo.og_title, title: detailSeo.title, }); setImagePreview(detailSeo.og_image || null); } }, [detailSeo, edit]); const handleSave = () => { const form = new FormData(); form.append("title", formData.title); form.append("description", formData.description); form.append("keywords", formData.keywords); form.append("og_title", formData.ogTitle); form.append("og_description", formData.ogDescription); // faqat File bo‘lsa qo‘shamiz if (formData.ogImage instanceof File) { form.append("og_image", formData.ogImage); } if (edit) { editSeo({ body: form, id: edit }); } else { mutate(form); } }; const getTitleLength = () => formData.title?.length ?? 0; const getDescriptionLength = () => formData.description?.length ?? 0; const isValidTitle = getTitleLength() > 10 && getTitleLength() <= 60; const isValidDescription = getDescriptionLength() > 60 && getDescriptionLength() <= 160; return (
{/* Header */}

{t("SEO Manager")}

{t("Saytingizni qidiruv tizimida yaxshi pozitsiyaga keltiring")}

{/* Main Form */}
{/* Title */}
{isValidTitle && ( )} {getTitleLength() > 0 && !isValidTitle && ( )}
{/* Description */}