From 5b6d67a607d1ab51c2c55caea2d5297d57e92d73 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Wed, 27 Dec 2023 13:55:36 -0700 Subject: [PATCH 1/5] Remove deprecated methods and refactor code to be a little cleaner --- src/pages/FlagCommentPage.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/pages/FlagCommentPage.js b/src/pages/FlagCommentPage.js index 6c6421593837d..22931ec844d16 100644 --- a/src/pages/FlagCommentPage.js +++ b/src/pages/FlagCommentPage.js @@ -1,5 +1,5 @@ import PropTypes from 'prop-types'; -import React, {useCallback} from 'react'; +import React, {useEffect, useRef} from 'react'; import {ScrollView, View} from 'react-native'; import {withOnyx} from 'react-native-onyx'; import _ from 'underscore'; @@ -13,7 +13,6 @@ import withLocalize, {withLocalizePropTypes} from '@components/withLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import compose from '@libs/compose'; import Navigation from '@libs/Navigation/Navigation'; -import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as Report from '@userActions/Report'; import * as Session from '@userActions/Session'; @@ -115,28 +114,31 @@ function FlagCommentPage(props) { }, ]; - const getActionToFlag = useCallback(() => { - let reportAction = props.reportActions[`${props.route.params.reportActionID.toString()}`]; + // The report action that gets flagged is either the report action that was passed in via the route params + // or the parent report action if the report action is a thread, since threads can't be flagged themselves. + const reportActionToFlag = useRef(null); + useEffect(() => { + reportActionToFlag.current = props.reportActions[`${props.route.params.reportActionID}`]; - // Handle threads if needed - if (reportAction === undefined || reportAction.reportActionID === undefined) { - reportAction = ReportActionsUtils.getParentReportAction(props.report); + // If the reportActionToFlag is not a thread, then return early + if (reportActionToFlag.current && reportActionToFlag.current.reportActionID !== undefined) { + return; } - return reportAction; - }, [props.report, props.reportActions, props.route.params.reportActionID]); + // If the reportActionToFlag is a thread, then the action to flag is the parent report action + reportActionToFlag.current = props.parentReportActions[`${props.report.parentReportActionID}`]; + }, [props.report, props.reportActions, props.route.params.reportActionID, props.parentReportActions]); const flagComment = (severity) => { let reportID = getReportID(props.route); - const reportAction = getActionToFlag(); // Handle threads if needed - if (ReportUtils.isChatThread(props.report) && reportAction.reportActionID === ReportActionsUtils.getParentReportAction(props.report).reportActionID) { - reportID = ReportUtils.getParentReport(props.report).reportID; + if (ReportUtils.isChatThread(props.report) && reportActionToFlag.current.reportActionID === props.report.parentReportActionID) { + reportID = props.report.parentReportID; } - if (ReportUtils.canFlagReportAction(reportAction, reportID)) { - Report.flagComment(reportID, reportAction, severity); + if (ReportUtils.canFlagReportAction(reportActionToFlag.current, reportID)) { + Report.flagComment(reportID, reportActionToFlag.current, severity); } Navigation.dismissModal(); @@ -161,7 +163,7 @@ function FlagCommentPage(props) { testID={FlagCommentPage.displayName} > {({safeAreaPaddingBottomStyle}) => ( - + Date: Mon, 8 Jan 2024 09:53:21 -0700 Subject: [PATCH 2/5] Simplify refactoring --- src/pages/FlagCommentPage.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/pages/FlagCommentPage.js b/src/pages/FlagCommentPage.js index 22931ec844d16..cae91a0975017 100644 --- a/src/pages/FlagCommentPage.js +++ b/src/pages/FlagCommentPage.js @@ -1,5 +1,5 @@ import PropTypes from 'prop-types'; -import React, {useEffect, useRef} from 'react'; +import React, {useCallback} from 'react'; import {ScrollView, View} from 'react-native'; import {withOnyx} from 'react-native-onyx'; import _ from 'underscore'; @@ -42,10 +42,15 @@ const propTypes = { }).isRequired, ...withLocalizePropTypes, + + /* Onyx Props */ + /** All the report actions from the parent report */ + parentReportActions: PropTypes.objectOf(PropTypes.shape(reportActionPropTypes)), }; const defaultProps = { reportActions: {}, + parentReportActions: {}, report: {}, }; @@ -114,31 +119,29 @@ function FlagCommentPage(props) { }, ]; - // The report action that gets flagged is either the report action that was passed in via the route params - // or the parent report action if the report action is a thread, since threads can't be flagged themselves. - const reportActionToFlag = useRef(null); - useEffect(() => { - reportActionToFlag.current = props.reportActions[`${props.route.params.reportActionID}`]; + const getActionToFlag = useCallback(() => { + let reportAction = props.reportActions[`${props.route.params.reportActionID.toString()}`]; - // If the reportActionToFlag is not a thread, then return early - if (reportActionToFlag.current && reportActionToFlag.current.reportActionID !== undefined) { - return; + // Handle threads if needed + if (reportAction === undefined || reportAction.reportActionID === undefined) { + reportAction = props.parentReportActions[`${props.report.parentReportActionID}`]; } - // If the reportActionToFlag is a thread, then the action to flag is the parent report action - reportActionToFlag.current = props.parentReportActions[`${props.report.parentReportActionID}`]; + return reportAction; }, [props.report, props.reportActions, props.route.params.reportActionID, props.parentReportActions]); const flagComment = (severity) => { let reportID = getReportID(props.route); + const reportAction = getActionToFlag(); + const parentReportAction = props.parentReportActions[`${props.report.parentReportActionID}`]; // Handle threads if needed - if (ReportUtils.isChatThread(props.report) && reportActionToFlag.current.reportActionID === props.report.parentReportActionID) { - reportID = props.report.parentReportID; + if (ReportUtils.isChatThread(props.report) && reportAction.reportActionID === parentReportAction.reportActionID) { + reportID = ReportUtils.getParentReport(props.report).reportID; } - if (ReportUtils.canFlagReportAction(reportActionToFlag.current, reportID)) { - Report.flagComment(reportID, reportActionToFlag.current, severity); + if (ReportUtils.canFlagReportAction(reportAction, reportID)) { + Report.flagComment(reportID, reportAction, severity); } Navigation.dismissModal(); @@ -163,7 +166,7 @@ function FlagCommentPage(props) { testID={FlagCommentPage.displayName} > {({safeAreaPaddingBottomStyle}) => ( - + Date: Mon, 8 Jan 2024 09:54:06 -0700 Subject: [PATCH 3/5] Default to empty object --- src/pages/FlagCommentPage.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/FlagCommentPage.js b/src/pages/FlagCommentPage.js index cae91a0975017..f98f76e4a91e9 100644 --- a/src/pages/FlagCommentPage.js +++ b/src/pages/FlagCommentPage.js @@ -124,7 +124,7 @@ function FlagCommentPage(props) { // Handle threads if needed if (reportAction === undefined || reportAction.reportActionID === undefined) { - reportAction = props.parentReportActions[`${props.report.parentReportActionID}`]; + reportAction = props.parentReportActions[`${props.report.parentReportActionID}`] || {}; } return reportAction; @@ -133,7 +133,7 @@ function FlagCommentPage(props) { const flagComment = (severity) => { let reportID = getReportID(props.route); const reportAction = getActionToFlag(); - const parentReportAction = props.parentReportActions[`${props.report.parentReportActionID}`]; + const parentReportAction = props.parentReportActions[`${props.report.parentReportActionID}`] || {}; // Handle threads if needed if (ReportUtils.isChatThread(props.report) && reportAction.reportActionID === parentReportAction.reportActionID) { From 1b342735e4bdd4d5b4731a9e4984ab61c148b685 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Mon, 8 Jan 2024 12:39:38 -0700 Subject: [PATCH 4/5] Update src/pages/FlagCommentPage.js Co-authored-by: Carlos Martins --- src/pages/FlagCommentPage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/FlagCommentPage.js b/src/pages/FlagCommentPage.js index f98f76e4a91e9..cc54d30874749 100644 --- a/src/pages/FlagCommentPage.js +++ b/src/pages/FlagCommentPage.js @@ -124,7 +124,7 @@ function FlagCommentPage(props) { // Handle threads if needed if (reportAction === undefined || reportAction.reportActionID === undefined) { - reportAction = props.parentReportActions[`${props.report.parentReportActionID}`] || {}; + reportAction = props.parentReportActions[props.report.parentReportActionID] || {}; } return reportAction; From 7669dc554e8dc60c909a696b23062c454e44f4b5 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Mon, 8 Jan 2024 12:39:47 -0700 Subject: [PATCH 5/5] Update src/pages/FlagCommentPage.js Co-authored-by: Carlos Martins --- src/pages/FlagCommentPage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/FlagCommentPage.js b/src/pages/FlagCommentPage.js index cc54d30874749..47d2ad356dadf 100644 --- a/src/pages/FlagCommentPage.js +++ b/src/pages/FlagCommentPage.js @@ -133,7 +133,7 @@ function FlagCommentPage(props) { const flagComment = (severity) => { let reportID = getReportID(props.route); const reportAction = getActionToFlag(); - const parentReportAction = props.parentReportActions[`${props.report.parentReportActionID}`] || {}; + const parentReportAction = props.parentReportActions[props.report.parentReportActionID] || {}; // Handle threads if needed if (ReportUtils.isChatThread(props.report) && reportAction.reportActionID === parentReportAction.reportActionID) {