1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00

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
This commit is contained in:
Daniel 2021-09-29 18:13:34 +02:00 committed by GitHub
parent 53f2c84209
commit acce73c943
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -79,6 +79,7 @@ const initialState = {
routeState: '', routeState: '',
swapsFeatureIsLive: true, swapsFeatureIsLive: true,
useNewSwapsApi: false, useNewSwapsApi: false,
isFetchingQuotes: false,
swapsQuoteRefreshTime: FALLBACK_QUOTE_REFRESH_TIME, swapsQuoteRefreshTime: FALLBACK_QUOTE_REFRESH_TIME,
swapsQuotePrefetchingRefreshTime: FALLBACK_QUOTE_REFRESH_TIME, swapsQuotePrefetchingRefreshTime: FALLBACK_QUOTE_REFRESH_TIME,
}, },
@ -229,6 +230,8 @@ export default class SwapsController {
const indexOfCurrentCall = this.indexOfNewestCallInFlight + 1; const indexOfCurrentCall = this.indexOfNewestCallInFlight + 1;
this.indexOfNewestCallInFlight = indexOfCurrentCall; this.indexOfNewestCallInFlight = indexOfCurrentCall;
this.setIsFetchingQuotes(true);
let [newQuotes] = await Promise.all([ let [newQuotes] = await Promise.all([
this._fetchTradesInfo(fetchParams, { this._fetchTradesInfo(fetchParams, {
...fetchParamsMetaData, ...fetchParamsMetaData,
@ -237,6 +240,20 @@ export default class SwapsController {
this._setSwapsRefreshRates(), 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) => ({ newQuotes = mapValues(newQuotes, (quote) => ({
...quote, ...quote,
sourceTokenInfo: fetchParamsMetaData.sourceTokenInfo, sourceTokenInfo: fetchParamsMetaData.sourceTokenInfo,
@ -540,6 +557,13 @@ export default class SwapsController {
this.store.updateState({ swapsState: { ...swapsState, routeState } }); this.store.updateState({ swapsState: { ...swapsState, routeState } });
} }
setIsFetchingQuotes(status) {
const { swapsState } = this.store.getState();
this.store.updateState({
swapsState: { ...swapsState, isFetchingQuotes: status },
});
}
setSwapsLiveness(swapsLiveness) { setSwapsLiveness(swapsLiveness) {
const { swapsState } = this.store.getState(); const { swapsState } = this.store.getState();
const { swapsFeatureIsLive, useNewSwapsApi } = swapsLiveness; const { swapsFeatureIsLive, useNewSwapsApi } = swapsLiveness;
@ -570,7 +594,6 @@ export default class SwapsController {
this.store.updateState({ this.store.updateState({
swapsState: { swapsState: {
...initialState.swapsState, ...initialState.swapsState,
tokens: swapsState.tokens,
swapsQuoteRefreshTime: swapsState.swapsQuoteRefreshTime, swapsQuoteRefreshTime: swapsState.swapsQuoteRefreshTime,
swapsQuotePrefetchingRefreshTime: swapsQuotePrefetchingRefreshTime:
swapsState.swapsQuotePrefetchingRefreshTime, swapsState.swapsQuotePrefetchingRefreshTime,

View File

@ -135,6 +135,7 @@ const EMPTY_INIT_STATE = {
swapsQuoteRefreshTime: 60000, swapsQuoteRefreshTime: 60000,
swapsQuotePrefetchingRefreshTime: 60000, swapsQuotePrefetchingRefreshTime: 60000,
swapsUserFeeLevel: '', swapsUserFeeLevel: '',
isFetchingQuotes: false,
}, },
}; };