109 lines
4.7 KiB
TypeScript
109 lines
4.7 KiB
TypeScript
import { useMutation, useQuery, useQueryClient, useInfiniteQuery } from "@tanstack/react-query";
|
|
import { toast } from "sonner";
|
|
import { useTranslation } from "react-i18next";
|
|
import { useDebouncedValue, normalizeSearch } from "@/hooks/useDebouncedSearch";
|
|
import { operatorsApi, type OperatorsListParams, type CreateOperatorPayload, type UpdateOperatorPayload } from "../api/operatorsApi";
|
|
|
|
// ─── Query Keys ───────────────────────────────────────────────────────────────
|
|
|
|
export const operatorKeys = {
|
|
all: ["operators"] as const,
|
|
lists: () => [...operatorKeys.all, "list"] as const,
|
|
list: (params: OperatorsListParams) => [...operatorKeys.lists(), params] as const,
|
|
details: () => [...operatorKeys.all, "detail"] as const,
|
|
detail: (id: number) => [...operatorKeys.details(), id] as const,
|
|
};
|
|
|
|
// ─── useOperators ─────────────────────────────────────────────────────────────
|
|
|
|
export function useOperators(params: OperatorsListParams = {}) {
|
|
const debouncedSearch = useDebouncedValue(params.search ?? "");
|
|
const normalized = normalizeSearch(debouncedSearch);
|
|
const queryParams: OperatorsListParams = { ...params, search: normalized || undefined };
|
|
|
|
return useQuery({
|
|
queryKey: operatorKeys.list(queryParams),
|
|
queryFn: ({ signal }) => operatorsApi.getOperators(queryParams, signal),
|
|
placeholderData: (prev) => prev,
|
|
});
|
|
}
|
|
|
|
// ─── useOperator ──────────────────────────────────────────────────────────────
|
|
|
|
export function useOperator(id: number) {
|
|
return useQuery({
|
|
queryKey: operatorKeys.detail(id),
|
|
queryFn: () => operatorsApi.getOperatorById(id),
|
|
enabled: !!id,
|
|
});
|
|
}
|
|
|
|
const STAFF_PAGE_SIZE = 25;
|
|
|
|
/**
|
|
* Paginated staff users (GET /users/staff) for infinite-scroll selects.
|
|
* `search` is debounced server-side search; each keystroke resets to page 1.
|
|
*/
|
|
export function useInfiniteStaff(search = "") {
|
|
const debouncedSearch = useDebouncedValue(search);
|
|
const effectiveSearch = normalizeSearch(debouncedSearch) || undefined;
|
|
|
|
return useInfiniteQuery({
|
|
queryKey: [...operatorKeys.all, "infinite", effectiveSearch] as const,
|
|
initialPageParam: 1,
|
|
queryFn: ({ pageParam, signal }) =>
|
|
operatorsApi.getOperators({ page: pageParam, limit: STAFF_PAGE_SIZE, search: effectiveSearch }, signal),
|
|
getNextPageParam: (lastPage, allPages) => {
|
|
const loaded = allPages.reduce((sum, p) => sum + (p?.users?.length ?? 0), 0);
|
|
const count = lastPage?.count ?? 0;
|
|
return loaded < count ? allPages.length + 1 : undefined;
|
|
},
|
|
});
|
|
}
|
|
|
|
// ─── useCreateOperator ────────────────────────────────────────────────────────
|
|
|
|
export function useCreateOperator() {
|
|
const { t } = useTranslation();
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (payload: CreateOperatorPayload) => operatorsApi.createOperator(payload),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: operatorKeys.lists() });
|
|
toast.success(t("operators.createSuccess", "Operator üstünlikli döredildi"));
|
|
},
|
|
});
|
|
}
|
|
|
|
// ─── useUpdateOperator ────────────────────────────────────────────────────────
|
|
|
|
export function useUpdateOperator(id: number) {
|
|
const { t } = useTranslation();
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (payload: UpdateOperatorPayload) => operatorsApi.updateOperator(id, payload),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: operatorKeys.lists() });
|
|
queryClient.invalidateQueries({ queryKey: operatorKeys.detail(id) });
|
|
toast.success(t("operators.updateSuccess", "Operator üstünlikli täzelendi"));
|
|
},
|
|
});
|
|
}
|
|
|
|
// ─── useDeleteOperator ────────────────────────────────────────────────────────
|
|
|
|
export function useDeleteOperator() {
|
|
const { t } = useTranslation();
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (id: number) => operatorsApi.deleteOperator(id),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: operatorKeys.lists() });
|
|
toast.success(t("operators.deleteSuccess", "Operator üstünlikli pozuldy"));
|
|
},
|
|
});
|
|
}
|