Files
simple-admin/src/pages/finance/ui/PayDialog.tsx
Samandar Turgunboyev 193d01ed51 bug fix
2025-11-01 19:12:38 +05:00

141 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { payAgency } from "@/pages/finance/lib/api";
import { Button } from "@/shared/ui/button";
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/shared/ui/dialog";
import { Input } from "@/shared/ui/input";
import { Label } from "@/shared/ui/label";
import { useMutation } from "@tanstack/react-query";
import { Loader2 } from "lucide-react";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
interface PayDialogProps {
open: boolean;
onClose: () => void;
agencyId: number;
}
interface PayFormValues {
amount: string; // formatted value (e.g. "1 200 000")
note: string;
}
// Narxni formatlovchi funksiya
function formatPrice(value: number | string): string {
const num = Number(value.toString().replace(/\D/g, ""));
if (isNaN(num)) return "";
return num.toLocaleString("ru-RU");
}
export function PayDialog({ open, onClose, agencyId }: PayDialogProps) {
const { t } = useTranslation();
const form = useForm<PayFormValues>({
defaultValues: {
amount: "",
note: "",
},
});
const { mutate, isPending } = useMutation({
mutationFn: ({
body,
}: {
body: {
travel_agency: number;
amount: number;
note: string;
};
}) => payAgency({ body }),
onSuccess: () => {
onClose();
},
onError: () => {
toast.error(t("Xatolik yuz berdi"), {
richColors: true,
position: "top-center",
});
},
});
const handleAmountChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const raw = e.target.value.replace(/\D/g, "");
const formatted = formatPrice(raw);
form.setValue("amount", formatted);
};
const handleSubmit = async (values: PayFormValues) => {
const cleanAmount = Number(
values.amount.replace(/\s/g, "").replace(/,/g, ""),
);
mutate({
body: {
amount: cleanAmount,
note: values.note,
travel_agency: agencyId,
},
});
};
return (
<Dialog open={open} onOpenChange={onClose}>
<DialogContent className="sm:max-w-md text-white bg-gray-900">
<DialogHeader>
<DialogTitle>{t("Qolgan summani tolash")}</DialogTitle>
</DialogHeader>
<form
onSubmit={form.handleSubmit(handleSubmit)}
className="space-y-4 py-2"
>
{/* Summani kiritish */}
<div className="space-y-2">
<Label htmlFor="amount">{t("Summani kiriting")}</Label>
<Input
id="amount"
inputMode="numeric"
placeholder="1 200 000"
value={form.watch("amount")}
onChange={handleAmountChange}
/>
{form.formState.errors.amount && (
<p className="text-red-400 text-sm">
{t("Summani togri kiriting")}
</p>
)}
</div>
{/* Izoh */}
<div className="space-y-2">
<Label htmlFor="note">{t("Izoh")}</Label>
<Input
id="note"
type="text"
placeholder={t("Izoh kiriting") || ""}
{...form.register("note")}
/>
</div>
{/* Tugmalar */}
<DialogFooter className="flex justify-end gap-3 pt-4">
<Button type="button" variant="outline" onClick={onClose}>
{t("Bekor qilish")}
</Button>
<Button type="submit" disabled={isPending}>
{isPending ? <Loader2 className="animate-spin" /> : t("Tolash")}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}