connected with loan order api

This commit is contained in:
Jelaletdin12
2026-08-19 22:09:39 +05:00
parent 7bb37f9b43
commit 73142c1fcf
19 changed files with 1234 additions and 476 deletions

View File

@@ -0,0 +1,213 @@
import { useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { Upload, X, Loader2, FileText, ExternalLink } from "lucide-react";
import { cn } from "@/lib/utils";
import { postFile, buildFileUrl } from "@/features/shared/api/sharedApi";
// ─── Types ────────────────────────────────────────────────────────────────────
interface DocumentUploadProps {
label?: string;
value?: string;
onChange?: (url: string) => void;
error?: string;
required?: boolean;
disabled?: boolean;
accept?: string;
placeholder?: string;
}
// ─── FieldLabel ───────────────────────────────────────────────────────────────
function FieldLabel({ label, required }: { label?: string; required?: boolean }) {
if (!label) return null;
return (
<label className="block text-xs font-semibold text-muted-foreground uppercase tracking-wider mb-1.5 select-none">
{label}
{required && <span className="text-destructive ml-0.5">*</span>}
</label>
);
}
// ─── ErrorMsg ─────────────────────────────────────────────────────────────────
function ErrorMsg({ error }: { error?: string }) {
if (!error) return null;
return (
<p className="mt-1.5 text-xs text-destructive font-medium flex items-center gap-1.5">
<span className="inline-block w-1 h-1 rounded-full bg-destructive shrink-0" />
{error}
</p>
);
}
// ─── Helper: Extract Filename ────────────────────────────────────────────────
function getDisplayFilename(urlOrName: string): string {
if (!urlOrName) return "";
try {
const parts = urlOrName.split("/");
return decodeURIComponent(parts[parts.length - 1]);
} catch {
return urlOrName;
}
}
const IMAGE_EXT_REGEX = /\.(png|jpe?g|gif|webp|bmp|svg|avif|heic|heif)$/i;
function isImageFileName(urlOrName: string): boolean {
const clean = urlOrName.split("?")[0].split("#")[0];
return IMAGE_EXT_REGEX.test(clean);
}
// ─── DocumentUpload ───────────────────────────────────────────────────────────
export function DocumentUpload({
label,
value,
onChange,
error,
required,
disabled,
accept = ".pdf,.doc,.docx,image/*",
placeholder,
}: DocumentUploadProps) {
const { t } = useTranslation();
const inputRef = useRef<HTMLInputElement>(null);
const [dragOver, setDragOver] = useState(false);
const [uploading, setUploading] = useState(false);
const [uploadError, setUploadError] = useState("");
const defaultPlaceholder = placeholder ?? t("common.fileUploadPlaceholder", "Faýl saýlaň ýa-da süýräp getiriň (PDF, DOCX, Surat)");
const handleFile = async (file: File | null) => {
if (!file || disabled) return;
setUploading(true);
setUploadError("");
try {
const filename = await postFile(file);
const fileUrl = buildFileUrl(filename);
onChange?.(fileUrl);
} catch {
setUploadError(t("common.fileUploadError", "Faýl ýüklenip bilinmedi"));
} finally {
setUploading(false);
if (inputRef.current) inputRef.current.value = "";
}
};
const handleDrop = (e: React.DragEvent) => {
e.preventDefault();
setDragOver(false);
handleFile(e.dataTransfer.files[0] ?? null);
};
const effectiveError = error || uploadError;
const fileName = value ? getDisplayFilename(value) : "";
return (
<div>
<FieldLabel label={label} required={required} />
{value ? (
<div className="p-3 rounded-md border border-border bg-muted/30 space-y-2">
{isImageFileName(value) ? (
<a
href={value.startsWith("http") ? value : buildFileUrl(value)}
target="_blank"
rel="noopener noreferrer"
className="block overflow-hidden rounded-md border border-border bg-background"
>
<img
src={value.startsWith("http") ? value : buildFileUrl(value)}
alt={fileName}
loading="lazy"
className="w-full h-36 object-cover"
/>
</a>
) : (
<div className="flex items-center gap-2.5 min-w-0">
<div className="w-8 h-8 rounded-md bg-primary/10 flex items-center justify-center shrink-0">
<FileText size={16} className="text-primary" />
</div>
<p className="text-sm font-medium text-foreground truncate">{fileName}</p>
</div>
)}
<div className="flex items-center justify-between gap-3">
<a
href={value.startsWith("http") ? value : buildFileUrl(value)}
target="_blank"
rel="noopener noreferrer"
className="text-xs text-primary hover:underline inline-flex items-center gap-1 shrink-0"
>
<ExternalLink size={11} />
{t("common.viewFile", "Faýla seret")}
</a>
{!disabled && (
<button
type="button"
onClick={() => onChange?.("")}
className="p-1.5 rounded-md text-muted-foreground hover:text-destructive hover:bg-destructive/10 transition-colors shrink-0"
title={t("common.remove", "Aýyr")}
>
<X size={16} />
</button>
)}
</div>
</div>
) : (
<div
role="button"
tabIndex={disabled ? -1 : 0}
onClick={() => !disabled && !uploading && inputRef.current?.click()}
onKeyDown={(e) => e.key === "Enter" && !disabled && !uploading && inputRef.current?.click()}
onDragOver={(e) => {
if (disabled) return;
e.preventDefault();
setDragOver(true);
}}
onDragLeave={() => setDragOver(false)}
onDrop={handleDrop}
className={cn(
"flex flex-col items-center justify-center gap-2 px-4 py-5",
"rounded-md border-2 border-dashed cursor-pointer select-none text-center",
"transition-all duration-200 text-sm",
"focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-2",
dragOver && "border-primary bg-primary/5 scale-[1.01]",
effectiveError ? "border-destructive/60" : "border-border",
!dragOver && !effectiveError && "hover:border-primary/50 hover:bg-muted/20",
(disabled || uploading) && "opacity-60 cursor-not-allowed pointer-events-none",
)}
>
{uploading ? (
<div className="flex items-center gap-2 py-1">
<Loader2 size={18} className="animate-spin text-primary" />
<span className="text-sm text-muted-foreground">{t("common.uploading", "Ýüklenýär...")}</span>
</div>
) : (
<>
<div className="w-9 h-9 rounded-full bg-primary/10 flex items-center justify-center">
<Upload size={18} className="text-primary" />
</div>
<p className="text-xs text-muted-foreground max-w-xs">{defaultPlaceholder}</p>
</>
)}
</div>
)}
<input
ref={inputRef}
type="file"
accept={accept}
disabled={disabled || uploading}
onChange={(e) => handleFile(e.target.files?.[0] ?? null)}
className="hidden"
/>
<ErrorMsg error={effectiveError} />
</div>
);
}

View File

@@ -1,66 +1,35 @@
import * as React from "react"
import * as React from "react";
import { cn } from "@/lib/utils"
import { cn } from "@/lib/utils";
function Table({ className, ...props }: React.ComponentProps<"table">) {
return (
<div
data-slot="table-container"
className="relative w-full overflow-x-auto"
>
<table
data-slot="table"
className={cn("w-full caption-bottom text-sm", className)}
{...props}
/>
<div data-slot="table-container" className="relative w-full overflow-x-auto scrollbar-thin">
<table data-slot="table" className={cn("w-full caption-bottom text-sm", className)} {...props} />
</div>
)
);
}
function TableHeader({ className, ...props }: React.ComponentProps<"thead">) {
return (
<thead
data-slot="table-header"
className={cn("[&_tr]:border-b", className)}
{...props}
/>
)
return <thead data-slot="table-header" className={cn("[&_tr]:border-b", className)} {...props} />;
}
function TableBody({ className, ...props }: React.ComponentProps<"tbody">) {
return (
<tbody
data-slot="table-body"
className={cn("[&_tr:last-child]:border-0", className)}
{...props}
/>
)
return <tbody data-slot="table-body" className={cn("[&_tr:last-child]:border-0", className)} {...props} />;
}
function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">) {
return (
<tfoot
data-slot="table-footer"
className={cn(
"border-t bg-muted/50 font-medium [&>tr]:last:border-b-0",
className
)}
{...props}
/>
)
return <tfoot data-slot="table-footer" className={cn("border-t bg-muted/50 font-medium [&>tr]:last:border-b-0", className)} {...props} />;
}
function TableRow({ className, ...props }: React.ComponentProps<"tr">) {
return (
<tr
data-slot="table-row"
className={cn(
"border-b transition-colors hover:bg-muted/50 has-aria-expanded:bg-muted/50 data-[state=selected]:bg-muted",
className
)}
className={cn("border-b transition-colors hover:bg-muted/50 has-aria-expanded:bg-muted/50 data-[state=selected]:bg-muted", className)}
{...props}
/>
)
);
}
function TableHead({ className, ...props }: React.ComponentProps<"th">) {
@@ -69,46 +38,21 @@ function TableHead({ className, ...props }: React.ComponentProps<"th">) {
data-slot="table-head"
className={cn(
"h-10 px-2 text-left align-middle font-medium whitespace-nowrap text-foreground [&:has([role=checkbox])]:pr-0",
className
className,
)}
{...props}
/>
)
);
}
function TableCell({ className, ...props }: React.ComponentProps<"td">) {
return (
<td
data-slot="table-cell"
className={cn(
"p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0",
className
)}
{...props}
/>
)
<td data-slot="table-cell" className={cn("p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0", className)} {...props} />
);
}
function TableCaption({
className,
...props
}: React.ComponentProps<"caption">) {
return (
<caption
data-slot="table-caption"
className={cn("mt-4 text-sm text-muted-foreground", className)}
{...props}
/>
)
function TableCaption({ className, ...props }: React.ComponentProps<"caption">) {
return <caption data-slot="table-caption" className={cn("mt-4 text-sm text-muted-foreground", className)} {...props} />;
}
export {
Table,
TableHeader,
TableBody,
TableFooter,
TableHead,
TableRow,
TableCell,
TableCaption,
}
export { Table, TableHeader, TableBody, TableFooter, TableHead, TableRow, TableCell, TableCaption };