create middleware block and update proxy file also make token stored in cookes and up date user page to can show user details also fixed delete user error and delete car error and broken corse inginx image block now we can uplode image to server and but it in my app

This commit is contained in:
m7amedez5511
2026-06-17 17:01:53 +03:00
parent 6ca2cc08d1
commit a23d21f222
15 changed files with 1017 additions and 137 deletions

View File

@@ -50,8 +50,8 @@ export async function request<T>(
}
//return res.json() as Promise<T>;
const text = await res.text();
return (text ? JSON.parse(text) : null) as Promise<T>;
const text = await res.text();
return (text && text.trim().length > 0 ? JSON.parse(text) : null) as T;
}
// ── Convenience shorthands ────────────────────────────
@@ -68,3 +68,5 @@ export const put = <T>(path: string, body: unknown, token?: string | null) =>
export const del = <T>(path: string, token?: string | null) =>
request<T>(path, { method: "DELETE" }, token);

View File

@@ -20,36 +20,52 @@ export const userService = {
get<ApiListResponse<User>>(`users${buildUsersQuery(page, search)}`, token),
/** إنشاء مستخدم جديد */
create: (data: Omit<UserFormData, "password"> & { password: string }, token: string | null) =>
post<UserResponse>("users", buildPayload(data, true), token),
create: (
data: Omit<UserFormData, "password"> & { password: string },
token: string | null,
) => post<UserResponse>("users", buildPayload(data, true), token),
/** تعديل بيانات مستخدم موجود */
update: (id: string, data: UserFormData, token: string | null) =>
put<UserResponse>(`users/${id}`, buildPayload(data, false), token),
/** حذف مستخدم */
delete: (id: string, token: string | null) =>
del<void>(`users/${id}`, token),
/** جلب قائمة الأدوار لاستخدامها في نموذج المستخدم */
delete: (id: string, token: string | null) => del<void>(`users/${id}`, token),
//get role
getRoles: (token: string | null) =>
get<{ data: { data: Role[] } }>("role?limit=100", token),
/** جلب قائمة الفروع لاستخدامها في نموذج المستخدم */
//get Branches for user form
getBranches: (token: string | null) =>
get<{ data: { data: Branch[] } }>("branches?limit=100", token),
//get user by id
getById: (id: string, token: string | null) =>
get<{
data: User & {
photo: string | null;
refreshToken: string | null;
isDeleted: boolean;
deletedAt: string | null;
updatedAt: string;
passwordChangedAt: string | null;
role: { name: string; description: string };
branch: { name: string };
};
}>(`users/${id}`, token),
};
/** تحويل بيانات النموذج إلى payload مناسب للـ API */
function buildPayload(data: UserFormData, isNew: boolean): Record<string, string> {
function buildPayload(
data: UserFormData,
isNew: boolean,
): Record<string, string> {
const payload: Record<string, string> = {
name: data.name.trim(),
phone: data.phone.trim(),
roleId: data.roleId,
name: data.name.trim(),
phone: data.phone.trim(),
roleId: data.roleId,
branchId: data.branchId,
};
if (data.email) payload.email = data.email.trim();
if (isNew && data.password) payload.password = data.password;
else if (!isNew && data.password) payload.password = data.password;
if (data.email) payload.email = data.email.trim();
if (isNew && data.password) payload.password = data.password;
else if (!isNew && data.password) payload.password = data.password;
return payload;
}
}