Skip to content

Commit

Permalink
Merge pull request #50864 from nkdengineer/fix/50667
Browse files Browse the repository at this point in the history
fix: Add a step validate to the Issue New Card form
  • Loading branch information
tgolen authored Nov 13, 2024
2 parents 23d7aa5 + c17ad24 commit 0984113
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type ValidateCodeFormProps = {

/** Function is called when validate code modal is mounted and on magic code resend */
sendValidateCode: () => void;

/** Wheather the form is loading or not */
isLoading?: boolean;
};

function BaseValidateCodeForm({
Expand All @@ -79,6 +82,7 @@ function BaseValidateCodeForm({
clearError,
sendValidateCode,
buttonStyles,
isLoading,
}: ValidateCodeFormProps) {
const {translate} = useLocalize();
const {isOffline} = useNetwork();
Expand Down Expand Up @@ -267,7 +271,8 @@ function BaseValidateCodeForm({
style={[styles.mt4]}
success
large
isLoading={account?.isLoading}
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
isLoading={account?.isLoading || isLoading}
/>
</OfflineWithFeedback>
</>
Expand Down
2 changes: 2 additions & 0 deletions src/components/ValidateCodeActionModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function ValidateCodeActionModal({
footer,
sendValidateCode,
hasMagicCodeBeenSent,
isLoading,
}: ValidateCodeActionModalProps) {
const themeStyles = useThemeStyles();
const safePaddingBottomStyle = useSafePaddingBottomStyle();
Expand Down Expand Up @@ -81,6 +82,7 @@ function ValidateCodeActionModal({
buttonStyles={[themeStyles.justifyContentEnd, themeStyles.flex1, safePaddingBottomStyle]}
ref={validateCodeFormRef}
hasMagicCodeBeenSent={hasMagicCodeBeenSent}
isLoading={isLoading}
/>
</View>
{footer?.()}
Expand Down
3 changes: 3 additions & 0 deletions src/components/ValidateCodeActionModal/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type ValidateCodeActionModalProps = {

/** If the magic code has been resent previously */
hasMagicCodeBeenSent?: boolean;

/** Wheather the form is loading or not */
isLoading?: boolean;
};

// eslint-disable-next-line import/prefer-default-export
Expand Down
8 changes: 7 additions & 1 deletion src/libs/actions/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ function clearIssueNewCardFlow() {
});
}

function clearIssueNewCardError(issueNewCard: IssueNewCardFlowData) {
Onyx.set(ONYXKEYS.ISSUE_NEW_EXPENSIFY_CARD, {...issueNewCard, errors: null});
}

function updateExpensifyCardLimit(workspaceAccountID: number, cardID: number, newLimit: number, newAvailableSpend: number, oldLimit?: number, oldAvailableSpend?: number) {
const authToken = NetworkStore.getAuthToken();

Expand Down Expand Up @@ -721,7 +725,7 @@ function configureExpensifyCardsForPolicy(policyID: string, bankAccountID?: numb
});
}

function issueExpensifyCard(policyID: string, feedCountry: string, data?: IssueNewCardData) {
function issueExpensifyCard(policyID: string, feedCountry: string, validateCode: string, data?: IssueNewCardData) {
if (!data) {
return;
}
Expand Down Expand Up @@ -768,6 +772,7 @@ function issueExpensifyCard(policyID: string, feedCountry: string, data?: IssueN
limit,
limitType,
cardTitle,
validateCode,
};

if (cardType === CONST.EXPENSIFY_CARD.CARD_TYPE.PHYSICAL) {
Expand Down Expand Up @@ -884,6 +889,7 @@ export {
requestReplacementExpensifyCard,
activatePhysicalExpensifyCard,
clearCardListErrors,
clearIssueNewCardError,
reportVirtualExpensifyCardFraud,
revealVirtualCardDetails,
updateSettlementFrequency,
Expand Down
38 changes: 32 additions & 6 deletions src/pages/workspace/expensifyCard/issueNew/ConfirmationStep.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, {useEffect, useRef} from 'react';
import React, {useEffect, useRef, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import FormAlertWithSubmitButton from '@components/FormAlertWithSubmitButton';
import InteractiveStepWrapper from '@components/InteractiveStepWrapper';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import ScrollView from '@components/ScrollView';
import Text from '@components/Text';
import ValidateCodeActionModal from '@components/ValidateCodeActionModal';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
Expand All @@ -15,6 +16,7 @@ import * as ErrorUtils from '@libs/ErrorUtils';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import Navigation from '@navigation/Navigation';
import * as Card from '@userActions/Card';
import * as User from '@userActions/User';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand All @@ -33,11 +35,14 @@ function ConfirmationStep({policyID, backTo}: ConfirmationStepProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();
const {isOffline} = useNetwork();

const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [issueNewCard] = useOnyx(ONYXKEYS.ISSUE_NEW_EXPENSIFY_CARD);

const [validateCodeAction] = useOnyx(ONYXKEYS.VALIDATE_ACTION_CODE);
const validateError = ErrorUtils.getLatestErrorMessageField(issueNewCard);
const [isValidateCodeActionModalVisible, setIsValidateCodeActionModalVisible] = useState(false);
const data = issueNewCard?.data;
const isSuccessful = issueNewCard?.isSuccessful;
const validateCodeSent = validateCodeAction?.validateCodeSent;

const submitButton = useRef<View>(null);

Expand All @@ -53,8 +58,8 @@ function ConfirmationStep({policyID, backTo}: ConfirmationStepProps) {
Card.clearIssueNewCardFlow();
}, [backTo, policyID, isSuccessful]);

const submit = () => {
Card.issueExpensifyCard(policyID, CONST.COUNTRY.US, data);
const submit = (validateCode: string) => {
Card.issueExpensifyCard(policyID, CONST.COUNTRY.US, validateCode, data);
};

const errorMessage = ErrorUtils.getLatestErrorMessage(issueNewCard);
Expand Down Expand Up @@ -122,11 +127,32 @@ function ConfirmationStep({policyID, backTo}: ConfirmationStepProps) {
isAlertVisible={!!errorMessage}
isDisabled={isOffline}
isLoading={issueNewCard?.isLoading}
onSubmit={submit}
onSubmit={() => setIsValidateCodeActionModalVisible(true)}
buttonText={translate('workspace.card.issueCard')}
/>
</View>
</ScrollView>
{!!issueNewCard && (
<ValidateCodeActionModal
handleSubmitForm={submit}
isLoading={issueNewCard?.isLoading}
sendValidateCode={() => User.requestValidateCodeAction()}
validateError={validateError}
hasMagicCodeBeenSent={validateCodeSent}
clearError={() => {
Card.clearIssueNewCardError(issueNewCard);
}}
onClose={() => {
if (validateError) {
Card.clearIssueNewCardError(issueNewCard);
}
setIsValidateCodeActionModalVisible(false);
}}
isVisible={isValidateCodeActionModalVisible}
title={translate('cardPage.validateCardTitle')}
description={translate('cardPage.enterMagicCode', {contactMethod: account?.primaryLogin ?? ''})}
/>
)}
</InteractiveStepWrapper>
);
}
Expand Down

0 comments on commit 0984113

Please sign in to comment.