Skip to content

Commit

Permalink
Merge pull request #47394 from VickyStash/feature/update-card-name-BE…
Browse files Browse the repository at this point in the history
…-call

[No QA] Implement UpdateExpensifyCardTitle API call
  • Loading branch information
mountiny authored Aug 14, 2024
2 parents 55f67f5 + 24cd8d7 commit 3b48840
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2289,6 +2289,7 @@ const CONST = {
DAILY: 'daily',
MONTHLY: 'monthly',
},
CARD_TITLE_INPUT_LIMIT: 255,
},
AVATAR_ROW_SIZE: {
DEFAULT: 4,
Expand Down
7 changes: 7 additions & 0 deletions src/libs/API/parameters/UpdateExpensifyCardTitleParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type UpdateExpensifyCardTitleParams = {
authToken: string;
cardID: number;
cardTitle: string;
};

export default UpdateExpensifyCardTitleParams;
1 change: 1 addition & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,4 @@ export type {CreateWorkspaceApprovalParams, UpdateWorkspaceApprovalParams, Remov
export type {default as StartIssueNewCardFlowParams} from './StartIssueNewCardFlowParams';
export type {default as ConfigureExpensifyCardsForPolicyParams} from './ConfigureExpensifyCardsForPolicyParams';
export type {default as CreateExpensifyCardParams} from './CreateExpensifyCardParams';
export type {default as UpdateExpensifyCardTitleParams} from './UpdateExpensifyCardTitleParams';
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const WRITE_COMMANDS = {
REQUEST_REPLACEMENT_EXPENSIFY_CARD: 'RequestReplacementExpensifyCard',
ACTIVATE_PHYSICAL_EXPENSIFY_CARD: 'ActivatePhysicalExpensifyCard',
UPDATE_EXPENSIFY_CARD_LIMIT: 'UpdateExpensifyCardLimit',
UPDATE_EXPENSIFY_CARD_TITLE: 'UpdateExpensifyCardTitle',
CHRONOS_REMOVE_OOO_EVENT: 'Chronos_RemoveOOOEvent',
MAKE_DEFAULT_PAYMENT_METHOD: 'MakeDefaultPaymentMethod',
ADD_PAYMENT_CARD: 'AddPaymentCard',
Expand Down Expand Up @@ -354,6 +355,7 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.REQUEST_REPLACEMENT_EXPENSIFY_CARD]: Parameters.RequestReplacementExpensifyCardParams;
[WRITE_COMMANDS.ACTIVATE_PHYSICAL_EXPENSIFY_CARD]: Parameters.ActivatePhysicalExpensifyCardParams;
[WRITE_COMMANDS.UPDATE_EXPENSIFY_CARD_LIMIT]: Parameters.UpdateExpensifyCardLimitParams;
[WRITE_COMMANDS.UPDATE_EXPENSIFY_CARD_TITLE]: Parameters.UpdateExpensifyCardTitleParams;
[WRITE_COMMANDS.MAKE_DEFAULT_PAYMENT_METHOD]: Parameters.MakeDefaultPaymentMethodParams;
[WRITE_COMMANDS.ADD_PAYMENT_CARD]: Parameters.AddPaymentCardParams;
[WRITE_COMMANDS.ADD_PAYMENT_CARD_GBP]: Parameters.AddPaymentCardParams;
Expand Down
62 changes: 62 additions & 0 deletions src/libs/actions/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
RevealExpensifyCardDetailsParams,
StartIssueNewCardFlowParams,
UpdateExpensifyCardLimitParams,
UpdateExpensifyCardTitleParams,
} from '@libs/API/parameters';
import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as ErrorUtils from '@libs/ErrorUtils';
Expand Down Expand Up @@ -373,6 +374,66 @@ function updateExpensifyCardLimit(workspaceAccountID: number, cardID: number, ne
API.write(WRITE_COMMANDS.UPDATE_EXPENSIFY_CARD_LIMIT, parameters, {optimisticData, successData, failureData});
}

function updateExpensifyCardTitle(workspaceAccountID: number, cardID: number, newCardTitle: string, oldCardTitle?: string) {
const authToken = NetworkStore.getAuthToken();

if (!authToken) {
return;
}

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`,
value: {
[cardID]: {
nameValuePairs: {
cardTitle: newCardTitle,
},
isLoading: true,
errors: null,
},
},
},
];

const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`,
value: {
[cardID]: {
isLoading: false,
},
},
},
];

const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`,
value: {
[cardID]: {
nameValuePairs: {
cardTitle: oldCardTitle,
},
isLoading: false,
errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'),
},
},
},
];

const parameters: UpdateExpensifyCardTitleParams = {
authToken,
cardID,
cardTitle: newCardTitle,
};

API.write(WRITE_COMMANDS.UPDATE_EXPENSIFY_CARD_TITLE, parameters, {optimisticData, successData, failureData});
}

function startIssueNewCardFlow(policyID: string) {
const parameters: StartIssueNewCardFlowParams = {
policyID,
Expand Down Expand Up @@ -428,6 +489,7 @@ export {
setIssueNewCardStepAndData,
clearIssueNewCardFlow,
updateExpensifyCardLimit,
updateExpensifyCardTitle,
updateSettlementAccount,
startIssueNewCardFlow,
configureExpensifyCardsForPolicy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as ValidationUtils from '@libs/ValidationUtils';
import Navigation from '@navigation/Navigation';
import type {SettingsNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import * as Card from '@userActions/Card';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand All @@ -34,9 +35,8 @@ function WorkspaceEditCardNamePage({route}: WorkspaceEditCardNamePageProps) {
const [cardsList] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`);
const card = cardsList?.[cardID];

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const submit = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.EDIT_EXPENSIFY_CARD_NAME_FORM>) => {
// TODO: add API call when it's supported https://github.com/Expensify/Expensify/issues/407832
Card.updateExpensifyCardTitle(workspaceAccountID, Number(cardID), values[INPUT_IDS.NAME], card?.nameValuePairs?.cardTitle);
Navigation.goBack();
};

Expand Down Expand Up @@ -74,6 +74,7 @@ function WorkspaceEditCardNamePage({route}: WorkspaceEditCardNamePageProps) {
aria-label={translate('workspace.card.issueNewCard.cardName')}
role={CONST.ROLE.PRESENTATION}
defaultValue={card?.nameValuePairs?.cardTitle}
maxLength={CONST.EXPENSIFY_CARD.CARD_TITLE_INPUT_LIMIT}
ref={inputCallbackRef}
/>
</FormProvider>
Expand Down

0 comments on commit 3b48840

Please sign in to comment.