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
+246
-194
@@ -1,136 +1,90 @@
|
||||
import { useState, useMemo } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useState, useEffect } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Pencil, CheckCircle2, Eye, Trash2 } from "lucide-react";
|
||||
import { Pencil, CheckCircle2, UserRound, X } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
import { Section, InfoRow, CollapsibleSection, EmptyState, AuditLog, type AuditRowProps } from "@/components/viewPageComponents";
|
||||
import { DataTableToolbar, type ColumnMeta } from "@/components/dataTableToolbar";
|
||||
import { DataTable, type ColumnDef } from "@/components/dataTable";
|
||||
import { BentoGrid, BentoCard, InfoRow, CollapsibleSection, EmptyState, AuditLog } from "@/components/viewPageComponents";
|
||||
import { DataTableToolbar } from "@/components/dataTableToolbar";
|
||||
import { FormInput } from "@/components/formInput";
|
||||
import { AvatarUpload } from "@/components/avatarUpload";
|
||||
import { FormActions } from "@/components/formActions";
|
||||
import { useAuthStore } from "@/app/store/authStore";
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
interface RoleRow {
|
||||
id: number;
|
||||
code: string;
|
||||
name: string;
|
||||
guardName: string;
|
||||
}
|
||||
|
||||
// ─── Mock Data ────────────────────────────────────────────────────────────────
|
||||
|
||||
const MOCK_ROLES: RoleRow[] = [{ id: 2, code: "superadmin", name: "Superadmin", guardName: "web" }];
|
||||
|
||||
const MOCK_AUDIT: AuditRowProps[] = [
|
||||
{
|
||||
id: "9043",
|
||||
action: "Üýtgetmek",
|
||||
by: "Nurmuhammet Allanov",
|
||||
target: "Ulanyç: Nurmuhammet Allanov (nurmuhammet)",
|
||||
status: "success",
|
||||
date: "2026-06-04 10:23",
|
||||
},
|
||||
{
|
||||
id: "5869",
|
||||
action: "Üýtgetmek",
|
||||
by: "Nurmuhammet Allanov",
|
||||
target: "Ulanyç: Nurmuhammet Allanov (nurmuhammet)",
|
||||
status: "success",
|
||||
date: "2026-06-03 14:11",
|
||||
},
|
||||
];
|
||||
|
||||
// ─── ProfilePage ──────────────────────────────────────────────────────────────
|
||||
import { useProfile, useUpdateProfile } from "@/features/auth/hooks/useLogin";
|
||||
|
||||
export default function ProfilePage() {
|
||||
const { t } = useTranslation();
|
||||
const navigate = useNavigate();
|
||||
const user = useAuthStore((s) => s.user);
|
||||
const { data: profileQueryData, isLoading } = useProfile();
|
||||
const authUser = useAuthStore((s) => s.user);
|
||||
const user = profileQueryData ?? authUser;
|
||||
|
||||
const updateProfileMutation = useUpdateProfile();
|
||||
|
||||
const [isEditing, setIsEditing] = useState(false);
|
||||
|
||||
const [roleSearch, setRoleSearch] = useState("");
|
||||
const [permSearch, setPermSearch] = useState("");
|
||||
const [branchSearch, setBranchSearch] = useState("");
|
||||
const [roleColVisibility, setRoleColVisibility] = useState({});
|
||||
const [roleColOrder, setRoleColOrder] = useState<string[]>([]);
|
||||
|
||||
// ── Role table columns (ColumnDef for DataTable) ───────────────────────────
|
||||
// ── Form State for Editing ──
|
||||
const [formData, setFormData] = useState({
|
||||
name: "",
|
||||
firstName: "",
|
||||
lastName: "",
|
||||
phone: "",
|
||||
avatar: "",
|
||||
});
|
||||
|
||||
const roleColumns: ColumnDef<RoleRow>[] = useMemo(
|
||||
() => [
|
||||
{
|
||||
accessorKey: "id",
|
||||
header: t("common.id", "ID"),
|
||||
cell: ({ row }) => <span className="text-sm font-semibold text-primary">{row.original.id}</span>,
|
||||
size: 60,
|
||||
},
|
||||
{
|
||||
accessorKey: "code",
|
||||
header: t("roles.fields.code", "KOD"),
|
||||
cell: ({ row }) => <span className="text-sm text-foreground">{row.original.code}</span>,
|
||||
},
|
||||
{
|
||||
accessorKey: "name",
|
||||
header: t("roles.fields.name", "ADY"),
|
||||
cell: ({ row }) => <span className="text-sm text-foreground">{row.original.name}</span>,
|
||||
},
|
||||
{
|
||||
accessorKey: "guardName",
|
||||
header: t("roles.fields.guardName", "GUARD NAME"),
|
||||
cell: ({ row }) => <span className="text-sm text-foreground">{row.original.guardName}</span>,
|
||||
},
|
||||
{
|
||||
id: "actions",
|
||||
header: "",
|
||||
enableHiding: false,
|
||||
size: 90,
|
||||
cell: () => (
|
||||
<div className="flex items-center justify-end gap-0.5">
|
||||
<button className="p-1.5 cursor-pointer rounded hover:bg-muted/60 text-muted-foreground hover:text-foreground transition-colors">
|
||||
<Eye size={14} />
|
||||
</button>
|
||||
<button className="p-1.5 cursor-pointer rounded hover:bg-muted/60 text-muted-foreground hover:text-foreground transition-colors">
|
||||
<Pencil size={14} />
|
||||
</button>
|
||||
<button className="p-1.5 cursor-pointer rounded hover:bg-muted/60 text-destructive/70 hover:text-destructive transition-colors">
|
||||
<Trash2 size={14} />
|
||||
</button>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
],
|
||||
[t],
|
||||
);
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
|
||||
// ── ColumnMeta for DataTableToolbar ───────────────────────────────────────
|
||||
useEffect(() => {
|
||||
if (user) {
|
||||
setFormData({
|
||||
name: user.name ?? "",
|
||||
firstName: user.firstName ?? "",
|
||||
lastName: user.lastName ?? "",
|
||||
phone: user.phone ?? "",
|
||||
avatar: user.avatar ?? "",
|
||||
});
|
||||
}
|
||||
}, [user, isEditing]);
|
||||
|
||||
const roleColumnMeta: ColumnMeta[] = useMemo(
|
||||
() => [
|
||||
{ id: "id", label: t("common.id", "ID") },
|
||||
{ id: "code", label: t("roles.fields.code", "KOD") },
|
||||
{ id: "name", label: t("roles.fields.name", "ADY") },
|
||||
{ id: "guardName", label: t("roles.fields.guardName", "GUARD NAME") },
|
||||
],
|
||||
[t],
|
||||
);
|
||||
const handleFieldChange = (field: keyof typeof formData, value: string) => {
|
||||
setFormData((prev) => ({ ...prev, [field]: value }));
|
||||
if (errors[field]) {
|
||||
setErrors((prev) => {
|
||||
const next = { ...prev };
|
||||
delete next[field];
|
||||
return next;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// ── Filtered roles ────────────────────────────────────────────────────────
|
||||
const handleSave = async () => {
|
||||
if (!formData.name.trim()) {
|
||||
setErrors({ name: t("common.errors.required", "Meýdança hökmany doldurylmaly") });
|
||||
return;
|
||||
}
|
||||
|
||||
const filteredRoles = useMemo(() => {
|
||||
const q = roleSearch.toLowerCase().trim();
|
||||
if (!q) return MOCK_ROLES;
|
||||
return MOCK_ROLES.filter(
|
||||
(r) => r.code.toLowerCase().includes(q) || r.name.toLowerCase().includes(q) || r.guardName.toLowerCase().includes(q),
|
||||
);
|
||||
}, [roleSearch]);
|
||||
try {
|
||||
await updateProfileMutation.mutateAsync({
|
||||
name: formData.name.trim(),
|
||||
firstName: formData.firstName.trim() || undefined,
|
||||
lastName: formData.lastName.trim() || undefined,
|
||||
phone: formData.phone.trim() || undefined,
|
||||
avatar: formData.avatar || undefined,
|
||||
});
|
||||
setIsEditing(false);
|
||||
} catch {
|
||||
// Error is handled in useUpdateProfile onError toast
|
||||
}
|
||||
};
|
||||
|
||||
// ── Loading skeleton ──────────────────────────────────────────────────────
|
||||
|
||||
if (!user) {
|
||||
// ── Loading state ──
|
||||
if (isLoading && !user) {
|
||||
return (
|
||||
<div className="p-6 space-y-4">
|
||||
<Skeleton className="h-7 w-64" />
|
||||
<div className="bg-card border border-border rounded-xl overflow-hidden">
|
||||
<div className="bg-card border border-border rounded-xl overflow-hidden p-6 space-y-4">
|
||||
{Array.from({ length: 5 }).map((_, i) => (
|
||||
<div key={i} className="grid grid-cols-[220px_1fr] items-center py-2.5 px-4 border-b border-border last:border-0">
|
||||
<Skeleton className="h-4 w-24" />
|
||||
@@ -142,100 +96,198 @@ export default function ProfilePage() {
|
||||
);
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* ── Title + actions ──────────────────────────────────────────────── */}
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-xl font-bold text-foreground leading-tight">
|
||||
{t("users.viewTitle", "Ulanyjy giňişleýin")}: {user.name}
|
||||
{isEditing ? t("profile.editTitle", "Profili üýtgetmek") : `${t("profile.title", "Meniň profilim")}: ${user.name}`}
|
||||
</h1>
|
||||
<div className="flex items-center gap-2 shrink-0 ml-4">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-8 w-8"
|
||||
onClick={() => navigate("/profile/edit")}
|
||||
title={t("common.edit", "Üýtgetmek")}
|
||||
>
|
||||
<Pencil size={15} />
|
||||
</Button>
|
||||
<div className="flex items-center gap-2 shrink-0">
|
||||
{!isEditing ? (
|
||||
<Button variant="outline" size="sm" className="gap-2" onClick={() => setIsEditing(true)}>
|
||||
<Pencil size={15} />
|
||||
{t("common.edit", "Üýtgetmek")}
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="gap-2 text-muted-foreground hover:text-foreground"
|
||||
onClick={() => setIsEditing(false)}
|
||||
disabled={updateProfileMutation.isPending}
|
||||
>
|
||||
<X size={15} />
|
||||
{t("common.cancel", "Yza")}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* ── Main info ────────────────────────────────────────────────────── */}
|
||||
<Section>
|
||||
<InfoRow label={t("common.id", "ID")} value={user.id} />
|
||||
<InfoRow label={t("users.fields.username", "Ulanyjy ady")} value={user.name} />
|
||||
<InfoRow label={t("users.fields.name", "Ady")} value={user.name} />
|
||||
<InfoRow label={t("users.fields.phone", "Telefon")} value={null} />
|
||||
<InfoRow label={t("users.fields.email", "E-poçta")} value={user.email} />
|
||||
<InfoRow label={t("users.fields.isActive", "İşjeň")}>
|
||||
<CheckCircle2 size={18} className="text-emerald-500" />
|
||||
</InfoRow>
|
||||
</Section>
|
||||
{/* ── Main content ─────────────────────────────────────────────────── */}
|
||||
{!isEditing ? (
|
||||
<>
|
||||
<BentoGrid cols={1}>
|
||||
<BentoCard title={t("profile.sections.basic", "Esasy maglumatlar")}>
|
||||
<div className="flex flex-col items-center gap-4 px-4 pt-4 pb-2">
|
||||
{user.avatar ? (
|
||||
<img
|
||||
src={user.avatar}
|
||||
alt="avatar"
|
||||
className="w-20 h-20 rounded-full border border-border bg-muted/40 object-cover shrink-0 shadow-sm"
|
||||
/>
|
||||
) : (
|
||||
<div className="w-20 h-20 rounded-full border border-border bg-muted/40 flex items-center justify-center shrink-0 shadow-sm">
|
||||
<UserRound size={32} className="text-muted-foreground/60" />
|
||||
</div>
|
||||
)}
|
||||
<div className="min-w-0 text-center">
|
||||
<p className="text-base font-semibold text-foreground truncate">
|
||||
{[user.firstName, user.lastName].filter(Boolean).join(" ") || user.name}
|
||||
</p>
|
||||
{user.phone && <p className="text-xs text-muted-foreground mt-0.5">{user.phone}</p>}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* ── Rollar ───────────────────────────────────────────────────────── */}
|
||||
<CollapsibleSection title={t("users.sections.roles", "Rollar")}>
|
||||
<DataTableToolbar
|
||||
searchValue={roleSearch}
|
||||
onSearchChange={setRoleSearch}
|
||||
searchPlaceholder={t("common.search", "Gözlemek")}
|
||||
columns={roleColumnMeta}
|
||||
columnVisibility={roleColVisibility}
|
||||
onColumnVisibilityChange={setRoleColVisibility}
|
||||
columnOrder={roleColOrder}
|
||||
onColumnOrderChange={setRoleColOrder}
|
||||
actionLabel={t("users.actions.addRole", "Rol birikdiriň")}
|
||||
onAction={() => {}}
|
||||
/>
|
||||
<DataTable
|
||||
columns={roleColumns}
|
||||
data={filteredRoles}
|
||||
getRowId={(row) => String(row.id)}
|
||||
currentPage={1}
|
||||
totalPages={1}
|
||||
totalCount={filteredRoles.length}
|
||||
/>
|
||||
</CollapsibleSection>
|
||||
<InfoRow label={t("common.id", "ID")} value={user.id} />
|
||||
<InfoRow label={t("users.fields.username", "Ulanyjy ady")} value={user.name} />
|
||||
<InfoRow label={t("users.fields.firstName", "At ady")} value={user.firstName ?? "—"} />
|
||||
<InfoRow label={t("users.fields.lastName", "Familiýasy")} value={user.lastName ?? "—"} />
|
||||
<InfoRow label={t("users.fields.phone", "Telefon belgisi")} value={user.phone ?? "—"} />
|
||||
{user.role && <InfoRow label={t("users.fields.role", "Roly")} value={user.role} />}
|
||||
{user.email && <InfoRow label={t("users.fields.email", "E-poçta")} value={user.email} />}
|
||||
<InfoRow
|
||||
label={t("users.fields.createdAt", "Döredilen wagty")}
|
||||
value={user.createdAt ? new Date(user.createdAt).toLocaleString() : "—"}
|
||||
/>
|
||||
<InfoRow label={t("users.fields.isActive", "Işjeň")}>
|
||||
<div className="flex items-center gap-1.5 text-emerald-500 font-medium text-sm">
|
||||
<CheckCircle2 size={16} />
|
||||
<span>{t("common.active", "Işjeň")}</span>
|
||||
</div>
|
||||
</InfoRow>
|
||||
</BentoCard>
|
||||
</BentoGrid>
|
||||
|
||||
{/* ── Rugsatlar ────────────────────────────────────────────────────── */}
|
||||
<CollapsibleSection title={t("users.sections.permissions", "Rugsatlar")}>
|
||||
<DataTableToolbar
|
||||
searchValue={permSearch}
|
||||
onSearchChange={setPermSearch}
|
||||
searchPlaceholder={t("common.search", "Gözlemek")}
|
||||
columns={[]}
|
||||
columnVisibility={{}}
|
||||
onColumnVisibilityChange={() => {}}
|
||||
columnOrder={[]}
|
||||
onColumnOrderChange={() => {}}
|
||||
actionLabel={t("users.actions.addPermission", "Rugsat birikdiriň")}
|
||||
onAction={() => {}}
|
||||
/>
|
||||
<EmptyState label={t("users.empty.permissions", "Berlen kriteriýalara Rugsat gabat gelmedi.")} />
|
||||
</CollapsibleSection>
|
||||
{/* ── Rollar ───────────────────────────────────────────────────────── */}
|
||||
<CollapsibleSection title={t("users.sections.roles", "Rollar")}>
|
||||
<DataTableToolbar
|
||||
searchValue={roleSearch}
|
||||
onSearchChange={setRoleSearch}
|
||||
searchPlaceholder={t("common.search", "Gözlemek")}
|
||||
columns={[]}
|
||||
columnVisibility={{}}
|
||||
onColumnVisibilityChange={() => {}}
|
||||
columnOrder={[]}
|
||||
onColumnOrderChange={() => {}}
|
||||
actionLabel={t("users.actions.addRole", "Rol birikdiriň")}
|
||||
onAction={() => {}}
|
||||
/>
|
||||
<EmptyState label={t("users.empty.roles", "Berlen kriteriýalara Rol gabat gelmedi.")} />
|
||||
</CollapsibleSection>
|
||||
|
||||
{/* ── Şahamçalar ───────────────────────────────────────────────────── */}
|
||||
<CollapsibleSection title={t("users.sections.branches", "Şahamçalar")}>
|
||||
<DataTableToolbar
|
||||
searchValue={branchSearch}
|
||||
onSearchChange={setBranchSearch}
|
||||
searchPlaceholder={t("common.search", "Gözlemek")}
|
||||
columns={[]}
|
||||
columnVisibility={{}}
|
||||
onColumnVisibilityChange={() => {}}
|
||||
columnOrder={[]}
|
||||
onColumnOrderChange={() => {}}
|
||||
actionLabel={t("users.actions.addBranch", "Şahamça birikdiriň")}
|
||||
onAction={() => {}}
|
||||
/>
|
||||
<EmptyState label={t("users.empty.branches", "Berlen kriteriýalara Şahamça gabat gelmedi.")} />
|
||||
</CollapsibleSection>
|
||||
{/* ── Rugsatlar ────────────────────────────────────────────────────── */}
|
||||
<CollapsibleSection title={t("users.sections.permissions", "Rugsatlar")}>
|
||||
<DataTableToolbar
|
||||
searchValue={permSearch}
|
||||
onSearchChange={setPermSearch}
|
||||
searchPlaceholder={t("common.search", "Gözlemek")}
|
||||
columns={[]}
|
||||
columnVisibility={{}}
|
||||
onColumnVisibilityChange={() => {}}
|
||||
columnOrder={[]}
|
||||
onColumnOrderChange={() => {}}
|
||||
actionLabel={t("users.actions.addPermission", "Rugsat birikdiriň")}
|
||||
onAction={() => {}}
|
||||
/>
|
||||
<EmptyState label={t("users.empty.permissions", "Berlen kriteriýalara Rugsat gabat gelmedi.")} />
|
||||
</CollapsibleSection>
|
||||
|
||||
{/* ── Ammallar ─────────────────────────────────────────────────────── */}
|
||||
<CollapsibleSection title={t("users.sections.audit", "Ammallar")}>
|
||||
<AuditLog logs={MOCK_AUDIT} currentPage={1} totalPages={1} totalCount={MOCK_AUDIT.length} />
|
||||
</CollapsibleSection>
|
||||
{/* ── Şahamçalar ───────────────────────────────────────────────────── */}
|
||||
<CollapsibleSection title={t("users.sections.branches", "Şahamçalar")}>
|
||||
<DataTableToolbar
|
||||
searchValue={branchSearch}
|
||||
onSearchChange={setBranchSearch}
|
||||
searchPlaceholder={t("common.search", "Gözlemek")}
|
||||
columns={[]}
|
||||
columnVisibility={{}}
|
||||
onColumnVisibilityChange={() => {}}
|
||||
columnOrder={[]}
|
||||
onColumnOrderChange={() => {}}
|
||||
actionLabel={t("users.actions.addBranch", "Şahamça birikdiriň")}
|
||||
onAction={() => {}}
|
||||
/>
|
||||
<EmptyState label={t("users.empty.branches", "Berlen kriteriýalara Şahamça gabat gelmedi.")} />
|
||||
</CollapsibleSection>
|
||||
|
||||
{/* ── Ammallar ─────────────────────────────────────────────────────── */}
|
||||
<CollapsibleSection title={t("users.sections.audit", "Ammallar")}>
|
||||
<AuditLog logs={[]} currentPage={1} totalPages={1} totalCount={0} />
|
||||
</CollapsibleSection>
|
||||
</>
|
||||
) : (
|
||||
<div className="w-full">
|
||||
<BentoCard title={t("profile.editTitle", "Profili üýtgetmek")}>
|
||||
<div className="space-y-4 p-2">
|
||||
<AvatarUpload
|
||||
label={t("users.fields.avatar", "Awatar")}
|
||||
value={formData.avatar}
|
||||
onChange={(url) => handleFieldChange("avatar", url)}
|
||||
disabled={updateProfileMutation.isPending}
|
||||
/>
|
||||
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("users.fields.name", "Ulanyjy ady")}
|
||||
value={formData.name}
|
||||
onChange={(v) => handleFieldChange("name", v)}
|
||||
error={errors.name}
|
||||
required
|
||||
disabled={updateProfileMutation.isPending}
|
||||
/>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("users.fields.firstName", "At ady")}
|
||||
value={formData.firstName}
|
||||
onChange={(v) => handleFieldChange("firstName", v)}
|
||||
disabled={updateProfileMutation.isPending}
|
||||
/>
|
||||
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("users.fields.lastName", "Familiýasy")}
|
||||
value={formData.lastName}
|
||||
onChange={(v) => handleFieldChange("lastName", v)}
|
||||
disabled={updateProfileMutation.isPending}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("users.fields.phone", "Telefon belgisi")}
|
||||
value={formData.phone}
|
||||
onChange={(v) => handleFieldChange("phone", v)}
|
||||
disabled={updateProfileMutation.isPending}
|
||||
/>
|
||||
|
||||
<div className="pt-4 border-t border-border">
|
||||
<FormActions
|
||||
onSubmit={handleSave}
|
||||
onCancel={() => setIsEditing(false)}
|
||||
isPending={updateProfileMutation.isPending}
|
||||
submitLabel={t("common.save", "Ýatda sakla")}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</BentoCard>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user