cloud-marketplace-query.ts109 lines · main
| 1 | import { useQuery } from '@tanstack/react-query' |
| 2 | |
| 3 | import { cloudMarketplaceKeys } from './keys' |
| 4 | import { get, handleError } from '@/data/fetchers' |
| 5 | import { useProfile } from '@/lib/profile' |
| 6 | import type { ResponseError, UseCustomQueryOptions } from '@/types' |
| 7 | |
| 8 | export type CloudMarketplaceOnboardingInfoVariables = { |
| 9 | buyerId: string |
| 10 | } |
| 11 | |
| 12 | export async function getCloudMarketplaceOnboardingInfo( |
| 13 | { buyerId }: CloudMarketplaceOnboardingInfoVariables, |
| 14 | signal?: AbortSignal |
| 15 | ) { |
| 16 | const { data, error } = await get( |
| 17 | '/platform/cloud-marketplace/buyers/{buyer_id}/onboarding-info', |
| 18 | { |
| 19 | params: { path: { buyer_id: buyerId } }, |
| 20 | signal, |
| 21 | } |
| 22 | ) |
| 23 | |
| 24 | if (error) handleError(error) |
| 25 | |
| 26 | return data |
| 27 | } |
| 28 | |
| 29 | export type CloudMarketplaceOnboardingInfo = Awaited< |
| 30 | ReturnType<typeof getCloudMarketplaceOnboardingInfo> |
| 31 | > |
| 32 | export type CloudMarketplaceOnboardingInfoError = ResponseError |
| 33 | |
| 34 | export const useCloudMarketplaceOnboardingInfoQuery = <TData = CloudMarketplaceOnboardingInfo>( |
| 35 | { buyerId }: CloudMarketplaceOnboardingInfoVariables, |
| 36 | { |
| 37 | enabled = true, |
| 38 | ...options |
| 39 | }: UseCustomQueryOptions< |
| 40 | CloudMarketplaceOnboardingInfo, |
| 41 | CloudMarketplaceOnboardingInfoError, |
| 42 | TData |
| 43 | > = {} |
| 44 | ) => { |
| 45 | const { profile } = useProfile() |
| 46 | return useQuery<CloudMarketplaceOnboardingInfo, CloudMarketplaceOnboardingInfoError, TData>({ |
| 47 | queryKey: cloudMarketplaceKeys.onboardingInfo(buyerId), |
| 48 | queryFn: ({ signal }) => getCloudMarketplaceOnboardingInfo({ buyerId }, signal), |
| 49 | enabled: enabled && profile !== undefined, |
| 50 | ...options, |
| 51 | staleTime: 30 * 60 * 1000, |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | export type CloudMarketplaceContractEligibilityVariables = { |
| 56 | buyerId: string |
| 57 | } |
| 58 | |
| 59 | export async function getCloudMarketplaceContractLinkingEligibility( |
| 60 | { buyerId }: CloudMarketplaceContractEligibilityVariables, |
| 61 | signal?: AbortSignal |
| 62 | ) { |
| 63 | const { data, error } = await get( |
| 64 | '/platform/cloud-marketplace/buyers/{buyer_id}/contract-linking-eligibility', |
| 65 | { |
| 66 | params: { path: { buyer_id: buyerId } }, |
| 67 | signal, |
| 68 | } |
| 69 | ) |
| 70 | |
| 71 | if (error) handleError(error) |
| 72 | |
| 73 | return data |
| 74 | } |
| 75 | |
| 76 | export type CloudMarketplaceContractLinkingEligibility = Awaited< |
| 77 | ReturnType<typeof getCloudMarketplaceContractLinkingEligibility> |
| 78 | > |
| 79 | export type CloudMarketplaceContractLinkingIneligibilityReason = |
| 80 | CloudMarketplaceContractLinkingEligibility['eligibility']['reasons'][0] |
| 81 | |
| 82 | export type CloudMarketplaceContractEligibilityError = ResponseError |
| 83 | |
| 84 | export const useCloudMarketplaceContractLinkingEligibilityQuery = < |
| 85 | TData = CloudMarketplaceContractLinkingEligibility, |
| 86 | >( |
| 87 | { buyerId }: CloudMarketplaceContractEligibilityVariables, |
| 88 | { |
| 89 | enabled = true, |
| 90 | ...options |
| 91 | }: UseCustomQueryOptions< |
| 92 | CloudMarketplaceContractLinkingEligibility, |
| 93 | CloudMarketplaceContractEligibilityError, |
| 94 | TData |
| 95 | > = {} |
| 96 | ) => { |
| 97 | const { profile } = useProfile() |
| 98 | return useQuery< |
| 99 | CloudMarketplaceContractLinkingEligibility, |
| 100 | CloudMarketplaceContractEligibilityError, |
| 101 | TData |
| 102 | >({ |
| 103 | queryKey: cloudMarketplaceKeys.contractLinkingEligibility(buyerId), |
| 104 | queryFn: ({ signal }) => getCloudMarketplaceContractLinkingEligibility({ buyerId }, signal), |
| 105 | enabled: enabled && profile !== undefined, |
| 106 | ...options, |
| 107 | staleTime: 60 * 1000, |
| 108 | }) |
| 109 | } |