From a09c39e3a6bb5e27503af4070bafc93532426d9c Mon Sep 17 00:00:00 2001 From: Dmytro Klymenko Date: Sat, 12 Jun 2021 15:26:33 +0300 Subject: [PATCH 1/3] #3272 add current date updater --- src/ONYXKEYS.js | 3 +++ src/libs/DateUtils.js | 19 +++++++++++++++++++ src/pages/home/report/ReportActionItemDate.js | 13 ++++++++++++- src/setup/index.website.js | 5 +++++ 4 files changed, 39 insertions(+), 1 deletion(-) 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.website.js b/src/setup/index.website.js index 18e4d7df0505..066e89d8cf6f 100644 --- a/src/setup/index.website.js +++ b/src/setup/index.website.js @@ -2,9 +2,11 @@ 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'; + /** * Download the latest app version from the server, and if it is different than the current one, * then refresh. If the page is visibile, prompt the user to refresh. @@ -56,4 +58,7 @@ export default function () { if (Config.IS_IN_PRODUCTION) { checkForUpdates(webUpdater()); } + + // Start current date updater + DateUtils.startCurrentDateUpdater(); } From 348f41281f57a0a5ecd96192a3323708a47d1faa Mon Sep 17 00:00:00 2001 From: Dmytro Klymenko Date: Sun, 13 Jun 2021 07:02:24 +0300 Subject: [PATCH 2/3] #3272 add date updater to desktop --- src/setup/index.desktop.js | 4 ++++ 1 file changed, 4 insertions(+) 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(); } From 1a13dc691b247cd423e286bf09385728645fa472 Mon Sep 17 00:00:00 2001 From: Dmytro Klymenko Date: Sun, 13 Jun 2021 12:22:57 +0300 Subject: [PATCH 3/3] #3272 remove extra linebreak --- src/setup/index.website.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/setup/index.website.js b/src/setup/index.website.js index 066e89d8cf6f..a5c603188451 100644 --- a/src/setup/index.website.js +++ b/src/setup/index.website.js @@ -6,7 +6,6 @@ import DateUtils from '../libs/DateUtils'; import {version as currentVersion} from '../../package.json'; import Visibility from '../libs/Visibility'; - /** * Download the latest app version from the server, and if it is different than the current one, * then refresh. If the page is visibile, prompt the user to refresh.