From 47dbb22024b46f92bda8b5301864a6a5f6e0e41b Mon Sep 17 00:00:00 2001 From: Daniel <80175477+dan437@users.noreply.github.com> Date: Wed, 29 Sep 2021 18:13:34 +0200 Subject: [PATCH] Fix form prefilling in Swaps on the Build Quote page (#12244) * Fix form prefilling in Swaps on the Build Quote page * Fix UTs * Clean up tokens when resetting swaps state, in case a user is changing a network --- app/scripts/controllers/swaps.js | 25 ++++++++++++++++++++++++- app/scripts/controllers/swaps.test.js | 1 + 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/scripts/controllers/swaps.js b/app/scripts/controllers/swaps.js index 150b4fece..5f3ec9851 100644 --- a/app/scripts/controllers/swaps.js +++ b/app/scripts/controllers/swaps.js @@ -79,6 +79,7 @@ const initialState = { routeState: '', swapsFeatureIsLive: true, useNewSwapsApi: false, + isFetchingQuotes: false, swapsQuoteRefreshTime: FALLBACK_QUOTE_REFRESH_TIME, swapsQuotePrefetchingRefreshTime: FALLBACK_QUOTE_REFRESH_TIME, }, @@ -229,6 +230,8 @@ export default class SwapsController { const indexOfCurrentCall = this.indexOfNewestCallInFlight + 1; this.indexOfNewestCallInFlight = indexOfCurrentCall; + this.setIsFetchingQuotes(true); + let [newQuotes] = await Promise.all([ this._fetchTradesInfo(fetchParams, { ...fetchParamsMetaData, @@ -237,6 +240,20 @@ export default class SwapsController { this._setSwapsRefreshRates(), ]); + const { + swapsState: { isFetchingQuotes }, + } = this.store.getState(); + + // If isFetchingQuotes is false, it means a user left Swaps (we cleaned the state) + // and we don't want to set any API response with quotes into state. + if (!isFetchingQuotes) { + return [ + {}, // quotes + null, // selectedAggId + ]; + } + this.setIsFetchingQuotes(false); + newQuotes = mapValues(newQuotes, (quote) => ({ ...quote, sourceTokenInfo: fetchParamsMetaData.sourceTokenInfo, @@ -540,6 +557,13 @@ export default class SwapsController { this.store.updateState({ swapsState: { ...swapsState, routeState } }); } + setIsFetchingQuotes(status) { + const { swapsState } = this.store.getState(); + this.store.updateState({ + swapsState: { ...swapsState, isFetchingQuotes: status }, + }); + } + setSwapsLiveness(swapsLiveness) { const { swapsState } = this.store.getState(); const { swapsFeatureIsLive, useNewSwapsApi } = swapsLiveness; @@ -570,7 +594,6 @@ export default class SwapsController { this.store.updateState({ swapsState: { ...initialState.swapsState, - tokens: swapsState.tokens, swapsQuoteRefreshTime: swapsState.swapsQuoteRefreshTime, swapsQuotePrefetchingRefreshTime: swapsState.swapsQuotePrefetchingRefreshTime, diff --git a/app/scripts/controllers/swaps.test.js b/app/scripts/controllers/swaps.test.js index d6127c6db..978c705a8 100644 --- a/app/scripts/controllers/swaps.test.js +++ b/app/scripts/controllers/swaps.test.js @@ -135,6 +135,7 @@ const EMPTY_INIT_STATE = { swapsQuoteRefreshTime: 60000, swapsQuotePrefetchingRefreshTime: 60000, swapsUserFeeLevel: '', + isFetchingQuotes: false, }, };