Fix(installments): preselect value on brand change #2117
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Explanation for the root cause:
brand
iscard
, as we are setting the initialinstallmentAmount
value toinstallmentOptions?.preselectedValue || installmentOptions?.values[0]
in theuseState
hook, it sets the initialinstallmentAmount
value toundefined
.useEffect
is called,newAmount
is stillundefined
useState
is stateful, it would NOT get called, hence theinstallmentAmount
staysundefined
at this stage.useEffect
would be called because thebrand
changed. As the currentinstallmentAmount
isundefined
, theinstallmentOptions?.values?.includes(installmentAmount)
isfalse
.newAmount
would set toinstallmentOptions?.values[0]
, which is1
.preselectedValue
is not working as it was not used in theuseEffect
hook.Fix:
useEffect
toso that the
preselectedValue
is picked up.installmentOptions?.values?.includes(installmentAmount)
, we skip setting up the new value forinstallmentAmount
Tested scenarios
CARD
section2x $129.50
for the installments is selected in theInstallments payment
field.visa
card, the preselected value would depend on if the visa installment configuration supports that value. If so, theInstallments payment
field would remain unchanged. Otherwise, it would switch to the visainstallmentOptions
(either to thepreselectedValue
orvalues[0]
)