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

Create the buildOptimisticReportObject function #10801

Merged
merged 19 commits into from
Sep 8, 2022
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
3 changes: 3 additions & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ const CONST = {
ANNOUNCE: '#announce',
ADMINS: '#admins',
},
STATE: {
SUBMITTED: 'SUBMITTED',
},
STATE_NUM: {
OPEN: 0,
PROCESSING: 1,
Expand Down
25 changes: 25 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import md5 from './md5';
import Navigation from './Navigation/Navigation';
import ROUTES from '../ROUTES';
import * as NumberUtils from './NumberUtils';
import * as NumberFormatUtils from './NumberFormatUtils';

let sessionEmail;
Onyx.connect({
Expand Down Expand Up @@ -571,6 +572,29 @@ function hasReportNameError(report) {
return !_.isEmpty(lodashGet(report, 'errorFields.reportName', {}));
}

/*
* Builds an optimistic IOU report with a randomly generated reportID
*/
function buildOptimisticIOUReport(ownerEmail, recipientEmail, total, chatReportID, currency, locale) {
const formattedTotal = NumberFormatUtils.format(locale,
total, {
style: 'currency',
currency,
});
return {
cachedTotal: formattedTotal,
chatReportID,
currency,
hasOutstandingIOU: true,
managerEmail: recipientEmail,
ownerEmail,
reportID: generateReportID(),
state: CONST.REPORT.STATE.SUBMITTED,
stateNum: 1,
total,
};
}

/**
* Builds an optimistic IOU reportAction object
*
Expand Down Expand Up @@ -672,6 +696,7 @@ export {
navigateToDetailsPage,
generateReportID,
hasReportNameError,
buildOptimisticIOUReport,
buildOptimisticIOUReportAction,
isUnread,
};
2 changes: 1 addition & 1 deletion src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ function createWorkspace() {
expenseChatReportID,
expenseChatData,
expenseReportActionData,
} = Report.createOptimisticWorkspaceChats(policyID, workspaceName);
} = Report.buildOptimisticWorkspaceChats(policyID, workspaceName);

// We need to use makeRequestWithSideEffects as we try to redirect to the policy right after creation
// The policy hasn't been merged in Onyx data at this point, leading to an intermittent Not Found screen
Expand Down
26 changes: 13 additions & 13 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ function fetchAllReports(
}

/**
* Creates an optimistic chat report with a randomly generated reportID and as much information as we currently have
* Builds an optimistic chat report with a randomly generated reportID and as much information as we currently have
*
* @param {Array} participantList
* @param {String} reportName
Expand All @@ -596,7 +596,7 @@ function fetchAllReports(
* @param {String} oldPolicyName
* @returns {Object}
*/
function createOptimisticChatReport(
function buildOptimisticChatReport(
participantList,
reportName = 'Chat Report',
chatType = '',
Expand Down Expand Up @@ -635,7 +635,7 @@ function createOptimisticChatReport(
* @param {String} ownerEmail
* @returns {Object}
*/
function createOptimisticCreatedReportAction(ownerEmail) {
function buildOptimisticCreatedReportAction(ownerEmail) {
return {
0: {
actionName: CONST.REPORT.ACTIONS.TYPE.CREATED,
Expand Down Expand Up @@ -675,8 +675,8 @@ function createOptimisticCreatedReportAction(ownerEmail) {
* @param {String} policyName
* @returns {Object}
*/
function createOptimisticWorkspaceChats(policyID, policyName) {
const announceChatData = createOptimisticChatReport(
function buildOptimisticWorkspaceChats(policyID, policyName) {
const announceChatData = buildOptimisticChatReport(
[currentUserEmail],
CONST.REPORT.WORKSPACE_CHAT_ROOMS.ANNOUNCE,
CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE,
Expand All @@ -686,15 +686,15 @@ function createOptimisticWorkspaceChats(policyID, policyName) {
policyName,
);
const announceChatReportID = announceChatData.reportID;
const announceReportActionData = createOptimisticCreatedReportAction(announceChatData.ownerEmail);
const announceReportActionData = buildOptimisticCreatedReportAction(announceChatData.ownerEmail);

const adminsChatData = createOptimisticChatReport([currentUserEmail], CONST.REPORT.WORKSPACE_CHAT_ROOMS.ADMINS, CONST.REPORT.CHAT_TYPE.POLICY_ADMINS, policyID, null, false, policyName);
const adminsChatData = buildOptimisticChatReport([currentUserEmail], CONST.REPORT.WORKSPACE_CHAT_ROOMS.ADMINS, CONST.REPORT.CHAT_TYPE.POLICY_ADMINS, policyID, null, false, policyName);
const adminsChatReportID = adminsChatData.reportID;
const adminsReportActionData = createOptimisticCreatedReportAction(adminsChatData.ownerEmail);
const adminsReportActionData = buildOptimisticCreatedReportAction(adminsChatData.ownerEmail);

const expenseChatData = createOptimisticChatReport([currentUserEmail], '', CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, policyID, currentUserEmail, true, policyName);
const expenseChatData = buildOptimisticChatReport([currentUserEmail], '', CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, policyID, currentUserEmail, true, policyName);
const expenseChatReportID = expenseChatData.reportID;
const expenseReportActionData = createOptimisticCreatedReportAction(expenseChatData.ownerEmail);
const expenseReportActionData = buildOptimisticCreatedReportAction(expenseChatData.ownerEmail);

return {
announceChatReportID,
Expand Down Expand Up @@ -1631,9 +1631,9 @@ export {
readOldestAction,
openReport,
openPaymentDetailsPage,
createOptimisticWorkspaceChats,
createOptimisticChatReport,
createOptimisticCreatedReportAction,
buildOptimisticWorkspaceChats,
buildOptimisticChatReport,
buildOptimisticCreatedReportAction,
updatePolicyRoomName,
clearPolicyRoomNameErrors,
};