-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: APP-410 save buy flow on last user interaction #2574
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,9 @@ import { | |
spendingCapAtom, | ||
} from 'pages/BuyCredits/BuyCredits.atoms'; | ||
import { PAYMENT_OPTIONS } from 'pages/BuyCredits/BuyCredits.constants'; | ||
import { BuyCreditsSchemaTypes } from 'pages/BuyCredits/BuyCredits.types'; | ||
import { getCreditsAvailableBannerText } from 'pages/BuyCredits/BuyCredits.utils'; | ||
import { useMultiStep } from 'components/templates/MultiStepTemplate'; | ||
|
||
import { findDisplayDenom } from '../DenomLabel/DenomLabel.utils'; | ||
import { | ||
|
@@ -53,6 +55,11 @@ export const CreditsAmount = ({ | |
const setErrorBannerTextAtom = useSetAtom(errorBannerTextAtom); | ||
const [spendingCap, setSpendingCap] = useAtom(spendingCapAtom); | ||
const paymentOption = useAtomValue(paymentOptionAtom); | ||
const { | ||
data, | ||
handleSave: updateMultiStepData, | ||
activeStep, | ||
} = useMultiStep<BuyCreditsSchemaTypes>(); | ||
|
||
useEffect(() => { | ||
// Set initial credits amount to min(1, creditsAvailable) | ||
|
@@ -136,6 +143,16 @@ export const CreditsAmount = ({ | |
setErrorBannerTextAtom( | ||
getCreditsAvailableBannerText(_creditsAvailable), | ||
); | ||
// Udates the currency amount in multistep data when payment option is changed | ||
updateMultiStepData( | ||
{ | ||
...data, | ||
creditsAmount: _creditsAvailable, | ||
currencyAmount: _spendingCap, | ||
sellOrders: orderedSellOrders, | ||
}, | ||
activeStep, | ||
); | ||
} else { | ||
// Else we keep the same amount of credits | ||
// but we still need to update currency amount and sell orders | ||
|
@@ -148,8 +165,21 @@ export const CreditsAmount = ({ | |
}); | ||
setValue(CURRENCY_AMOUNT, currencyAmount); | ||
setValue(SELL_ORDERS, sellOrders); | ||
// Updates the currency amount in multistep data when payment option is changed | ||
updateMultiStepData( | ||
{ | ||
...data, | ||
creditsAmount: currentCreditsAmount, | ||
currencyAmount: currencyAmount, | ||
sellOrders: sellOrders, | ||
}, | ||
activeStep, | ||
); | ||
} | ||
} | ||
// Intentionally omit `updateMultiStepData` and `data` from the dependency array | ||
// because including them trigger unnecessary renders. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [ | ||
cardSellOrders, | ||
filteredCryptoSellOrders, | ||
|
@@ -163,6 +193,7 @@ export const CreditsAmount = ({ | |
card, | ||
setErrorBannerTextAtom, | ||
_, | ||
activeStep, | ||
]); | ||
|
||
// Max credits set | ||
|
@@ -172,26 +203,35 @@ export const CreditsAmount = ({ | |
setValue(CURRENCY_AMOUNT, spendingCap, { | ||
shouldValidate: true, | ||
}); | ||
setValue( | ||
SELL_ORDERS, | ||
orderedSellOrders.map(order => { | ||
const price = getSellOrderPrice({ order, card }); | ||
return formatSellOrder({ order, card, price }); | ||
}), | ||
const sellOrders = orderedSellOrders.map(order => { | ||
const price = getSellOrderPrice({ order, card }); | ||
return formatSellOrder({ order, card, price }); | ||
}); | ||
setValue(SELL_ORDERS, sellOrders); | ||
updateMultiStepData( | ||
{ | ||
...data, | ||
creditsAmount: creditsAvailable, | ||
currencyAmount: spendingCap, | ||
sellOrders: sellOrders, | ||
}, | ||
activeStep, | ||
); | ||
setMaxCreditsSelected(false); | ||
} | ||
}, [ | ||
activeStep, | ||
card, | ||
creditsAvailable, | ||
data, | ||
maxCreditsSelected, | ||
orderedSellOrders, | ||
paymentOption, | ||
setValue, | ||
spendingCap, | ||
updateMultiStepData, | ||
]); | ||
|
||
// Credits amount change | ||
const handleCreditsAmountChange = useCallback( | ||
(e: ChangeEvent<HTMLInputElement>) => { | ||
// Set currency amount according to credits quantity, | ||
|
@@ -205,11 +245,21 @@ export const CreditsAmount = ({ | |
}); | ||
setValue(CURRENCY_AMOUNT, currencyAmount, { shouldValidate: true }); | ||
setValue(SELL_ORDERS, sellOrders); | ||
updateMultiStepData( | ||
{ | ||
...data, | ||
creditsAmount: currentCreditsAmount, | ||
currencyAmount: currencyAmount, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not updating sellOrders too? |
||
}, | ||
activeStep, | ||
); | ||
}, | ||
[card, orderedSellOrders, setValue, creditTypePrecision], | ||
// Intentionally omit `updateMultiStepData` and `data` from the dependency array | ||
// because including them trigger unnecessary renders. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[card, orderedSellOrders, creditTypePrecision, setValue, activeStep], | ||
); | ||
|
||
// Currency amount change | ||
const handleCurrencyAmountChange = useCallback( | ||
(e: ChangeEvent<HTMLInputElement>) => { | ||
// Set credits quantity according to currency amount, | ||
|
@@ -221,10 +271,22 @@ export const CreditsAmount = ({ | |
orderedSellOrders, | ||
creditTypePrecision, | ||
}); | ||
|
||
updateMultiStepData( | ||
{ | ||
...data, | ||
creditsAmount: currentCreditsAmount, | ||
currencyAmount: isNaN(value) ? 0 : value, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
}, | ||
activeStep, | ||
); | ||
setValue(CREDITS_AMOUNT, currentCreditsAmount, { shouldValidate: true }); | ||
setValue(SELL_ORDERS, sellOrders); | ||
}, | ||
[card, orderedSellOrders, setValue, creditTypePrecision], | ||
// Intentionally omit `updateMultiStepData` and `data` from the dependency array | ||
// because including them trigger unnecessary renders. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[card, orderedSellOrders, creditTypePrecision, setValue], | ||
); | ||
|
||
const displayDenom = useMemo( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ import TextField from 'web-components/src/components/inputs/new/TextField/TextFi | |
import { paymentOptionAtom } from 'pages/BuyCredits/BuyCredits.atoms'; | ||
import { PAYMENT_OPTIONS } from 'pages/BuyCredits/BuyCredits.constants'; | ||
import { BuyCreditsSchemaTypes } from 'pages/BuyCredits/BuyCredits.types'; | ||
import { updateMultiStepCurrencyAndPaymentOption } from 'pages/BuyCredits/BuyCredits.utils'; | ||
import { DenomIconWithCurrency } from 'components/molecules/DenomIconWithCurrency/DenomIconWithCurrency'; | ||
import { useMultiStep } from 'components/templates/MultiStepTemplate'; | ||
|
||
|
@@ -43,8 +42,11 @@ export const CurrencyInput = ({ | |
control, | ||
} = useFormContext<ChooseCreditsFormSchemaType>(); | ||
const { _ } = useLingui(); | ||
const { data, handleSave, activeStep } = | ||
useMultiStep<BuyCreditsSchemaTypes>(); | ||
const { | ||
data, | ||
handleSave: updateMultiStepData, | ||
activeStep, | ||
} = useMultiStep<BuyCreditsSchemaTypes>(); | ||
const paymentOption = useAtomValue(paymentOptionAtom); | ||
const card = paymentOption === PAYMENT_OPTIONS.CARD; | ||
const { onChange } = register(CURRENCY_AMOUNT); | ||
|
@@ -97,9 +99,18 @@ export const CurrencyInput = ({ | |
const value = event.target.value; | ||
if (value === '') { | ||
setValue(CURRENCY_AMOUNT, 0, { shouldValidate: true }); | ||
updateMultiStepData( | ||
{ | ||
...data, | ||
currency, | ||
paymentOption, | ||
currencyAmount: 0, | ||
}, | ||
activeStep, | ||
); | ||
Comment on lines
+102
to
+110
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm updating the multistep data here to keep it in sync with the input field, don't you think this needs to be done? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure since it seems like this will be updated anyway from |
||
} | ||
}, | ||
[setValue], | ||
[activeStep, currency, data, paymentOption, setValue, updateMultiStepData], | ||
); | ||
|
||
const onHandleCurrencyChange = useCallback( | ||
|
@@ -114,15 +125,23 @@ export const CurrencyInput = ({ | |
)?.[0].askBaseDenom, | ||
}; | ||
setValue(CURRENCY, currency); | ||
updateMultiStepCurrencyAndPaymentOption( | ||
handleSave, | ||
data, | ||
currency, | ||
updateMultiStepData( | ||
{ | ||
...data, | ||
currency, | ||
paymentOption, | ||
}, | ||
activeStep, | ||
paymentOption, | ||
); | ||
}, | ||
[activeStep, cryptoCurrencies, data, handleSave, paymentOption, setValue], | ||
[ | ||
cryptoCurrencies, | ||
setValue, | ||
updateMultiStepData, | ||
data, | ||
paymentOption, | ||
activeStep, | ||
], | ||
); | ||
|
||
return ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why aren't we setting this to the same value as in
setValue(SELL_ORDERS, ...)
just above?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good you noticed this, thanks