Skip to content
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/OptionsListUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
*/
let currentUserLogin: string | undefined;
let currentUserAccountID: number | undefined;
Onyx.connect({

Check warning on line 188 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
currentUserLogin = value?.email;
Expand All @@ -194,19 +194,19 @@
});

let loginList: OnyxEntry<Login>;
Onyx.connect({

Check warning on line 197 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.LOGIN_LIST,
callback: (value) => (loginList = isEmptyObject(value) ? {} : value),
});

let allPersonalDetails: OnyxEntry<PersonalDetailsList>;
Onyx.connect({

Check warning on line 203 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => (allPersonalDetails = isEmptyObject(value) ? {} : value),
});

const policies: OnyxCollection<Policy> = {};
Onyx.connect({

Check warning on line 209 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
callback: (policy, key) => {
if (!policy || !key || !policy.name) {
Expand All @@ -218,14 +218,14 @@
});

let allPolicies: OnyxCollection<Policy> = {};
Onyx.connect({

Check warning on line 221 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (val) => (allPolicies = val),
});

let allReports: OnyxCollection<Report>;
Onyx.connect({

Check warning on line 228 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -234,7 +234,7 @@
});

let allReportNameValuePairs: OnyxCollection<ReportNameValuePairs>;
Onyx.connect({

Check warning on line 237 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -246,7 +246,7 @@
const allSortedReportActions: Record<string, ReportAction[]> = {};
let allReportActions: OnyxCollection<ReportActions>;
const lastVisibleReportActions: ReportActions = {};
Onyx.connect({

Check warning on line 249 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Expand Down Expand Up @@ -306,13 +306,13 @@
});

let activePolicyID: OnyxEntry<string>;
Onyx.connect({

Check warning on line 309 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
callback: (value) => (activePolicyID = value),
});

let nvpDismissedProductTraining: OnyxEntry<DismissedProductTraining>;
Onyx.connect({

Check warning on line 315 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NVP_DISMISSED_PRODUCT_TRAINING,
callback: (value) => (nvpDismissedProductTraining = value),
});
Expand Down Expand Up @@ -643,7 +643,7 @@
} else if (isTaskAction(lastReportAction)) {
lastMessageTextFromReport = formatReportLastMessageText(getTaskReportActionMessage(lastReportAction).text);
} else if (isCreatedTaskReportAction(lastReportAction)) {
lastMessageTextFromReport = getTaskCreatedMessage(lastReportAction);
lastMessageTextFromReport = getTaskCreatedMessage(lastReportAction, getReportOrDraftReport(lastReportAction?.childReportID));
} else if (
isActionOfType(lastReportAction, CONST.REPORT.ACTIONS.TYPE.SUBMITTED) ||
isActionOfType(lastReportAction, CONST.REPORT.ACTIONS.TYPE.SUBMITTED_AND_CLOSED) ||
Expand Down
23 changes: 5 additions & 18 deletions src/libs/TaskUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Report} from '@src/types/onyx';
import type {Message} from '@src/types/onyx/ReportAction';
Expand All @@ -11,15 +9,6 @@ import Navigation from './Navigation/Navigation';
import Parser from './Parser';
import {getReportActionHtml, getReportActionText} from './ReportActionsUtils';

let allReports: OnyxCollection<Report> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
allReports = value;
},
});

/**
* Check if the active route belongs to task edit flow.
*/
Expand Down Expand Up @@ -52,7 +41,7 @@ function getTaskReportActionMessage(action: OnyxEntry<ReportAction>): Pick<Messa
}
}

function getTaskTitleFromReport(taskReport: OnyxEntry<Report>, fallbackTitle = '', shouldReturnMarkdown = false): string {
function getTaskTitleFromReport(taskReport?: OnyxEntry<Report>, fallbackTitle = '', shouldReturnMarkdown = false): string {
// We need to check for reportID, not just reportName, because when a receiver opens the task for the first time,
// an optimistic report is created with the only property - reportName: 'Chat report',
// and it will be displayed as the task title without checking for reportID to be present.
Expand All @@ -61,14 +50,12 @@ function getTaskTitleFromReport(taskReport: OnyxEntry<Report>, fallbackTitle = '
return shouldReturnMarkdown ? Parser.htmlToMarkdown(title) : Parser.htmlToText(title).trim();
}

function getTaskTitle(taskReportID: string | undefined, fallbackTitle = '', shouldReturnMarkdown = false): string {
const taskReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`];
function getTaskTitle(taskReport?: OnyxEntry<Report>, fallbackTitle = '', shouldReturnMarkdown = false): string {
return getTaskTitleFromReport(taskReport, fallbackTitle, shouldReturnMarkdown);
}

function getTaskCreatedMessage(reportAction: OnyxEntry<ReportAction>, shouldReturnMarkdown = false) {
const taskReportID = reportAction?.childReportID;
const taskTitle = getTaskTitle(taskReportID, reportAction?.childReportName, shouldReturnMarkdown);
function getTaskCreatedMessage(reportAction: OnyxEntry<ReportAction>, taskReport?: OnyxEntry<Report>, shouldReturnMarkdown = false) {
const taskTitle = getTaskTitle(taskReport, reportAction?.childReportName, shouldReturnMarkdown);
return taskTitle ? translateLocal('task.messages.created', {title: taskTitle}) : '';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ function BaseReportActionContextMenu({
card,
originalReport,
isTryNewDotNVPDismissed,
childReport,
movedFromReport,
movedToReport,
};
Expand Down
5 changes: 3 additions & 2 deletions src/pages/home/report/ContextMenu/ContextMenuActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ type ContextMenuActionPayload = {
card?: Card;
originalReport: OnyxEntry<ReportType>;
isTryNewDotNVPDismissed?: boolean;
childReport?: OnyxEntry<ReportType>;
movedFromReport?: OnyxEntry<ReportType>;
movedToReport?: OnyxEntry<ReportType>;
};
Expand Down Expand Up @@ -501,7 +502,7 @@ const ContextMenuActions: ContextMenuAction[] = [
// If return value is true, we switch the `text` and `icon` on
// `ContextMenuItem` with `successText` and `successIcon` which will fall back to
// the `text` and `icon`
onPress: (closePopover, {reportAction, transaction, selection, report, reportID, card, originalReport, isTryNewDotNVPDismissed, movedFromReport, movedToReport}) => {
onPress: (closePopover, {reportAction, transaction, selection, report, reportID, card, originalReport, isTryNewDotNVPDismissed, movedFromReport, movedToReport, childReport}) => {
const isReportPreviewAction = isReportPreviewActionReportActionsUtils(reportAction);
const messageHtml = getActionHtml(reportAction);
const messageText = getReportActionMessageText(reportAction);
Expand Down Expand Up @@ -537,7 +538,7 @@ const ContextMenuActions: ContextMenuAction[] = [
setClipboardMessage(displayMessage);
}
} else if (isCreatedTaskReportAction(reportAction)) {
const taskPreviewMessage = getTaskCreatedMessage(reportAction, true);
const taskPreviewMessage = getTaskCreatedMessage(reportAction, childReport, true);
Clipboard.setString(taskPreviewMessage);
} else if (isMemberChangeAction(reportAction)) {
const logMessage = getMemberChangeMessageFragment(reportAction, getReportName).html ?? '';
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/libs/TaskutilsTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {getTaskTitle} from '../../../src/libs/TaskUtils';
import {createRegularTaskReport} from '../../utils/collections/reports';

jest.mock('../../../src/libs/Localize');

describe('TaskUtils', () => {
describe('getTaskTitle', () => {
it('should return the task title from the report', () => {
const taskReport = createRegularTaskReport(1, 123);
taskReport.reportName = '<b>Task</b> Title';
expect(getTaskTitle(taskReport)).toBe('Task Title');
});

it('should return the fallback title if reportName is not present', () => {
const taskReport = {reportID: '123'};
expect(getTaskTitle(taskReport, 'Fallback Title')).toBe('Fallback Title');
});

it('should return the fallback title if reportID is not present', () => {
const taskReport = createRegularTaskReport(1, 123);
taskReport.reportID = '';
expect(getTaskTitle(taskReport, 'Fallback Title')).toBe('Fallback Title');
});

it('should return the title in markdown if shouldReturnMarkdown is true', () => {
const taskReport = createRegularTaskReport(1, 123);
taskReport.reportName = '<b>Task</b> Title';
expect(getTaskTitle(taskReport, '', true)).toBe('*Task* Title');
});
});
});
Loading