Skip to content

Commit

Permalink
refactor useExchangeCalculation hook
Browse files Browse the repository at this point in the history
  • Loading branch information
alisher-epam committed Feb 14, 2024
1 parent e7e6c0a commit 32bad4c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
15 changes: 11 additions & 4 deletions src/common/hooks/useExchangeCalculation/useExchangeCalculation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from 'react';
import { useQuery } from 'react-query';

import { useExchangeRateValue } from '@folio/stripes-acq-components';
import {
useOkapiKy,
useNamespace,
Expand All @@ -20,10 +21,16 @@ export const useExchangeCalculation = ({ from, to, amount, rate }, options = {})
const ky = useOkapiKy();
const [namespace] = useNamespace({ key: 'exchange-calculation' });

const { exchangeRate } = useExchangeRateValue(
from,
to,
rate,
);

const [searchParams, setSearchParams] = useState({
amount,
from,
rate,
rate: rate || exchangeRate,
to,
});

Expand All @@ -32,16 +39,16 @@ export const useExchangeCalculation = ({ from, to, amount, rate }, options = {})
setSearchParams({
amount,
from,
rate,
rate: rate || exchangeRate,
to,
});
}, DEBOUNCE_DELAY), [amount, from, rate, to]);
}, DEBOUNCE_DELAY), [amount, from, rate, to, exchangeRate]);

useEffect(() => {
debounceSetSearchParams();

return () => debounceSetSearchParams.cancel();
}, [amount, debounceSetSearchParams, from, rate, to]);
}, [amount, debounceSetSearchParams, from, rate, to, exchangeRate]);

const {
amount: amountProp,
Expand Down
9 changes: 1 addition & 8 deletions src/invoices/InvoiceDetails/InvoiceDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
TagsBadge,
TagsPane,
useAcqRestrictions,
useExchangeRateValue,
} from '@folio/stripes-acq-components';

import { PrintVoucherContainer } from '../PrintVoucher';
Expand Down Expand Up @@ -108,12 +107,6 @@ function InvoiceDetails({
const isVendorInactive = vendor?.status === VENDOR_STATUS.INACTIVE;
const showHasPendingOrdersMessage = hasPendingOrders && !isPendingOrdersLoading;

const { exchangeRate } = useExchangeRateValue(
invoice.currency,
stripes.currency,
invoice.exchangeRate,
);

const shortcuts = [
{
name: 'new',
Expand Down Expand Up @@ -294,7 +287,7 @@ function InvoiceDetails({
approvalDate={invoice.approvalDate}
approvedBy={invoice.approvedBy}
batchGroupId={invoice.batchGroupId}
exchangeRate={invoice.exchangeRate || exchangeRate}
exchangeRate={invoice.exchangeRate}
fiscalYearId={invoice.fiscalYearId}
invoiceDate={invoice.invoiceDate}
paymentDate={invoice.paymentDate}
Expand Down
11 changes: 1 addition & 10 deletions src/invoices/InvoiceForm/InvoiceForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import {
PAYMENT_METHOD_OPTIONS,
TextField,
validateRequired,
useExchangeRateValue,
} from '@folio/stripes-acq-components';

import {
Expand Down Expand Up @@ -105,8 +104,6 @@ const InvoiceForm = ({
const intl = useIntl();
const history = useHistory();
const accordionStatusRef = useRef();
const stripes = useStripes();
const systemCurrency = stripes.currency;

const {
batch,
Expand Down Expand Up @@ -139,12 +136,6 @@ const InvoiceForm = ({
fiscalYearId,
} = initialValues;

const { exchangeRate: exchangeRateValue } = useExchangeRateValue(
values.currency,
systemCurrency,
values.exchangeRate,
);

const [selectedVendor, setSelectedVendor] = useState();
const [isLockTotalAmountEnabled, setLockTotalAmountEnabled] = useState(isNumber(lockTotal));

Expand Down Expand Up @@ -620,7 +611,7 @@ const InvoiceForm = ({
/>
<CalculatedExchangeAmount
currency={values.currency}
exchangeRate={values.exchangeRate || exchangeRateValue}
exchangeRate={values.exchangeRate}
total={values.total}
/>
</Accordion>
Expand Down

0 comments on commit 32bad4c

Please sign in to comment.