1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +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 ryanml
parent 7810cb3cdf
commit 47dbb22024
2 changed files with 25 additions and 1 deletions

View File

@ -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,

View File

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