Files
tbbank-frontend/src/features/onlinePaymentHistory/hooks/useOnlinePaymentsHistory.ts
T

88 lines
3.7 KiB
TypeScript

// features/onlinePaymentHistory/hooks/useOnlinePaymentsHistory.ts
import { useQuery } from "@tanstack/react-query";
import {
fetchOnlinePayments,
fetchOnlinePaymentById,
fetchOnlinePaymentDetail,
fetchOrderPaymentHistory,
fetchPaymentGateway,
type OnlinePaymentListParams,
} from "../api/onlinePaymentsHistoryApi";
// ─── Query Keys ───────────────────────────────────────────────────────────────
export const onlinePaymentKeys = {
all: ["onlinePayments"] as const,
lists: () => [...onlinePaymentKeys.all, "list"] as const,
list: (params: OnlinePaymentListParams) => [...onlinePaymentKeys.lists(), params] as const,
detail: (id: number | string) => [...onlinePaymentKeys.all, "detail", String(id)] as const,
paymentDetail: (id: number | string) => [...onlinePaymentKeys.all, "paymentDetail", String(id)] as const,
paymentGateway: (id: number | string) => [...onlinePaymentKeys.all, "paymentGateway", String(id)] as const,
orderHistory: (id: number | string, params: Record<string, unknown>) =>
[...onlinePaymentKeys.all, "orderHistory", String(id), params] as const,
};
// ─── useGetOnlinePayments ─────────────────────────────────────────────────────
export function useGetOnlinePayments(params: OnlinePaymentListParams = {}, options?: { enabled?: boolean }) {
return useQuery({
queryKey: onlinePaymentKeys.list(params),
queryFn: () => fetchOnlinePayments(params),
enabled: options?.enabled ?? true,
});
}
// ─── useGetOnlinePaymentById ──────────────────────────────────────────────────
/**
* Fetches the step-by-step history log for a single payment order.
* Returns an array of PaymentHistoryStep.
*/
export function useGetOnlinePaymentById(id: number | string) {
return useQuery({
queryKey: onlinePaymentKeys.detail(id),
queryFn: () => fetchOnlinePaymentById(Number(id)),
enabled: !!id,
});
}
// ─── useGetOnlinePaymentDetail ────────────────────────────────────────────────
/**
* Fetches the detailed payment record from /payment-history/payments/:id
*/
export function useGetOnlinePaymentDetail(id: number | string) {
return useQuery({
queryKey: onlinePaymentKeys.paymentDetail(id),
queryFn: () => fetchOnlinePaymentDetail(id),
enabled: !!id,
});
}
// ─── useGetOrderPaymentHistory ────────────────────────────────────────────────
/**
* Fetches the paginated payment history for a specific order /payment-history/:id
*/
export function useGetOrderPaymentHistory(orderId: number | string | undefined, params: { page?: number; limit?: number } = {}) {
return useQuery({
queryKey: onlinePaymentKeys.orderHistory(orderId ?? "", params),
queryFn: () => fetchOrderPaymentHistory(orderId!, params),
enabled: Boolean(orderId),
});
}
// ─── useGetPaymentGateway ─────────────────────────────────────────────────────
/**
* Fetches payment gateway details from /payment-history/payments/:id/gateway
*/
export function useGetPaymentGateway(id: number | string | undefined) {
return useQuery({
queryKey: onlinePaymentKeys.paymentGateway(id ?? ""),
queryFn: () => fetchPaymentGateway(id!),
enabled: Boolean(id),
});
}