Skip to content

Load parent report action from withOnyx in report action HOC #34113

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

Merged
merged 30 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
cc24a97
Add the data to be loaded from onyx
tgolen Jan 8, 2024
d318c95
Remove the use of lodash.
tgolen Jan 8, 2024
b2b8868
Remove deprecated method
tgolen Jan 8, 2024
33e348e
Merge branch 'main' into tgolen-refactor-hocsideloading
tgolen Jan 12, 2024
4237770
Update onyx
tgolen Jan 12, 2024
83b1547
Fix types
tgolen Jan 12, 2024
2440051
Remove extra onyx subscription
tgolen Jan 12, 2024
05b979d
Add all withOnyx properties and import type
tgolen Jan 12, 2024
6f7b07f
Remove unused imports
tgolen Jan 12, 2024
fe4cb1a
Style
tgolen Jan 12, 2024
9cf1ed8
Merge branch 'main' into tgolen-refactor-hocsideloading
tgolen Jan 16, 2024
b2173c0
Merge branch 'main' into tgolen-refactor-hocsideloading
tgolen Jan 30, 2024
e77f37d
Update Onyx
tgolen Jan 31, 2024
093b922
Import type
tgolen Jan 31, 2024
253ee78
Merge branch 'main' into tgolen-refactor-hocsideloading
tgolen Jan 31, 2024
824f2fd
Update lock file
tgolen Jan 31, 2024
20c290f
Separate into multiple withOnyx
tgolen Feb 1, 2024
5484326
Merge branch 'main' into tgolen-refactor-hocsideloading
tgolen Feb 2, 2024
3d0c4e7
Merge branch 'main' into tgolen-refactor-hocsideloading
tgolen Feb 5, 2024
1c275d1
Try to improve the withOnyx types
tgolen Feb 5, 2024
64a65fa
Use hook for dimensions
tgolen Feb 5, 2024
f18570a
Try to fix types
tgolen Feb 5, 2024
6febb6e
Remove debug
tgolen Feb 5, 2024
240065b
Reset the component to an earlier point in time
tgolen Feb 5, 2024
0a10216
Add a better check for when to show the not-found page
tgolen Feb 5, 2024
e112608
Fix selector types
tgolen Feb 5, 2024
cf4be5f
Update src/pages/home/report/withReportAndReportActionOrNotFound.tsx
tgolen Feb 6, 2024
31009d2
Merge branch 'main' into tgolen-refactor-hocsideloading
tgolen Feb 7, 2024
2deac05
Merge branch 'main' into tgolen-refactor-hocsideloading
tgolen Feb 8, 2024
d698da3
Fix changes from TS migration
tgolen Feb 8, 2024
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"react-native-linear-gradient": "^2.8.1",
"react-native-localize": "^2.2.6",
"react-native-modal": "^13.0.0",
"react-native-onyx": "2.0.1",
"react-native-onyx": "2.0.2",
"react-native-pager-view": "6.2.2",
"react-native-pdf": "6.7.3",
"react-native-performance": "^5.1.0",
Expand Down
26 changes: 7 additions & 19 deletions src/pages/FlagCommentPage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import PropTypes from 'prop-types';
import React, {useCallback} from 'react';
import {ScrollView, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
Expand All @@ -17,7 +16,6 @@ import * as ReportUtils from '@libs/ReportUtils';
import * as Report from '@userActions/Report';
import * as Session from '@userActions/Session';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import reportActionPropTypes from './home/report/reportActionPropTypes';
import withReportAndReportActionOrNotFound from './home/report/withReportAndReportActionOrNotFound';
Expand All @@ -44,13 +42,13 @@ const propTypes = {
...withLocalizePropTypes,

/* Onyx Props */
/** All the report actions from the parent report */
parentReportActions: PropTypes.objectOf(PropTypes.shape(reportActionPropTypes)),
/** The full action from the parent report */
parentReportAction: PropTypes.shape(reportActionPropTypes),
};

const defaultProps = {
reportActions: {},
parentReportActions: {},
parentReportAction: {},
report: {},
};

Expand Down Expand Up @@ -124,19 +122,18 @@ function FlagCommentPage(props) {

// Handle threads if needed
if (reportAction === undefined || reportAction.reportActionID === undefined) {
reportAction = props.parentReportActions[props.report.parentReportActionID] || {};
reportAction = props.parentReportAction;
}

return reportAction;
}, [props.report, props.reportActions, props.route.params.reportActionID, props.parentReportActions]);
}, [props.reportActions, props.route.params.reportActionID, props.parentReportAction]);

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) && reportAction.reportActionID === parentReportAction.reportActionID) {
if (ReportUtils.isChatThread(props.report) && reportAction.reportActionID === props.parentReportAction.reportActionID) {
reportID = ReportUtils.getParentReport(props.report).reportID;
}

Expand Down Expand Up @@ -197,13 +194,4 @@ FlagCommentPage.propTypes = propTypes;
FlagCommentPage.defaultProps = defaultProps;
FlagCommentPage.displayName = 'FlagCommentPage';

export default compose(
withLocalize,
withReportAndReportActionOrNotFound,
withOnyx({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed because the parentReportAction comes from withReportAndReportActionOrNotFound now.

parentReportActions: {
key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID || report.reportID}`,
canEvict: false,
},
}),
)(FlagCommentPage);
export default compose(withLocalize, withReportAndReportActionOrNotFound)(FlagCommentPage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: This is function component so it would be good to use useLocalize hook. This will remove necessity of compose

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I don't want to add too many extra things to these PRs. They get difficult to review and test then.

25 changes: 20 additions & 5 deletions src/pages/home/report/withReportAndReportActionOrNotFound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import type {RouteProp} from '@react-navigation/native';
import type {ComponentType, ForwardedRef, RefAttributes} from 'react';
import React, {useCallback, useEffect} from 'react';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import type {OnyxCollection, OnyxEntry, WithOnyxInstanceState} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import withWindowDimensions from '@components/withWindowDimensions';
import type {WindowDimensionsProps} from '@components/withWindowDimensions/types';
import compose from '@libs/compose';
import getComponentDisplayName from '@libs/getComponentDisplayName';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import * as Report from '@userActions/Report';
Expand All @@ -27,6 +26,9 @@ type OnyxProps = {
/** Array of report actions for this report */
reportActions: OnyxEntry<OnyxTypes.ReportActions>;

/** The report's parentReportAction */
parentReportAction: OnyxEntry<OnyxTypes.ReportAction>;

/** The policies which the user has access to */
policies: OnyxCollection<OnyxTypes.Policy>;

Expand All @@ -49,11 +51,11 @@ export default function <TProps extends ComponentProps, TRef>(WrappedComponent:

// Handle threads if needed
if (!reportAction?.reportActionID) {
reportAction = ReportActionsUtils.getParentReportAction(props.report);
reportAction = props?.parentReportAction ?? {};
}

return reportAction;
}, [props.report, props.reportActions, props.route.params.reportActionID]);
}, [props.reportActions, props.route.params.reportActionID, props.parentReportAction]);

const reportAction = getReportAction();

Expand All @@ -78,7 +80,9 @@ export default function <TProps extends ComponentProps, TRef>(WrappedComponent:
}

// Perform the access/not found checks
if (shouldHideReport || isEmptyObject(reportAction)) {
// Be sure to avoid showing the not-found page while the parent report actions are still being read from Onyx. The parentReportAction will be undefined while it's being read from Onyx
// and then reportAction will either be a valid parentReportAction or an empty object. In the case of an empty object, then it's OK to show the not-found page.
if (shouldHideReport || (props?.parentReportAction !== undefined && isEmptyObject(reportAction))) {
return <NotFoundPage />;
}

Expand Down Expand Up @@ -114,6 +118,17 @@ export default function <TProps extends ComponentProps, TRef>(WrappedComponent:
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${route.params.reportID}`,
canEvict: false,
},
parentReportAction: {
key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report ? report.parentReportID : 0}`,
selector: (parentReportActions: OnyxEntry<OnyxTypes.ReportActions>, props: WithOnyxInstanceState<OnyxProps>): OnyxEntry<OnyxTypes.ReportAction> => {
const parentReportActionID = props?.report?.parentReportActionID;
if (!parentReportActionID) {
return null;
}
return parentReportActions?.[parentReportActionID] ?? null;
},
canEvict: false,
},
}),
withWindowDimensions,
)(React.forwardRef(WithReportOrNotFound));
Expand Down