From 1740226294f2289666565ecaf6796094bef7a3fc Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Sun, 28 Jan 2024 18:48:35 +0200 Subject: [PATCH] feat(webapp): hook up latest exchange rate api --- .../Entries/AutoExchangeProvider.tsx | 9 ++--- .../webapp/src/hooks/query/exchangeRates.tsx | 39 ++++++++----------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/packages/webapp/src/containers/Entries/AutoExchangeProvider.tsx b/packages/webapp/src/containers/Entries/AutoExchangeProvider.tsx index 6554b85a1..dcaf86283 100644 --- a/packages/webapp/src/containers/Entries/AutoExchangeProvider.tsx +++ b/packages/webapp/src/containers/Entries/AutoExchangeProvider.tsx @@ -1,6 +1,5 @@ -import { useExchangeRate } from '@/hooks/query'; -import { useCurrentOrganization } from '@/hooks/state'; import React from 'react'; +import { useLatestExchangeRate } from '@/hooks/query'; interface AutoExchangeRateProviderProps { children: React.ReactNode; @@ -18,15 +17,15 @@ const AutoExchangeRateContext = React.createContext( function AutoExchangeRateProvider({ children }: AutoExchangeRateProviderProps) { const [autoExRateCurrency, setAutoExRateCurrency] = React.useState(''); - const currentOrganization = useCurrentOrganization(); // Retrieves the exchange rate. const { data: autoExchangeRate, isLoading: isAutoExchangeRateLoading } = - useExchangeRate(autoExRateCurrency, currentOrganization.base_currency, { - enabled: Boolean(currentOrganization.base_currency && autoExRateCurrency), + useLatestExchangeRate(autoExRateCurrency, { + enabled: Boolean(autoExRateCurrency), refetchOnWindowFocus: false, staleTime: 0, cacheTime: 0, + retry: 0, }); const value = { diff --git a/packages/webapp/src/hooks/query/exchangeRates.tsx b/packages/webapp/src/hooks/query/exchangeRates.tsx index f56958040..a0f58f7ac 100644 --- a/packages/webapp/src/hooks/query/exchangeRates.tsx +++ b/packages/webapp/src/hooks/query/exchangeRates.tsx @@ -1,34 +1,27 @@ // @ts-nocheck import { useQuery } from 'react-query'; 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. */ -export function useExchangeRate( - fromCurrency: string, - toCurrency: string, - props, -) { - return useQuery( - [QUERY_TYPES.EXCHANGE_RATE, fromCurrency, toCurrency], - async () => { - await delay(100); +export function useLatestExchangeRate(toCurrency: string, props) { + const apiRequest = useApiRequest(); - return { - from_currency: fromCurrency, - to_currency: toCurrency, - exchange_rate: 1.00, - }; - }, + return useQuery( + [QUERY_TYPES.EXCHANGE_RATE, toCurrency], + () => + apiRequest + .http({ + url: `/api/exchange_rates/latest`, + method: 'get', + params: { + to_currency: toCurrency, + }, + }) + .then((res) => res.data), props, ); }