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

Fix: Change offline pattern for unassigning cards #52109

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
2 changes: 1 addition & 1 deletion src/libs/API/parameters/UnassignCompanyCard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type UnassignCompanyCard = {
authToken?: string | null;
cardID: string;
cardID: number;
};

export default UnassignCompanyCard;
2 changes: 1 addition & 1 deletion src/libs/API/parameters/UpdateCompanyCard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type UpdateCompanyCard = {
authToken?: string | null;
cardID: string;
cardID: number;
};

export default UpdateCompanyCard;
38 changes: 35 additions & 3 deletions src/libs/actions/CompanyCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Card} from '@src/types/onyx';
import type {AssignCard, AssignCardData} from '@src/types/onyx/AssignCard';
import type {AddNewCardFeedData, AddNewCardFeedStep, CompanyCardFeed} from '@src/types/onyx/CardFeeds';
import type {OnyxData} from '@src/types/onyx/Request';
Expand Down Expand Up @@ -232,8 +233,9 @@ function assignWorkspaceCompanyCard(policyID: string, data?: Partial<AssignCardD
API.write(WRITE_COMMANDS.ASSIGN_COMPANY_CARD, parameters, onyxData);
}

function unassignWorkspaceCompanyCard(workspaceAccountID: number, cardID: string, bankName: string) {
function unassignWorkspaceCompanyCard(workspaceAccountID: number, bankName: string, card?: Card) {
const authToken = NetworkStore.getAuthToken();
const cardID = card?.cardID ?? '-1';

const onyxData: OnyxData = {
optimisticData: [
Expand All @@ -244,12 +246,42 @@ function unassignWorkspaceCompanyCard(workspaceAccountID: number, cardID: string
[cardID]: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CARD_LIST,
value: {
[cardID]: null,
},
},
],

failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${bankName}`,
value: {
[cardID]: {
...card,
errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'),
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CARD_LIST,
value: {
[cardID]: {
...card,
errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'),
},
},
},
],
};

const parameters = {
authToken,
cardID,
cardID: Number(cardID),
};

API.write(WRITE_COMMANDS.UNASSIGN_COMPANY_CARD, parameters, onyxData);
Expand Down Expand Up @@ -378,7 +410,7 @@ function updateWorkspaceCompanyCard(workspaceAccountID: number, cardID: string,

const parameters = {
authToken,
cardID,
cardID: Number(cardID),
};

API.write(WRITE_COMMANDS.UPDATE_COMPANY_CARD, parameters, {optimisticData, finallyData, failureData});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import OfflineWithFeedback from '@components/OfflineWithFeedback';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import usePolicy from '@hooks/usePolicy';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -48,6 +49,7 @@ function WorkspaceCompanyCardDetailsPage({route}: WorkspaceCompanyCardDetailsPag
const {translate} = useLocalize();
const styles = useThemeStyles();
const theme = useTheme();
const {isOffline} = useNetwork();
const accountingIntegrations = Object.values(CONST.POLICY.CONNECTIONS.NAME);
const connectedIntegration = getConnectedIntegration(policy, accountingIntegrations) ?? connectionSyncProgress?.connectionName;

Expand All @@ -62,7 +64,7 @@ function WorkspaceCompanyCardDetailsPage({route}: WorkspaceCompanyCardDetailsPag

const unassignCard = () => {
setIsUnassignModalVisible(false);
CompanyCards.unassignWorkspaceCompanyCard(workspaceAccountID, cardID, bank);
CompanyCards.unassignWorkspaceCompanyCard(workspaceAccountID, bank, card);
Navigation.goBack();
};

Expand Down Expand Up @@ -168,6 +170,7 @@ function WorkspaceCompanyCardDetailsPage({route}: WorkspaceCompanyCardDetailsPag
>
<MenuItem
icon={Expensicons.Sync}
disabled={isOffline || card?.isLoadingLastUpdated}
iconFill={theme.success}
title={translate('workspace.moreFeatures.companyCards.updateCard')}
style={styles.mv1}
Expand Down
Loading