-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Internal QA] Migrate ReportLostCardPage to Navigation-Based Validation #76280
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
Changes from all commits
91b5b21
4d87d29
6f151a7
cbe9fff
747f3fa
0d6980b
cbc3175
75842d4
28ad240
675b97f
e912507
65f641a
75a19d7
8d22f89
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 |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import {deepEqual} from 'fast-equals'; | ||
| import React, {useCallback, useEffect, useState} from 'react'; | ||
| import ScreenWrapper from '@components/ScreenWrapper'; | ||
| import ValidateCodeActionContent from '@components/ValidateCodeActionModal/ValidateCodeActionContent'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import usePrevious from '@hooks/usePrevious'; | ||
| import {clearCardListErrors, requestReplacementExpensifyCard} from '@libs/actions/Card'; | ||
| import {setErrors} from '@libs/actions/FormActions'; | ||
| import {requestValidateCodeAction, resetValidateActionCodeSent} from '@libs/actions/User'; | ||
| import {getLatestErrorMessageField} from '@libs/ErrorUtils'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types'; | ||
| import type {SettingsNavigatorParamList} from '@libs/Navigation/types'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import ROUTES from '@src/ROUTES'; | ||
| import type SCREENS from '@src/SCREENS'; | ||
| import SuccessReportCardLost from './SuccessReportCardLost'; | ||
|
|
||
| type ReportCardLostConfirmMagicCodePageProps = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.REPORT_CARD_LOST_OR_DAMAGED_CONFIRM_MAGIC_CODE>; | ||
|
|
||
| function ReportCardLostConfirmMagicCodePage({ | ||
| route: { | ||
| params: {cardID = '', reason = 'damaged'}, | ||
| }, | ||
| }: ReportCardLostConfirmMagicCodePageProps) { | ||
| const {translate} = useLocalize(); | ||
| const [account] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: false}); | ||
| const [formData] = useOnyx(ONYXKEYS.FORMS.REPORT_PHYSICAL_CARD_FORM, {canBeMissing: true}); | ||
|
|
||
| const primaryLogin = account?.primaryLogin ?? ''; | ||
| const [cardList] = useOnyx(ONYXKEYS.CARD_LIST, {canBeMissing: true}); | ||
| const physicalCard = cardList?.[cardID]; | ||
| const [newCardID, setNewCardID] = useState<string>(''); | ||
| const previousCardList = usePrevious(cardList); | ||
| const validateError = getLatestErrorMessageField(physicalCard); | ||
|
|
||
| useEffect(() => { | ||
| const newID = Object.keys(cardList ?? {}).find((cardKey) => cardList?.[cardKey]?.cardID && !(cardKey in (previousCardList ?? {}))); | ||
| if (!newID || physicalCard?.cardID) { | ||
| return; | ||
| } | ||
| setNewCardID(newID); | ||
| }, [cardList, physicalCard?.cardID, previousCardList]); | ||
|
|
||
| useEffect(() => { | ||
| if (formData?.isLoading) { | ||
| return; | ||
| } | ||
|
|
||
| const newErrors = physicalCard?.errors ?? {}; | ||
| // Only update if errors have actually changed to prevent additional rerender | ||
| if (deepEqual(newErrors, formData?.errors ?? {})) { | ||
|
Contributor
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. The reload didn't happen before without using
Contributor
Author
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. As far as I could reproduce it updating But looking @ your comment below it may be that I just get API response that code has beed issued fast enough that I cannot get into the state before. |
||
| return; | ||
| } | ||
| setErrors(ONYXKEYS.FORMS.REPORT_PHYSICAL_CARD_FORM, newErrors); | ||
| }, [formData?.isLoading, formData?.errors, physicalCard?.errors]); | ||
|
|
||
| const handleValidateCodeEntered = useCallback( | ||
| (validateCode: string) => { | ||
| if (!physicalCard) { | ||
| return; | ||
| } | ||
| requestReplacementExpensifyCard(physicalCard.cardID, reason, validateCode); | ||
| }, | ||
| [physicalCard, reason], | ||
| ); | ||
|
|
||
| if (newCardID) { | ||
| return ( | ||
| <ScreenWrapper | ||
| includeSafeAreaPaddingBottom | ||
| testID={ReportCardLostConfirmMagicCodePage.displayName} | ||
| > | ||
| <SuccessReportCardLost cardID={newCardID} /> | ||
| </ScreenWrapper> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <ValidateCodeActionContent | ||
| validateCodeActionErrorField="replaceLostCard" | ||
| handleSubmitForm={handleValidateCodeEntered} | ||
| isLoading={formData?.isLoading} | ||
| title={translate('cardPage.validateCardTitle')} | ||
| descriptionPrimary={translate('cardPage.enterMagicCode', {contactMethod: primaryLogin})} | ||
| sendValidateCode={() => requestValidateCodeAction()} | ||
| validateError={validateError} | ||
| clearError={() => { | ||
| if (!physicalCard?.cardID) { | ||
| return; | ||
| } | ||
| clearCardListErrors(physicalCard?.cardID); | ||
| }} | ||
| onClose={() => { | ||
| resetValidateActionCodeSent(); | ||
| Navigation.goBack(ROUTES.SETTINGS_WALLET_REPORT_CARD_LOST_OR_DAMAGED.getRoute(cardID)); | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| ReportCardLostConfirmMagicCodePage.displayName = 'ReportCardLostConfirmMagicCodePage'; | ||
|
|
||
| export default ReportCardLostConfirmMagicCodePage; | ||
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.
Where do these default params come from?
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.
cardID = ''is widely used in all other pages,reason = 'damaged'there are 2 of them, just picked one