Skip to content

Commit

Permalink
Merge pull request #38386 from Expensify/alberto-qabOpti
Browse files Browse the repository at this point in the history
Set Quick Action NVP optimistically
  • Loading branch information
cristipaval authored Mar 19, 2024
2 parents 6b7f01f + 1341932 commit 7014d47
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,21 @@ const CONST = {
REPORT: 'report',
PERSONAL_DETAIL: 'personalDetail',
},

QUICK_ACTIONS: {
REQUEST_MANUAL: 'requestManual',
REQUEST_SCAN: 'requestScan',
REQUEST_DISTANCE: 'requestDistance',
SPLIT_MANUAL: 'splitManual',
SPLIT_SCAN: 'splitScan',
SPLIT_DISTANCE: 'splitDistance',
TRACK_MANUAL: 'trackManual',
TRACK_SCAN: 'trackScan',
TRACK_DISTANCE: 'trackDistance',
ASSIGN_TASK: 'assignTask',
SEND_MONEY: 'sendMoney',
},

RECEIPT: {
ICON_SIZE: 164,
PERMISSION_GRANTED: 'granted',
Expand Down
4 changes: 4 additions & 0 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ const ONYXKEYS = {
/** The NVP with the last distance rate used per policy */
NVP_LAST_SELECTED_DISTANCE_RATES: 'lastSelectedDistanceRates',

/** The NVP with the last action taken (for the Quick Action Button) */
NVP_QUICK_ACTION_GLOBAL_CREATE: 'nvp_quickActionGlobalCreate',

/** Does this user have push notifications enabled for this device? */
PUSH_NOTIFICATIONS_ENABLED: 'pushNotificationsEnabled',

Expand Down Expand Up @@ -599,6 +602,7 @@ type OnyxValuesMapping = {
[ONYXKEYS.LOGS]: Record<number, OnyxTypes.Log>;
[ONYXKEYS.SHOULD_STORE_LOGS]: boolean;
[ONYXKEYS.CACHED_PDF_PATHS]: Record<string, string>;
[ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE]: OnyxTypes.QuickAction;
};

type OnyxValues = OnyxValuesMapping & OnyxCollectionValuesMapping & OnyxFormValuesMapping & OnyxFormDraftValuesMapping;
Expand Down
83 changes: 82 additions & 1 deletion src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ Onyx.connect({
},
});

let quickAction: OnyxEntry<OnyxTypes.QuickAction> = {};
Onyx.connect({
key: ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE,
callback: (value) => {
quickAction = value;
},
});

/**
* Initialize money request info
* @param reportID to attach the transaction to
Expand Down Expand Up @@ -457,11 +465,16 @@ function buildOnyxDataForMoneyRequest(
policyTagList?: OnyxEntry<OnyxTypes.PolicyTagList>,
policyCategories?: OnyxEntry<OnyxTypes.PolicyCategories>,
optimisticNextStep?: OnyxTypes.ReportNextStep | null,
isOneOnOneSplit = false,
): [OnyxUpdate[], OnyxUpdate[], OnyxUpdate[]] {
const isScanRequest = TransactionUtils.isScanRequest(transaction);
const outstandingChildRequest = getOutstandingChildRequest(policy ?? {}, iouReport);
const clearedPendingFields = Object.fromEntries(Object.keys(transaction.pendingFields ?? {}).map((key) => [key, null]));
const optimisticData: OnyxUpdate[] = [];
let newQuickAction: ValueOf<typeof CONST.QUICK_ACTIONS> = isScanRequest ? CONST.QUICK_ACTIONS.REQUEST_SCAN : CONST.QUICK_ACTIONS.REQUEST_MANUAL;
if (TransactionUtils.isDistanceRequest(transaction)) {
newQuickAction = CONST.QUICK_ACTIONS.REQUEST_DISTANCE;
}

if (chatReport) {
optimisticData.push({
Expand Down Expand Up @@ -551,6 +564,18 @@ function buildOnyxDataForMoneyRequest(
},
);

if (!isOneOnOneSplit) {
optimisticData.push({
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE,
value: {
action: newQuickAction,
reportID: chatReport?.reportID,
isFirstQuickAction: isEmptyObject(quickAction),
},
});
}

if (optimisticPolicyRecentlyUsedCategories.length) {
optimisticData.push({
onyxMethod: Onyx.METHOD.SET,
Expand Down Expand Up @@ -1649,6 +1674,7 @@ function createSplitsAndOnyxData(
tag: string,
existingSplitChatReportID = '',
billable = false,
iouRequestType: IOURequestType = CONST.IOU.REQUEST_TYPE.MANUAL,
): SplitsAndOnyxData {
const currentUserEmailForIOUSplit = PhoneNumber.addSMSDomainIfPhoneNumber(currentUserLogin);
const participantAccountIDs = participants.map((participant) => Number(participant.accountID));
Expand Down Expand Up @@ -1712,6 +1738,15 @@ function createSplitsAndOnyxData(
key: `${ONYXKEYS.COLLECTION.REPORT}${splitChatReport.reportID}`,
value: splitChatReport,
},
{
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE,
value: {
action: iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE ? CONST.QUICK_ACTIONS.SPLIT_DISTANCE : CONST.QUICK_ACTIONS.SPLIT_MANUAL,
reportID: splitChatReport.reportID,
isFirstQuickAction: isEmptyObject(quickAction),
},
},
existingSplitChatReport
? {
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -1943,6 +1978,11 @@ function createSplitsAndOnyxData(
optimisticTransactionThread,
optimisticCreatedActionForTransactionThread,
shouldCreateNewOneOnOneIOUReport,
null,
null,
null,
null,
true,
);

const individualSplit = {
Expand Down Expand Up @@ -2001,6 +2041,7 @@ function splitBill(
tag: string,
existingSplitChatReportID = '',
billable = false,
iouRequestType: IOURequestType = CONST.IOU.REQUEST_TYPE.MANUAL,
) {
const currentCreated = DateUtils.enrichMoneyRequestTimestamp(created);
const {splitData, splits, onyxData} = createSplitsAndOnyxData(
Expand All @@ -2016,6 +2057,7 @@ function splitBill(
tag,
existingSplitChatReportID,
billable,
iouRequestType,
);

const parameters: SplitBillParams = {
Expand Down Expand Up @@ -2057,9 +2099,24 @@ function splitBillAndOpenReport(
category: string,
tag: string,
billable: boolean,
iouRequestType: IOURequestType = CONST.IOU.REQUEST_TYPE.MANUAL,
) {
const currentCreated = DateUtils.enrichMoneyRequestTimestamp(created);
const {splitData, splits, onyxData} = createSplitsAndOnyxData(participants, currentUserLogin, currentUserAccountID, amount, comment, currency, merchant, currentCreated, category, tag);
const {splitData, splits, onyxData} = createSplitsAndOnyxData(
participants,
currentUserLogin,
currentUserAccountID,
amount,
comment,
currency,
merchant,
currentCreated,
category,
tag,
'',
billable,
iouRequestType,
);

const parameters: SplitBillParams = {
reportID: splitData.chatReportID,
Expand Down Expand Up @@ -2167,6 +2224,15 @@ function startSplitBill(
key: `${ONYXKEYS.COLLECTION.REPORT}${splitChatReport.reportID}`,
value: splitChatReport,
},
{
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE,
value: {
action: CONST.QUICK_ACTIONS.SPLIT_SCAN,
reportID: splitChatReport.reportID,
isFirstQuickAction: isEmptyObject(quickAction),
},
},
existingSplitChatReport
? {
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -2542,6 +2608,11 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA
optimisticTransactionThread,
optimisticCreatedActionForTransactionThread,
shouldCreateNewOneOnOneIOUReport,
null,
null,
null,
null,
true,
);

splits.push({
Expand Down Expand Up @@ -3274,6 +3345,15 @@ function getSendMoneyParams(
lastVisibleActionCreated: reportPreviewAction.created,
},
};
const optimisticQuickActionData: OnyxUpdate = {
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE,
value: {
action: CONST.QUICK_ACTIONS.SEND_MONEY,
reportID: chatReport.reportID,
isFirstQuickAction: isEmptyObject(quickAction),
},
};
const optimisticIOUReportData: OnyxUpdate = {
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticIOUReport.reportID}`,
Expand Down Expand Up @@ -3440,6 +3520,7 @@ function getSendMoneyParams(

const optimisticData: OnyxUpdate[] = [
optimisticChatReportData,
optimisticQuickActionData,
optimisticIOUReportData,
optimisticChatReportActionsData,
optimisticIOUReportActionsData,
Expand Down
20 changes: 20 additions & 0 deletions src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ Onyx.connect({
callback: (value) => (allPersonalDetails = value),
});

let quickAction: OnyxEntry<OnyxTypes.QuickAction> = {};
Onyx.connect({
key: ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE,
callback: (value) => {
quickAction = value;
},
});

const allReportActions: OnyxCollection<ReportActions> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
Expand Down Expand Up @@ -227,6 +235,18 @@ function createTaskAndNavigate(
},
);

// FOR QUICK ACTION NVP
optimisticData.push({
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE,
value: {
action: CONST.QUICK_ACTIONS.ASSIGN_TASK,
reportID: parentReportID,
isFirstQuickAction: isEmptyObject(quickAction),
targetAccountID: assigneeAccountID,
},
});

// If needed, update optimistic data for parent report action of the parent report.
const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(parentReportID, currentTime, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
if (!isEmptyObject(optimisticParentReportData)) {
Expand Down
2 changes: 2 additions & 0 deletions src/pages/iou/request/step/IOURequestStepConfirmation.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ function IOURequestStepConfirmation({
transaction.tag,
report.reportID,
transaction.billable,
transaction.iouRequestType,
);
return;
}
Expand All @@ -313,6 +314,7 @@ function IOURequestStepConfirmation({
transaction.category,
transaction.tag,
transaction.billable,
transaction.iouRequestType,
);
return;
}
Expand Down
18 changes: 18 additions & 0 deletions src/types/onyx/QuickAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type {ValueOf} from 'type-fest';
import type CONST from '@src/CONST';

type QuickAction = {
/** The action to take */
action?: ValueOf<typeof CONST.QUICK_ACTIONS>;

/** ID of the report */
reportID?: string;

/** ID of the target account for task actions */
targetAccountID?: number;

/** True if it is the first quick action we store for this user */
isFirstQuickAction?: boolean;
};

export default QuickAction;
2 changes: 2 additions & 0 deletions src/types/onyx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type {PolicyTag, PolicyTagList, PolicyTags} from './PolicyTag';
import type PreferredTheme from './PreferredTheme';
import type PriorityMode from './PriorityMode';
import type PrivatePersonalDetails from './PrivatePersonalDetails';
import type QuickAction from './QuickAction';
import type RecentlyUsedCategories from './RecentlyUsedCategories';
import type RecentlyUsedReportFields from './RecentlyUsedReportFields';
import type RecentlyUsedTags from './RecentlyUsedTags';
Expand Down Expand Up @@ -119,6 +120,7 @@ export type {
PreferredTheme,
PriorityMode,
PrivatePersonalDetails,
QuickAction,
RecentWaypoint,
RecentlyUsedCategories,
RecentlyUsedTags,
Expand Down

0 comments on commit 7014d47

Please sign in to comment.