Skip to content

Commit

Permalink
Reset estimate when modal is closed, using a composable
Browse files Browse the repository at this point in the history
  • Loading branch information
mraveux committed Apr 15, 2021
1 parent 44258b4 commit b8636c2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
9 changes: 3 additions & 6 deletions src/components/modals/BuyCryptoModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@
</template>

<script lang="ts">
import { defineComponent, ref, computed, watch, onMounted, onUnmounted } from '@vue/composition-api';
import { defineComponent, ref, computed, watch, onMounted } from '@vue/composition-api';
import {
PageHeader,
PageBody,
Expand Down Expand Up @@ -331,7 +331,7 @@ import {
capDecimals,
currentLimitCrypto,
currentLimitFiat,
estimate,
useSwapEstimate,
eurPerBtc,
eurPerNim,
fetchAssets,
Expand Down Expand Up @@ -361,6 +361,7 @@ export default defineComponent({
const { width } = useWindowSize();
const { limits } = useSwapLimits({ nimAddress: activeAddress.value! });
const { estimate } = useSwapEstimate();
const $eurAmountInput = ref<typeof AmountInput & { focus(): void } | null>(null);
Expand Down Expand Up @@ -911,10 +912,6 @@ export default defineComponent({
fiatAmount.value = (currentLimitFiat.value || 0) * 1e2;
}
onUnmounted(() => {
estimate.value = null;
});
return {
$eurAmountInput,
addressListOpened,
Expand Down
9 changes: 3 additions & 6 deletions src/components/modals/SellCryptoModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
</template>

<script lang="ts">
import { defineComponent, ref, computed, watch, onMounted, onUnmounted } from '@vue/composition-api';
import { defineComponent, ref, computed, watch, onMounted } from '@vue/composition-api';
import {
PageHeader,
PageBody,
Expand Down Expand Up @@ -356,7 +356,7 @@ import {
capDecimals,
currentLimitCrypto,
currentLimitFiat,
estimate,
useSwapEstimate,
eurPerBtc,
eurPerNim,
fetchAssets,
Expand Down Expand Up @@ -396,6 +396,7 @@ export default defineComponent({
const { width } = useWindowSize();
const { limits } = useSwapLimits({ nimAddress: activeAddress.value! });
const { trials } = useSettingsStore();
const { estimate } = useSwapEstimate();
const $cryptoAmountInput = ref<typeof AmountInput & { focus(): void } | null>(null);
Expand Down Expand Up @@ -997,10 +998,6 @@ export default defineComponent({
}
}
onUnmounted(() => {
estimate.value = null;
});
return {
$cryptoAmountInput,
addressListOpened,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/swap/utils/BuyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
HtlcStatus,
} from '../../OasisApi';
import { i18n } from '../../../i18n/i18n-setup';
import { calculateFees, estimate, getFiatSwapParameters, selectedFiatCurrency } from './CommonUtils';
import { calculateFees, getFiatSwapParameters, selectedFiatCurrency, useSwapEstimate } from './CommonUtils';

const { activeSwap: swap } = useSwapsStore();
const { exchangeRates } = useFiatStore();
const { activeCurrency } = useAccountStore();

const { estimate } = useSwapEstimate();
/**
* Buy - Buy crypto related things
* - Computeds
Expand Down
10 changes: 9 additions & 1 deletion src/lib/swap/utils/CommonUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AssetList, Estimate, getAssets, RequestAsset, SwapAsset } from '@nimiq/fastspot-api';
import { CurrencyInfo } from '@nimiq/utils';
import { computed, ref } from '@vue/composition-api';
import { computed, onUnmounted, ref, getCurrentInstance } from '@vue/composition-api';
import { useSwapLimits } from '../../../composables/useSwapLimits';
import { useAccountStore } from '../../../stores/Account';
import { useAddressStore } from '../../../stores/Address';
Expand Down Expand Up @@ -277,3 +277,11 @@ export function getFiatSwapParameters({ from, to } : {

return { from: null, to: null };
}

export function useSwapEstimate() {
if (getCurrentInstance()) {
onUnmounted(() => estimate.value = null);
}

return { estimate };
}
3 changes: 2 additions & 1 deletion src/lib/swap/utils/SellUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import {
import { estimateFees } from '../../BitcoinTransactionUtils';
import { useBtcAddressStore } from '../../../stores/BtcAddress';
import { i18n } from '../../../i18n/i18n-setup';
import { assets, calculateFees, estimate, getFiatSwapParameters, selectedFiatCurrency } from './CommonUtils';
import { assets, calculateFees, getFiatSwapParameters, selectedFiatCurrency, useSwapEstimate } from './CommonUtils';

const { activeSwap: swap } = useSwapsStore();
const { exchangeRates } = useFiatStore();
const { activeCurrency } = useAccountStore();
const { accountBalance: accountBtcBalance, accountUtxos } = useBtcAddressStore();
const { estimate } = useSwapEstimate();

/**
* Sell - Sell crypto related things
Expand Down

0 comments on commit b8636c2

Please sign in to comment.