diff --git a/src/ONYXKEYS.js b/src/ONYXKEYS.js index d7fcd65074f3..dda30feab524 100755 --- a/src/ONYXKEYS.js +++ b/src/ONYXKEYS.js @@ -21,6 +21,9 @@ export default { // What the active route is for our navigator. Global route that determines what views to display. CURRENT_URL: 'currentURL', + // Stores current date + CURRENT_DATE: 'currentDate', + // Currently viewed reportID CURRENTLY_VIEWED_REPORTID: 'currentlyViewedReportID', diff --git a/src/libs/DateUtils.js b/src/libs/DateUtils.js index d4fb2008ae41..84b44f501a15 100644 --- a/src/libs/DateUtils.js +++ b/src/libs/DateUtils.js @@ -88,12 +88,31 @@ function timestampToRelative(locale, timestamp) { return moment(date).fromNow(); } +/** + * A throttled version of a function that updates the current date in Onyx store + */ +const updateCurrentDate = _.throttle(() => { + const currentDate = moment().format('YYYY-MM-DD'); + Onyx.set(ONYXKEYS.CURRENT_DATE, currentDate); +}, 1000 * 60 * 60 * 3); // 3 hours + +/** + * Initialises the event listeners that trigger the current date update + */ +function startCurrentDateUpdater() { + const trackedEvents = ['mousemove', 'touchstart', 'keydown', 'scroll']; + trackedEvents.forEach((eventName) => { + document.addEventListener(eventName, updateCurrentDate); + }); +} + /** * @namespace DateUtils */ const DateUtils = { timestampToRelative, timestampToDateTime, + startCurrentDateUpdater, }; export default DateUtils; diff --git a/src/pages/home/report/ReportActionItemDate.js b/src/pages/home/report/ReportActionItemDate.js index dc9e6a984a8c..d1417735ae18 100644 --- a/src/pages/home/report/ReportActionItemDate.js +++ b/src/pages/home/report/ReportActionItemDate.js @@ -1,8 +1,11 @@ import React, {memo} from 'react'; import PropTypes from 'prop-types'; import {Text} from 'react-native'; +import {withOnyx} from 'react-native-onyx'; import styles from '../../../styles/styles'; import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize'; +import compose from '../../../libs/compose'; +import ONYXKEYS from '../../../ONYXKEYS'; const propTypes = { /** UTC timestamp for when the action was created */ @@ -19,4 +22,12 @@ const ReportActionItemDate = props => ( ReportActionItemDate.propTypes = propTypes; ReportActionItemDate.displayName = 'ReportActionItemDate'; -export default withLocalize(memo(ReportActionItemDate)); +export default compose( + withLocalize, + withOnyx({ + currentDate: { + key: ONYXKEYS.CURRENT_DATE, + }, + }), + memo, +)(ReportActionItemDate); diff --git a/src/setup/index.desktop.js b/src/setup/index.desktop.js index f1c9364e7ab3..67c07723fd64 100644 --- a/src/setup/index.desktop.js +++ b/src/setup/index.desktop.js @@ -2,6 +2,7 @@ import {AppRegistry} from 'react-native'; import {ipcRenderer} from 'electron'; import Config from '../CONFIG'; import LocalNotification from '../libs/Notification/LocalNotification'; +import DateUtils from '../libs/DateUtils'; export default function () { @@ -12,4 +13,7 @@ export default function () { ipcRenderer.on('update-downloaded', () => { LocalNotification.showUpdateAvailableNotification(); }); + + // Start current date updater + DateUtils.startCurrentDateUpdater(); } diff --git a/src/setup/index.website.js b/src/setup/index.website.js index 18e4d7df0505..a5c603188451 100644 --- a/src/setup/index.website.js +++ b/src/setup/index.website.js @@ -2,6 +2,7 @@ import {AppRegistry} from 'react-native'; import checkForUpdates from '../libs/checkForUpdates'; import Config from '../CONFIG'; import HttpUtils from '../libs/HttpUtils'; +import DateUtils from '../libs/DateUtils'; import {version as currentVersion} from '../../package.json'; import Visibility from '../libs/Visibility'; @@ -56,4 +57,7 @@ export default function () { if (Config.IS_IN_PRODUCTION) { checkForUpdates(webUpdater()); } + + // Start current date updater + DateUtils.startCurrentDateUpdater(); }