update laye out and update home page also create footer page
This commit is contained in:
34
src/hooks/useCurrentUser.ts
Normal file
34
src/hooks/useCurrentUser.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState, useCallback } from "react";
|
||||
import { getStoredToken } from "@/src/lib/auth";
|
||||
import { userService, extractMeUser } from "@/src/services/user.service";
|
||||
import type { UserMe } from "@/src/types/user";
|
||||
|
||||
export function useCurrentUser() {
|
||||
const [user, setUser] = useState<UserMe | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const token = getStoredToken();
|
||||
const res = await userService.getMe(token);
|
||||
const u = extractMeUser(res.data ?? res);
|
||||
if (!u) throw new Error("لا توجد بيانات");
|
||||
setUser(u as UserMe);
|
||||
} catch {
|
||||
setError("تعذر تحميل بيانات الحساب. حاول مرة أخرى.");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
load();
|
||||
}, [load]);
|
||||
|
||||
return { user, loading, error, reload: load };
|
||||
}
|
||||
@@ -4,7 +4,15 @@ import type { ApiListResponse, User, UserFormData, UserResponse } from "@/src/ty
|
||||
import type { Role } from "@/src/types/role";
|
||||
import type { Branch } from "@/src/types/branch";
|
||||
|
||||
|
||||
export interface MeApiResponse {
|
||||
success: boolean;
|
||||
message: string;
|
||||
responseAt: string;
|
||||
data: {
|
||||
data: User[] | User;
|
||||
meta?: { total: number; page: number; limit: number; totalPages: number };
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**Building a query string to fetch users with search and pagination*/
|
||||
@@ -49,7 +57,14 @@ export const userService = {
|
||||
branch: { name: string };
|
||||
};
|
||||
}>(`users/${id}`, token),
|
||||
getMe: (token: string | null) =>
|
||||
get<MeApiResponse>("users/me", token),
|
||||
};
|
||||
export function extractMeUser(res: MeApiResponse): User | null {
|
||||
const d = res.data?.data;
|
||||
if (Array.isArray(d)) return d[0] ?? null;
|
||||
return d ?? null;
|
||||
}
|
||||
|
||||
/** Converting the form data into a payload suitable for the API */
|
||||
function buildPayload(
|
||||
|
||||
@@ -111,4 +111,10 @@ export interface ApiErrorResponse {
|
||||
path: string;
|
||||
details: { field: string; code: string }[];
|
||||
};
|
||||
}
|
||||
|
||||
export interface UserMe extends User {
|
||||
photo: string | null;
|
||||
isDeleted: boolean;
|
||||
updatedAt: string;
|
||||
}
|
||||
Reference in New Issue
Block a user