Skip to content

Commit

Permalink
Expensify#3272 add current date updater
Browse files Browse the repository at this point in the history
  • Loading branch information
dklymenk committed Jun 12, 2021
1 parent 05aedfe commit a09c39e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/ONYXKEYS.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand Down
19 changes: 19 additions & 0 deletions src/libs/DateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
13 changes: 12 additions & 1 deletion src/pages/home/report/ReportActionItemDate.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -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);
5 changes: 5 additions & 0 deletions src/setup/index.website.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -56,4 +58,7 @@ export default function () {
if (Config.IS_IN_PRODUCTION) {
checkForUpdates(webUpdater());
}

// Start current date updater
DateUtils.startCurrentDateUpdater();
}

0 comments on commit a09c39e

Please sign in to comment.