fix docs
This commit is contained in:
81
src/features/b2b/api/b2bApi.ts
Normal file
81
src/features/b2b/api/b2bApi.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { getCsrfToken } from '@shared/api/csrf'
|
||||
import { refreshAccessToken } from '@shared/api/tokenStore'
|
||||
|
||||
const B2B_API_URL = 'https://app.b2b.elcsa.ru/api'
|
||||
|
||||
async function doB2bRequest<T>(
|
||||
path: string,
|
||||
options: RequestInit,
|
||||
allowRetry: boolean,
|
||||
): Promise<T> {
|
||||
const csrf = await getCsrfToken()
|
||||
|
||||
const res = await fetch(`${B2B_API_URL}${path}`, {
|
||||
...options,
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'X-CSRF-Token': csrf,
|
||||
...options.headers,
|
||||
},
|
||||
})
|
||||
|
||||
if (res.status === 401 && allowRetry) {
|
||||
try {
|
||||
await refreshAccessToken()
|
||||
return doB2bRequest<T>(path, options, false)
|
||||
} catch {
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
}
|
||||
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw data
|
||||
return data as T
|
||||
}
|
||||
|
||||
export interface CreatePurchaseRequestBody {
|
||||
usdt_amount: number | string
|
||||
comment?: string | null
|
||||
target_wallet_chain?: string | null
|
||||
target_wallet_address?: string | null
|
||||
}
|
||||
|
||||
export interface B2bPurchaseRequest {
|
||||
id: string
|
||||
organization_id: string
|
||||
status: string
|
||||
usdt_amount: string
|
||||
rub_amount: string | null
|
||||
exchange_rate: string | null
|
||||
service_fee_percent: string | null
|
||||
comment: string | null
|
||||
admin_comment: string | null
|
||||
target_wallet_chain: string | null
|
||||
target_wallet_address: string | null
|
||||
tx_hash: string | null
|
||||
created_at: string | null
|
||||
updated_at: string | null
|
||||
completed_at: string | null
|
||||
}
|
||||
|
||||
export interface B2bPurchaseRequestListResponse {
|
||||
items: B2bPurchaseRequest[]
|
||||
total: number
|
||||
}
|
||||
|
||||
export function createPurchaseRequest(
|
||||
body: CreatePurchaseRequestBody,
|
||||
): Promise<B2bPurchaseRequest> {
|
||||
return doB2bRequest('/purchase-requests', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
}, true)
|
||||
}
|
||||
|
||||
export function getMyPurchaseRequests(
|
||||
limit = 50,
|
||||
offset = 0,
|
||||
): Promise<B2bPurchaseRequestListResponse> {
|
||||
return doB2bRequest(`/purchase-requests?limit=${limit}&offset=${offset}`, {}, true)
|
||||
}
|
||||
13
src/features/b2b/hooks/useCreatePurchaseRequest.ts
Normal file
13
src/features/b2b/hooks/useCreatePurchaseRequest.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { createPurchaseRequest } from '../api/b2bApi'
|
||||
|
||||
export function useCreatePurchaseRequest() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: createPurchaseRequest,
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['b2b', 'purchase-requests'] })
|
||||
},
|
||||
})
|
||||
}
|
||||
10
src/features/b2b/hooks/useMyPurchaseRequests.ts
Normal file
10
src/features/b2b/hooks/useMyPurchaseRequests.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { getMyPurchaseRequests } from '../api/b2bApi'
|
||||
|
||||
export function useMyPurchaseRequests() {
|
||||
return useQuery({
|
||||
queryKey: ['b2b', 'purchase-requests'],
|
||||
queryFn: () => getMyPurchaseRequests(),
|
||||
staleTime: 30_000,
|
||||
})
|
||||
}
|
||||
7
src/features/b2b/index.ts
Normal file
7
src/features/b2b/index.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export { useCreatePurchaseRequest } from './hooks/useCreatePurchaseRequest'
|
||||
export { useMyPurchaseRequests } from './hooks/useMyPurchaseRequests'
|
||||
export type {
|
||||
CreatePurchaseRequestBody,
|
||||
B2bPurchaseRequest,
|
||||
B2bPurchaseRequestListResponse,
|
||||
} from './api/b2bApi'
|
||||
Reference in New Issue
Block a user