connect users section api, fixed some ui, added some lang support to missed words
This commit is contained in:
1 parent
0e75c85fe1
commit
7bb37f9b43
24 files changed
+1871
-1543
No files matched your search
@@ -0,0 +1,157 @@
|
||||
import { useRef, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Upload, X, Loader2 } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { postFile, buildFileUrl } from "@/features/shared/api/sharedApi";
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
interface AvatarUploadProps {
|
||||
label?: string;
|
||||
value?: string;
|
||||
onChange?: (url: string) => void;
|
||||
error?: string;
|
||||
required?: boolean;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
// ─── 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>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── AvatarUpload ─────────────────────────────────────────────────────────────
|
||||
|
||||
export function AvatarUpload({ label, value, onChange, error, required, disabled }: AvatarUploadProps) {
|
||||
const { t } = useTranslation();
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const [dragOver, setDragOver] = useState(false);
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [uploadError, setUploadError] = useState("");
|
||||
|
||||
const handleFile = async (file: File | null) => {
|
||||
if (!file || disabled) return;
|
||||
|
||||
if (!file.type.startsWith("image/")) {
|
||||
setUploadError(t("users.fields.avatarInvalid", "Diňe surat faýllary kabul edilýär"));
|
||||
return;
|
||||
}
|
||||
|
||||
setUploading(true);
|
||||
setUploadError("");
|
||||
try {
|
||||
const filename = await postFile(file);
|
||||
onChange?.(buildFileUrl(filename));
|
||||
} catch {
|
||||
setUploadError(t("users.fields.avatarUploadError", "Surat ýü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;
|
||||
|
||||
return (
|
||||
<div>
|
||||
<FieldLabel label={label} required={required} />
|
||||
|
||||
<div className="flex flex-col items-center gap-4">
|
||||
{/* Preview — rounded, on top */}
|
||||
<div className="w-26 h-26 rounded-full border border-border bg-muted/40 overflow-hidden shrink-0 flex items-center justify-center shadow-sm">
|
||||
{value ? (
|
||||
<img src={value} alt="avatar" className="w-full h-full object-cover" />
|
||||
) : (
|
||||
<Upload size={26} className="text-muted-foreground/60" />
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Upload zone — full width, below */}
|
||||
<div className="w-full flex flex-col gap-2 min-w-0">
|
||||
<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 items-center justify-center gap-2",
|
||||
"min-h-20 rounded-md border-2 border-dashed cursor-pointer select-none px-4",
|
||||
"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 ? (
|
||||
<>
|
||||
<Loader2 size={16} className="animate-spin text-primary" />
|
||||
<span className="text-muted-foreground">{t("users.fields.avatarUploading", "Ýüklenýär...")}</span>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Upload size={16} className="text-primary" />
|
||||
<span className="text-foreground">{t("users.fields.avatarUpload", "Surat saýlaň")}</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{value && !disabled && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onChange?.("")}
|
||||
className="flex items-center gap-1.5 text-xs text-destructive hover:text-destructive/80 transition-colors w-fit mx-auto"
|
||||
>
|
||||
<X size={12} />
|
||||
{t("users.fields.avatarRemove", "Aýyr")}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
disabled={disabled || uploading}
|
||||
onChange={(e) => handleFile(e.target.files?.[0] ?? null)}
|
||||
className="hidden"
|
||||
/>
|
||||
|
||||
<ErrorMsg error={effectiveError} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user