31 lines
871 B
TypeScript
31 lines
871 B
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|||
|
|
import { VoucherRowsResponse } from '../../types/services/voucher'
|
||
|
|
import { api } from '../api'
|
||
|
|
|
||
|
|
export interface VouchersQueryParams {
|
||
|
|
rows?: number
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useVoucherRows(params: VouchersQueryParams = {}) {
|
||
|
|
const { rows = 4 } = params
|
||
|
|
|
||
|
|
return useQuery<VoucherRowsResponse>({
|
||
|
|
queryKey: ['voucher-rows', { rows }],
|
||
|
|
queryFn: async () => {
|
||
|
|
const res = await api.get(`/vouchers/rows`, {
|
||
|
|
params: { rows }
|
||
|
|
})
|
||
|
|
return res.data.data
|
||
|
|
},
|
||
|
|
staleTime: 5 * 60 * 1000,
|
||
|
|
refetchOnWindowFocus: false
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// Manual fetch function for cases where you need to fetch without using the hook
|
||
|
|
export async function fetchVoucherRows(rows: number = 4): Promise<VoucherRowsResponse> {
|
||
|
|
const res = await api.get(`/vouchers/rows`, {
|
||
|
|
params: { rows }
|
||
|
|
})
|
||
|
|
return res.data.data
|
||
|
|
}
|