mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 05:40:31 +00:00
feat: implement auto entries rates re-calculation after change the exchange rate
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { useExchangeRate } from '@/hooks/query';
|
||||
import { useCurrentOrganization } from '@/hooks/state';
|
||||
import React from 'react';
|
||||
|
||||
interface AutoExchangeRateProviderProps {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
interface AutoExchangeRateProviderValue {
|
||||
autoExRateCurrency: string;
|
||||
isAutoExchangeRateLoading: boolean;
|
||||
}
|
||||
|
||||
const AutoExchangeRateContext = React.createContext(
|
||||
{} as AutoExchangeRateProviderValue,
|
||||
);
|
||||
|
||||
function AutoExchangeRateProvider({ children }: AutoExchangeRateProviderProps) {
|
||||
const [autoExRateCurrency, setAutoExRateCurrency] =
|
||||
React.useState<string>('');
|
||||
const currentOrganization = useCurrentOrganization();
|
||||
|
||||
// Retrieves the exchange rate.
|
||||
const { data: autoExchangeRate, isLoading: isAutoExchangeRateLoading } =
|
||||
useExchangeRate(autoExRateCurrency, currentOrganization.base_currency, {
|
||||
enabled: Boolean(currentOrganization.base_currency && autoExRateCurrency),
|
||||
refetchOnWindowFocus: false,
|
||||
staleTime: 0,
|
||||
cacheTime: 0,
|
||||
});
|
||||
|
||||
const value = {
|
||||
autoExRateCurrency,
|
||||
setAutoExRateCurrency,
|
||||
isAutoExchangeRateLoading,
|
||||
autoExchangeRate,
|
||||
};
|
||||
|
||||
return (
|
||||
<AutoExchangeRateContext.Provider value={value}>
|
||||
{children}
|
||||
</AutoExchangeRateContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
const useAutoExRateContext = () => React.useContext(AutoExchangeRateContext);
|
||||
|
||||
export {
|
||||
useAutoExRateContext,
|
||||
AutoExchangeRateContext,
|
||||
AutoExchangeRateProvider,
|
||||
};
|
||||
Reference in New Issue
Block a user