Skip to content
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

Remove cards optimistically #51841

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion src/libs/actions/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ type IssueNewCardFlowData = {
data?: Partial<IssueNewCardData>;
};

function reportVirtualExpensifyCardFraud(cardID: number) {
function reportVirtualExpensifyCardFraud(card?: Card) {
const cardID = card?.cardID ?? -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I m still not sure what is the best way to handle these. I feel like that if the cardID is not available we should cut this earlier, but not sure about the best practice here

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -46,6 +47,20 @@ function reportVirtualExpensifyCardFraud(cardID: number) {
isLoading: true,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CARD_LIST,
value: {
[cardID]: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${card?.fundID ?? '-1'}_${CONST.EXPENSIFY_CARD.BANK}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, if the fundID is not available, this is kinda useless

value: {
[cardID]: null,
},
},
];

const successData: OnyxUpdate[] = [
Expand All @@ -66,6 +81,26 @@ function reportVirtualExpensifyCardFraud(cardID: number) {
isLoading: false,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CARD_LIST,
value: {
[cardID]: {
...card,
errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'),
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${card?.fundID ?? '-1'}_${CONST.EXPENSIFY_CARD.BANK}`,
value: {
[cardID]: {
...card,
errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'),
},
},
},
];

const parameters: ReportVirtualExpensifyCardFraudParams = {
Expand Down Expand Up @@ -582,6 +617,13 @@ function deactivateCard(workspaceAccountID: number, card?: Card) {
[cardID]: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CARD_LIST,
value: {
[cardID]: null,
},
},
];

const failureData: OnyxUpdate[] = [
Expand All @@ -595,6 +637,16 @@ function deactivateCard(workspaceAccountID: number, card?: Card) {
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CARD_LIST,
value: {
[cardID]: {
...card,
errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'),
},
},
},
];

const parameters: CardDeactivateParams = {
Expand Down
13 changes: 10 additions & 3 deletions src/pages/settings/Wallet/ReportVirtualCardFraudPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useEffect} from 'react';
import {View} from 'react-native';
import React, {useCallback, useEffect} from 'react';
import {InteractionManager, View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import FormAlertWithSubmitButton from '@components/FormAlertWithSubmitButton';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
Expand Down Expand Up @@ -36,6 +36,13 @@ function ReportVirtualCardFraudPage({

const prevIsLoading = usePrevious(formData?.isLoading);

const submit = useCallback(() => {
Navigation.dismissModal();
InteractionManager.runAfterInteractions(() => {
Card.reportVirtualExpensifyCardFraud(virtualCard);
});
}, [virtualCard]);
Comment on lines +39 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we need this callback here and not in some other pages?


useEffect(() => {
if (!prevIsLoading || formData?.isLoading) {
return;
Expand All @@ -61,7 +68,7 @@ function ReportVirtualCardFraudPage({
<Text style={[styles.webViewStyles.baseFontStyle, styles.mh5]}>{translate('reportFraudPage.description')}</Text>
<FormAlertWithSubmitButton
isAlertVisible={!!virtualCardError}
onSubmit={() => Card.reportVirtualExpensifyCardFraud(virtualCard.cardID)}
onSubmit={submit}
message={virtualCardError}
isLoading={formData?.isLoading}
buttonText={translate('reportFraudPage.deactivateCard')}
Expand Down
Loading