181 lines
5.8 KiB
React
181 lines
5.8 KiB
React
import { createContext, useContext, useState, useCallback } from "react";
|
|
import api from "@/utils/api.util";
|
|
import { toast } from "sonner";
|
|
|
|
const StaffGroupContext = createContext(null);
|
|
|
|
export const useStaffGroups = () => {
|
|
const ctx = useContext(StaffGroupContext);
|
|
if (!ctx) throw new Error("useStaffGroups must be used inside StaffGroupProvider");
|
|
return ctx;
|
|
};
|
|
|
|
const BASE = "/staff";
|
|
|
|
const PAGINATION_INIT = {
|
|
page: 1,
|
|
limit: 10,
|
|
totalRecords: 0,
|
|
totalPages: 0,
|
|
hasPrevPage: false,
|
|
hasNextPage: false,
|
|
};
|
|
|
|
export const StaffGroupProvider = ({ children }) => {
|
|
const [groups, setGroups] = useState([]);
|
|
const [group, setGroup] = useState(null);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState(null);
|
|
|
|
// ── Members pagination state ───────────────────────────────────────────────
|
|
const [members, setMembers] = useState([]);
|
|
const [memberAttributes, setMemberAttributes] = useState([]);
|
|
const [memberPagination, setMemberPagination] = useState(PAGINATION_INIT);
|
|
const [membersLoading, setMembersLoading] = useState(false);
|
|
|
|
const request = useCallback(async (fn) => {
|
|
setLoading(true);
|
|
setError(null);
|
|
try {
|
|
return await fn();
|
|
} catch (err) {
|
|
const message = err?.response?.data?.message || err.message || "Something went wrong.";
|
|
setError(message);
|
|
toast(message);
|
|
return null;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
// Lightweight wrapper for members-specific loading flag
|
|
// so it doesn't block the whole page when paginating/filtering
|
|
const membersRequest = useCallback(async (fn) => {
|
|
setMembersLoading(true);
|
|
try {
|
|
return await fn();
|
|
} catch (err) {
|
|
const message = err?.response?.data?.message || err.message || "Something went wrong.";
|
|
toast(message);
|
|
return null;
|
|
} finally {
|
|
setMembersLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
// ─── GET /api/staff/groups ────────────────────────────────────────────────
|
|
const fetchMyGroups = useCallback(
|
|
() =>
|
|
request(async () => {
|
|
const res = await api.get(`${BASE}/groups`);
|
|
setGroups(res.data?.data ?? []);
|
|
return res.data;
|
|
}),
|
|
[request]
|
|
);
|
|
|
|
// ─── GET /api/staff/groups/:group_id ─────────────────────────────────────
|
|
const fetchGroupById = useCallback(
|
|
(groupId) =>
|
|
request(async () => {
|
|
const res = await api.get(`${BASE}/groups/${groupId}`);
|
|
setGroup(res.data?.data ?? null);
|
|
return res.data;
|
|
}),
|
|
[request]
|
|
);
|
|
|
|
// ─── GET /api/staff/groups/by-code/:group_code ───────────────────────────
|
|
const fetchGroupByCode = useCallback(
|
|
(groupCode) =>
|
|
request(async () => {
|
|
const res = await api.get(`${BASE}/groups/by-code/${groupCode}`);
|
|
setGroup(res.data?.data ?? null);
|
|
return res.data;
|
|
}),
|
|
[request]
|
|
);
|
|
|
|
// ─── GET /api/staff/groups/:group_id/members ─────────────────────────────
|
|
// Paginated, searchable, filterable — mirrors the paginate() pattern.
|
|
// params: { page, limit, search, filters, sort }
|
|
const fetchGroupMembers = useCallback(
|
|
(groupId, params = {}) =>
|
|
membersRequest(async () => {
|
|
const res = await api.get(`${BASE}/groups/${groupId}/members`, {
|
|
params: {
|
|
page: params.page ?? 1,
|
|
limit: params.limit ?? 10,
|
|
search: params.search ?? undefined,
|
|
filters: params.filters?.length
|
|
? JSON.stringify(params.filters)
|
|
: undefined,
|
|
sort: params.sort?.length
|
|
? JSON.stringify(params.sort)
|
|
: undefined,
|
|
},
|
|
});
|
|
|
|
const payload = res.data?.data;
|
|
|
|
setMembers(payload.data ?? []);
|
|
setMemberAttributes(payload?.attributes ?? []);
|
|
setMemberPagination({
|
|
page: payload?.page ?? 1,
|
|
limit: payload?.limit ?? 10,
|
|
total: payload?.total ?? 0,
|
|
totalPages: payload?.totalPages ?? 1,
|
|
});
|
|
|
|
return res.data;
|
|
}),
|
|
[membersRequest]
|
|
);
|
|
|
|
// ─── GET /api/staff/groups/:group_id/members/field-values ────────────────
|
|
// Fetches distinct values for a given column — used by FilterSheet.
|
|
// column: the column id (e.g. "acc_type", "is_active")
|
|
// params: forwarded query params (search, page, limit, etc.)
|
|
const fetchGroupMemberFieldValues = useCallback(
|
|
(groupId, column, params = {}) =>
|
|
membersRequest(async () => {
|
|
const res = await api.get(
|
|
`${BASE}/groups/${groupId}/members/field-values`,
|
|
{
|
|
params: {
|
|
column,
|
|
page: params.page ?? 1,
|
|
limit: params.limit ?? 20,
|
|
search: params.search ?? undefined,
|
|
},
|
|
}
|
|
);
|
|
return res.data;
|
|
}),
|
|
[membersRequest]
|
|
);
|
|
|
|
return (
|
|
<StaffGroupContext.Provider
|
|
value={{
|
|
// Groups
|
|
groups, group, loading, error,
|
|
setGroup,
|
|
fetchMyGroups,
|
|
fetchGroupById,
|
|
fetchGroupByCode,
|
|
|
|
// Members (paginated)
|
|
members,
|
|
memberAttributes,
|
|
memberPagination,
|
|
setMemberPagination,
|
|
membersLoading,
|
|
fetchGroupMembers,
|
|
fetchGroupMemberFieldValues,
|
|
}}
|
|
>
|
|
{children}
|
|
</StaffGroupContext.Provider>
|
|
);
|
|
}; |