mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 15:20:34 +00:00
feat(webapp): hook up latest exchange rate api
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import { useExchangeRate } from '@/hooks/query';
|
|
||||||
import { useCurrentOrganization } from '@/hooks/state';
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { useLatestExchangeRate } from '@/hooks/query';
|
||||||
|
|
||||||
interface AutoExchangeRateProviderProps {
|
interface AutoExchangeRateProviderProps {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
@@ -18,15 +17,15 @@ const AutoExchangeRateContext = React.createContext(
|
|||||||
function AutoExchangeRateProvider({ children }: AutoExchangeRateProviderProps) {
|
function AutoExchangeRateProvider({ children }: AutoExchangeRateProviderProps) {
|
||||||
const [autoExRateCurrency, setAutoExRateCurrency] =
|
const [autoExRateCurrency, setAutoExRateCurrency] =
|
||||||
React.useState<string>('');
|
React.useState<string>('');
|
||||||
const currentOrganization = useCurrentOrganization();
|
|
||||||
|
|
||||||
// Retrieves the exchange rate.
|
// Retrieves the exchange rate.
|
||||||
const { data: autoExchangeRate, isLoading: isAutoExchangeRateLoading } =
|
const { data: autoExchangeRate, isLoading: isAutoExchangeRateLoading } =
|
||||||
useExchangeRate(autoExRateCurrency, currentOrganization.base_currency, {
|
useLatestExchangeRate(autoExRateCurrency, {
|
||||||
enabled: Boolean(currentOrganization.base_currency && autoExRateCurrency),
|
enabled: Boolean(autoExRateCurrency),
|
||||||
refetchOnWindowFocus: false,
|
refetchOnWindowFocus: false,
|
||||||
staleTime: 0,
|
staleTime: 0,
|
||||||
cacheTime: 0,
|
cacheTime: 0,
|
||||||
|
retry: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
const value = {
|
const value = {
|
||||||
|
|||||||
@@ -1,34 +1,27 @@
|
|||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
import { useQuery } from 'react-query';
|
import { useQuery } from 'react-query';
|
||||||
import QUERY_TYPES from './types';
|
import QUERY_TYPES from './types';
|
||||||
|
import useApiRequest from '../useRequest';
|
||||||
|
|
||||||
function getRandomItemFromArray(arr) {
|
|
||||||
const randomIndex = Math.floor(Math.random() * arr.length);
|
|
||||||
return arr[randomIndex];
|
|
||||||
}
|
|
||||||
function delay(t, val) {
|
|
||||||
return new Promise((resolve) => setTimeout(resolve, t, val));
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Retrieves tax rates.
|
* Retrieves latest exchange rate.
|
||||||
* @param {number} customerId - Customer id.
|
* @param {number} customerId - Customer id.
|
||||||
*/
|
*/
|
||||||
export function useExchangeRate(
|
export function useLatestExchangeRate(toCurrency: string, props) {
|
||||||
fromCurrency: string,
|
const apiRequest = useApiRequest();
|
||||||
toCurrency: string,
|
|
||||||
props,
|
|
||||||
) {
|
|
||||||
return useQuery(
|
|
||||||
[QUERY_TYPES.EXCHANGE_RATE, fromCurrency, toCurrency],
|
|
||||||
async () => {
|
|
||||||
await delay(100);
|
|
||||||
|
|
||||||
return {
|
return useQuery(
|
||||||
from_currency: fromCurrency,
|
[QUERY_TYPES.EXCHANGE_RATE, toCurrency],
|
||||||
to_currency: toCurrency,
|
() =>
|
||||||
exchange_rate: 1.00,
|
apiRequest
|
||||||
};
|
.http({
|
||||||
},
|
url: `/api/exchange_rates/latest`,
|
||||||
|
method: 'get',
|
||||||
|
params: {
|
||||||
|
to_currency: toCurrency,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then((res) => res.data),
|
||||||
props,
|
props,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user