import { useState, useEffect, useMemo, useCallback, useRef } from "react"; import { useNavigate } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { useEditor, EditorContent } from "@tiptap/react"; import StarterKit from "@tiptap/starter-kit"; import Link from "@tiptap/extension-link"; import Underline from "@tiptap/extension-underline"; import { Bold, Italic, Strikethrough, Link2, Heading2, Quote, Code, List, ListOrdered, Minus, WrapText, Paperclip, Type, X, FileText, } from "lucide-react"; import { cn } from "@/lib/utils"; import { FormInput } from "@/components/formInput"; import { FormActions } from "@/components/formActions"; import { BentoGrid, BentoCard } from "@/components/bento"; import { useCreateRequiredDocument, useUpdateRequiredDocument } from "../hooks/useRequiredDocuments"; import { createRequiredDocumentFormSchema, DEFAULT_FORM_VALUES, buildPayload, mapInitial } from "../schemas/requiredDocument.schema"; import type { RequiredDocumentFormData } from "../schemas/requiredDocument.schema"; import type { LoanDocument, LoanDocumentTranslation } from "../api/requiredDocumentsApi"; // ─── Types ──────────────────────────────────────────────────────────────────── type Locale = "tk" | "ru" | "en"; interface RequiredDocumentFormProps { mode: "create" | "edit"; initialData?: LoanDocument; } // ─── Lang tabs ──────────────────────────────────────────────────────────────── const LOCALES: Locale[] = ["tk", "ru", "en"]; // ─── Tiptap Toolbar ─────────────────────────────────────────────────────────── interface RichTextToolbarProps { editor: ReturnType; onFileAttach?: (file: File) => void; } // ─── RichTextToolbar ───────────────────────────────────────────────────────── function RichTextToolbar({ editor, onFileAttach }: RichTextToolbarProps) { const [showLinkModal, setShowLinkModal] = useState(false); const fileInputRef = useRef(null); if (!editor) return null; const handleLinkConfirm = (url: string) => { if (url) { const href = url.startsWith("http") ? url : `https://${url}`; editor.chain().focus().setLink({ href }).run(); } setShowLinkModal(false); }; const handleAttachmentClick = () => fileInputRef.current?.click(); const handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; onFileAttach?.(file); editor.chain().focus().insertContent(`📎 ${file.name} `).run(); e.target.value = ""; }; const tools = [ { icon: Bold, action: () => editor.chain().focus().toggleBold().run(), active: editor.isActive("bold"), title: "Bold", }, { icon: Italic, action: () => editor.chain().focus().toggleItalic().run(), active: editor.isActive("italic"), title: "Italic", }, { icon: Strikethrough, action: () => editor.chain().focus().toggleStrike().run(), active: editor.isActive("strike"), title: "Strike", }, { icon: Link2, action: () => setShowLinkModal(true), active: editor.isActive("link"), title: "Link", }, { icon: Heading2, action: () => editor.chain().focus().toggleHeading({ level: 2 }).run(), active: editor.isActive("heading", { level: 2 }), title: "H2", }, { icon: Quote, action: () => editor.chain().focus().toggleBlockquote().run(), active: editor.isActive("blockquote"), title: "Quote", }, { icon: Code, action: () => editor.chain().focus().toggleCodeBlock().run(), active: editor.isActive("codeBlock"), title: "Code Block", }, { icon: List, action: () => editor.chain().focus().toggleBulletList().run(), active: editor.isActive("bulletList"), title: "Bullet List", }, { icon: ListOrdered, action: () => editor.chain().focus().toggleOrderedList().run(), active: editor.isActive("orderedList"), title: "Ordered List", }, { icon: Minus, action: () => editor.chain().focus().setHorizontalRule().run(), active: false, title: "Divider", }, { icon: WrapText, action: () => editor.chain().focus().setHardBreak().run(), active: false, title: "Hard Break", }, { icon: Paperclip, action: handleAttachmentClick, active: false, title: "Attachment", }, { icon: Type, action: () => editor.chain().focus().selectAll().clearNodes().unsetAllMarks().run(), active: false, title: "Clear format", }, ]; return ( <>
{ // eslint-disable-next-line react-hooks/refs tools.map(({ icon: Icon, action, active, title }) => ( )) }
{/* Link modal */} {showLinkModal && setShowLinkModal(false)} />} ); } // ─── Link Modal ─────────────────────────────────────────────────────────────── function LinkModal({ onConfirm, onClose }: { onConfirm: (url: string) => void; onClose: () => void }) { const [url, setUrl] = useState(""); return (
{ if (e.target === e.currentTarget) onClose(); }} >

URL giriziň

setUrl(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") onConfirm(url); if (e.key === "Escape") onClose(); }} placeholder="https://..." className="border border-input rounded px-2 py-1.5 text-sm w-full focus:outline-none focus:ring-1 focus:ring-ring" />
); } // ─── RichTextEditor ─────────────────────────────────────────────────────────── interface RichTextEditorProps { value: string; onChange: (html: string) => void; error?: string; onFileAttach?: (file: File) => void; } function RichTextEditor({ value, onChange, error, onFileAttach }: RichTextEditorProps) { const editor = useEditor({ extensions: [ StarterKit.configure({ heading: { levels: [1, 2, 3] }, code: {}, blockquote: {}, bulletList: {}, orderedList: {}, horizontalRule: {}, hardBreak: {}, }), Underline, Link.configure({ openOnClick: false, autolink: true, HTMLAttributes: { rel: "noopener noreferrer", target: "_blank", }, }), ], content: value, onUpdate: ({ editor }) => onChange(editor.getHTML()), }); // Sync external value changes (e.g. tab switch) useEffect(() => { if (editor && editor.getHTML() !== value) { editor.commands.setContent(value, { emitUpdate: false }); } }, [value, editor]); return (
{error && (

{error}

)}
); } // ─── AttachedFilesList ──────────────────────────────────────────────────────── interface AttachedFilesListProps { files: File[]; onRemove: (index: number) => void; } function AttachedFilesList({ files, onRemove }: AttachedFilesListProps) { if (files.length === 0) return null; return (
{files.map((file, i) => (
{file.name} {(file.size / 1024).toFixed(1)} KB
))}
); } // ─── LangTabs ──────────────────────────────────────────────────────────────── interface LangTabsProps { active: Locale; onChange: (locale: Locale) => void; } function LangTabs({ active, onChange }: LangTabsProps) { const { t } = useTranslation(); return (
{LOCALES.map((code) => ( ))}
); } // ─── Error helpers ────────────────────────────────────────────────────────── type LegacyErrorKey = `name_${Locale}` | `desc_${Locale}`; const RHF_TO_LEGACY_KEY: Record = { nameTk: "name_tk", nameRu: "name_ru", nameEn: "name_en", descTk: "desc_tk", descRu: "desc_ru", descEn: "desc_en", }; function flattenErrors(errors: Record): Partial> { const result: Partial> = {}; for (const [key, err] of Object.entries(errors)) { const legacyKey = RHF_TO_LEGACY_KEY[key]; if (legacyKey && err?.message) { result[legacyKey] = err.message; } } return result; } // ─── RequiredDocumentForm ───────────────────────────────────────────────────── export function RequiredDocumentForm({ mode, initialData }: RequiredDocumentFormProps) { const { t: _t, i18n } = useTranslation(); const t: (key: string, fallback?: string) => string = useCallback( (key, fallback) => _t(key, fallback ?? key) as string, [_t, i18n.language], ); const navigate = useNavigate(); // Per-field locale tabs (UI only, not form state) const [nameLang, setNameLang] = useState("tk"); const [descLang, setDescLang] = useState("tk"); // ── Attached files — stored as File[] for FormData submission ────────────── const [attachedFiles, setAttachedFiles] = useState([]); const handleFileAttach = useCallback((file: File) => { setAttachedFiles((prev) => [...prev, file]); }, []); const handleFileRemove = useCallback((index: number) => { setAttachedFiles((prev) => prev.filter((_, i) => i !== index)); }, []); const createMutation = useCreateRequiredDocument(); const updateMutation = useUpdateRequiredDocument(initialData?.id ?? 0); const isPending = createMutation.isPending || updateMutation.isPending; const schema = useMemo(() => createRequiredDocumentFormSchema(t), [t, i18n.language]); const { watch, setValue, getValues, formState: { errors: rhfErrors }, trigger, } = useForm({ resolver: zodResolver(schema), defaultValues: initialData ? { ...DEFAULT_FORM_VALUES, ...mapInitial(initialData) } : DEFAULT_FORM_VALUES, }); const form = watch(); const errors = useMemo(() => flattenErrors(rhfErrors as Record), [rhfErrors]); useEffect(() => { if (Object.keys(rhfErrors).length > 0) trigger(); }, [i18n.language]); const name: LoanDocumentTranslation = useMemo( () => ({ tk: form.nameTk, ru: form.nameRu, en: form.nameEn }), [form.nameTk, form.nameRu, form.nameEn], ); const description: LoanDocumentTranslation = useMemo( () => ({ tk: form.descTk, ru: form.descRu, en: form.descEn }), [form.descTk, form.descRu, form.descEn], ); const setName = useCallback( (updater: (prev: LoanDocumentTranslation) => LoanDocumentTranslation) => { const current = getValues(); const prev: LoanDocumentTranslation = { tk: current.nameTk, ru: current.nameRu, en: current.nameEn, }; const next = updater(prev); setValue("nameTk", next.tk); setValue("nameRu", next.ru); setValue("nameEn", next.en); }, [setValue, getValues], ); const setDescription = useCallback( (updater: (prev: LoanDocumentTranslation) => LoanDocumentTranslation) => { const current = getValues(); const prev: LoanDocumentTranslation = { tk: current.descTk, ru: current.descRu, en: current.descEn, }; const next = updater(prev); setValue("descTk", next.tk); setValue("descRu", next.ru); setValue("descEn", next.en); }, [setValue, getValues], ); // ── Submit ────────────────────────────────────────────────────────────────── const handleSubmit = async (e?: React.FormEvent) => { e?.preventDefault(); const isValid = await trigger(); if (!isValid) return; const payload = buildPayload(getValues()); // Build FormData — JSON fields + all attached File objects const formData = new FormData(); formData.append("name[tk]", payload.name.tk); formData.append("name[ru]", payload.name.ru); formData.append("name[en]", payload.name.en); formData.append("description[tk]", payload.description.tk); formData.append("description[ru]", payload.description.ru); formData.append("description[en]", payload.description.en); attachedFiles.forEach((file) => { formData.append("files", file, file.name); }); if (mode === "create") { await createMutation.mutateAsync(payload); navigate("/settings/loan/loan-documents"); } else { await updateMutation.mutateAsync(payload); navigate("/settings/loan/loan-documents"); } }; const submitLabel = mode === "create" ? t("loanDocuments.form.create", "Karz gerekli resminamalary döretiň") : t("loanDocuments.form.update", "Karz gerekli resminamalary täzele"); return (

{mode === "create" ? t("loanDocuments.createTitle", "Karz gerekli resminama dörediň") : t("loanDocuments.editTitle", "Karz gerekli resminama üýtgetmek")}

{t("loanDocuments.fields.name", "Ady")} *

setName((prev) => ({ ...prev, [nameLang]: val }))} placeholder={t("loanDocuments.fields.namePlaceholder", "Ady")} error={errors[`name_${nameLang}`]} /> {LOCALES.filter((l) => l !== nameLang && errors[`name_${l}`]).map((code) => (

{t("languages." + code, code === "tk" ? "Türkmen" : code === "ru" ? "Русский" : "English")}: {errors[`name_${code}`]}

))}

{t("loanDocuments.fields.description", "Yazgy")} *

setDescription((prev) => ({ ...prev, [descLang]: val }))} error={errors[`desc_${descLang}`]} onFileAttach={handleFileAttach} /> {LOCALES.filter((l) => l !== descLang && errors[`desc_${l}`]).map((code) => (

{t("languages." + code, code === "tk" ? "Türkmen" : code === "ru" ? "Русский" : "English")}: {errors[`desc_${code}`]}

))}
navigate("/settings/loan/loan-documents")} cancelVariant="ghost" loadingLabel={t("common.saving", "Saklanylýar...")} submitLabel={submitLabel} className="mt-4" />
); }