diff --git a/src/components/LHNOptionsList/OptionRowLHN.js b/src/components/LHNOptionsList/OptionRowLHN.js index 20dfc639fcba..797b5ae469de 100644 --- a/src/components/LHNOptionsList/OptionRowLHN.js +++ b/src/components/LHNOptionsList/OptionRowLHN.js @@ -1,7 +1,8 @@ import _ from 'underscore'; -import React from 'react'; +import React, {useEffect} from 'react'; import PropTypes from 'prop-types'; import {View, StyleSheet} from 'react-native'; +import lodashGet from 'lodash/get'; import * as optionRowStyles from '../../styles/optionRowStyles'; import styles from '../../styles/styles'; import * as StyleUtils from '../../styles/StyleUtils'; @@ -12,6 +13,7 @@ import Hoverable from '../Hoverable'; import DisplayNames from '../DisplayNames'; import colors from '../../styles/colors'; import withLocalize, {withLocalizePropTypes} from '../withLocalize'; +import {withReportCommentDrafts} from '../OnyxProvider'; import Text from '../Text'; import SubscriptAvatar from '../SubscriptAvatar'; import CONST from '../../CONST'; @@ -23,12 +25,18 @@ import PressableWithSecondaryInteraction from '../PressableWithSecondaryInteract import * as ReportActionContextMenu from '../../pages/home/report/ContextMenu/ReportActionContextMenu'; import * as ContextMenuActions from '../../pages/home/report/ContextMenu/ContextMenuActions'; import * as OptionsListUtils from '../../libs/OptionsListUtils'; +import compose from '../../libs/compose'; +import ONYXKEYS from '../../ONYXKEYS'; +import * as Report from '../../libs/actions/Report'; const propTypes = { /** Style for hovered state */ // eslint-disable-next-line react/forbid-prop-types hoverStyle: PropTypes.object, + /** The comment left by the user */ + comment: PropTypes.string, + /** The ID of the report that the option is for */ reportID: PropTypes.string.isRequired, @@ -52,11 +60,20 @@ const defaultProps = { onSelectRow: () => {}, isFocused: false, style: null, + comment: '', }; function OptionRowLHN(props) { const optionItem = SidebarUtils.getOptionData(props.reportID); + useEffect(() => { + if (!optionItem || optionItem.hasDraftComment || !props.comment || props.comment.length <= 0 || props.isFocused) { + return; + } + Report.setReportWithDraft(props.reportID, true); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + if (!optionItem) { return null; } @@ -255,4 +272,13 @@ OptionRowLHN.propTypes = propTypes; OptionRowLHN.defaultProps = defaultProps; OptionRowLHN.displayName = 'OptionRowLHN'; -export default withLocalize(OptionRowLHN); +export default compose( + withLocalize, + withReportCommentDrafts({ + propName: 'comment', + transformValue: (drafts, props) => { + const draftKey = `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${props.reportID}`; + return lodashGet(drafts, draftKey, ''); + }, + }), +)(OptionRowLHN); diff --git a/src/components/OnyxProvider.js b/src/components/OnyxProvider.js index 929d8cbe3f7f..dfe96684ae3c 100644 --- a/src/components/OnyxProvider.js +++ b/src/components/OnyxProvider.js @@ -12,6 +12,7 @@ const [withCurrentDate, CurrentDateProvider] = createOnyxContext(ONYXKEYS.CURREN const [withReportActionsDrafts, ReportActionsDraftsProvider] = createOnyxContext(ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS); const [withBlockedFromConcierge, BlockedFromConciergeProvider] = createOnyxContext(ONYXKEYS.NVP_BLOCKED_FROM_CONCIERGE); const [withBetas, BetasProvider] = createOnyxContext(ONYXKEYS.BETAS); +const [withReportCommentDrafts, ReportCommentDraftsProvider] = createOnyxContext(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT); const propTypes = { /** Rendered child component */ @@ -20,7 +21,17 @@ const propTypes = { function OnyxProvider(props) { return ( - + {props.children} ); @@ -31,4 +42,4 @@ OnyxProvider.propTypes = propTypes; export default OnyxProvider; -export {withNetwork, withPersonalDetails, withReportActionsDrafts, withCurrentDate, withBlockedFromConcierge, withBetas, NetworkContext}; +export {withNetwork, withPersonalDetails, withReportActionsDrafts, withCurrentDate, withBlockedFromConcierge, withBetas, NetworkContext, withReportCommentDrafts}; diff --git a/src/pages/home/report/ReportActionCompose.js b/src/pages/home/report/ReportActionCompose.js index 35e33bb5170a..eb3909a4ae45 100644 --- a/src/pages/home/report/ReportActionCompose.js +++ b/src/pages/home/report/ReportActionCompose.js @@ -245,6 +245,10 @@ class ReportActionCompose extends React.Component { showPopoverMenu: this.showPopoverMenu, }); } + + if (this.props.comment.length !== 0) { + Report.setReportWithDraft(this.props.reportID, true); + } } componentDidUpdate(prevProps) {