Skip to content
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

Some initial fixes and code style updates #1

Merged
merged 6 commits into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 38 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# EditorConfig is awesome: http://EditorConfig.org

# Howto with your editor:
# Sublime: https://github.com/sindresorhus/editorconfig-sublime

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[**]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4

# Standard at: https://github.com/felixge/node-style-guide
[**.js, **.json]
trim_trailing_whitespace = true
quote_type = single
curly_bracket_next_line = false
spaces_around_operators = true
space_after_control_statements = true
space_after_anonymous_functions = false
spaces_in_brackets = false

# No Standard. Please document a standard if different from .js
[**.yml, **.html, **.css]
trim_trailing_whitespace = true

# No standard. Please document a standard if different from .js
[**.md]

# Standard at:
[Makefile]

[package*]
indent_style = space
indent_size = 2
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* @format
*/

import {AppRegistry} from 'react-native';
import { AppRegistry } from 'react-native';
import App from './js/App';
import {name as appName} from './app.json';
import { name as appName } from './app.json';

AppRegistry.registerComponent(appName, () => App);
90 changes: 45 additions & 45 deletions js/Expensify.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
import {init as StoreInit} from './store/Store.js';
import { init as StoreInit } from './store/Store.js';
import SignInPage from './page/SignInPage.js';
import HomePage from './page/HomePage/HomePage.js';
import * as Store from './store/Store.js';
import * as ActiveClientManager from './lib/ActiveClientManager.js';
import {verifyAuthToken} from './store/actions/SessionActions.js';
import { verifyAuthToken } from './store/actions/SessionActions.js';
import STOREKEYS from './store/STOREKEYS.js';
import React, {Component} from 'react';
import {Route, Router, Redirect, Switch} from './lib/Router';
import React, { Component } from 'react';
import { Route, Router, Redirect, Switch } from './lib/Router';

// Initialize the store when the app loads for the first time
StoreInit();

export default class Expensify extends Component {
constructor(props) {
super(props);
constructor(props) {
super(props);

this.sessionChanged = this.sessionChanged.bind(this);
this.sessionChanged = this.sessionChanged.bind(this);

this.state = {
redirectTo: null,
};
}
this.state = {
redirectTo: null,
};
}

async componentDidMount() {
// Listen for when the app wants to redirect to a specific URL
Store.subscribe(STOREKEYS.APP_REDIRECT_TO, (redirectTo) => {
this.setState({redirectTo});
});
async componentDidMount() {
// Listen for when the app wants to redirect to a specific URL
Store.subscribe(STOREKEYS.APP_REDIRECT_TO, (redirectTo) => {
this.setState({ redirectTo });
});

// Verify that our authToken is OK to use
verifyAuthToken();
// Verify that our authToken is OK to use
verifyAuthToken();

// Initialize this client as being an active client
await ActiveClientManager.init();
//TODO: Refactor window events
// window.addEventListener('beforeunload', () => {
// ActiveClientManager.removeClient();
// });
}
// Initialize this client as being an active client
await ActiveClientManager.init();
//TODO: Refactor window events
// window.addEventListener('beforeunload', () => {
// ActiveClientManager.removeClient();
// });
}

/**
* When the session changes, change which page the user sees
*
* @param {object} newSession
*/
sessionChanged(newSession) {
this.setState({isAuthTokenValid: newSession && newSession.authToken});
}
/**
* When the session changes, change which page the user sees
*
* @param {object} newSession
*/
sessionChanged(newSession) {
this.setState({ isAuthTokenValid: newSession && newSession.authToken });
}

render() {
return (
<Router>
{/* If there is ever a property for redirecting, we do the redirect here */}
{this.state.redirectTo && <Redirect to={this.state.redirectTo} />}
render() {
return (
<Router>
{/* If there is ever a property for redirecting, we do the redirect here */}
{this.state.redirectTo && <Redirect to={this.state.redirectTo} />}

<Switch>
<Route path="/signin" component={SignInPage} />
<Route path="/" component={HomePage} />
</Switch>
</Router>
);
}
<Switch>
<Route path="/signin" component={SignInPage} />
<Route path="/" component={HomePage} />
</Switch>
</Router>
);
}
}
4 changes: 2 additions & 2 deletions js/ROUTES.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
* This is a file containing constants for all of the routes we want to be able to go to
*/
export default {
SIGNIN: '/signin',
HOME: '/',
SIGNIN: '/signin',
HOME: '/',
};
8 changes: 4 additions & 4 deletions js/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const CONFIG = {
PUSHER: {
APP_KEY: '829fd8fd2a6036568469',
CLUSTER: 'us3',
},
PUSHER: {
APP_KEY: '829fd8fd2a6036568469',
CLUSTER: 'us3',
},
};
28 changes: 14 additions & 14 deletions js/lib/ActiveClientManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ const clientID = Guid();
* Add our client ID to the list of active IDs
*/
const init = async () => {
const activeClientIDs = (await Store.get(STOREKEYS.ACTIVE_CLIENT_IDS)) || [];
activeClientIDs.push(clientID);
Store.set(STOREKEYS.ACTIVE_CLIENT_IDS, activeClientIDs);
const activeClientIDs = (await Store.get(STOREKEYS.ACTIVE_CLIENT_IDS)) || [];
activeClientIDs.push(clientID);
Store.set(STOREKEYS.ACTIVE_CLIENT_IDS, activeClientIDs);
};

/**
* Remove this client ID from the array of active client IDs when this client is exited
*/
function removeClient() {
const activeClientIDs = Store.get(STOREKEYS.ACTIVE_CLIENT_IDS) || [];
const newActiveClientIDs = activeClientIDs.filter(
(activeClientID) => activeClientID !== clientID,
);
Store.set(STOREKEYS.ACTIVE_CLIENT_IDS, newActiveClientIDs);
const activeClientIDs = Store.get(STOREKEYS.ACTIVE_CLIENT_IDS) || [];
const newActiveClientIDs = activeClientIDs.filter(
(activeClientID) => activeClientID !== clientID,
);
Store.set(STOREKEYS.ACTIVE_CLIENT_IDS, newActiveClientIDs);
}

/**
Expand All @@ -30,11 +30,11 @@ function removeClient() {
* @returns {boolean}
*/
function isClientTheLeader() {
const activeClientIDs = Store.get(STOREKEYS.ACTIVE_CLIENT_IDS) || [];
if (!activeClientIDs.length) {
return false;
}
return activeClientIDs[0] === clientID;
const activeClientIDs = Store.get(STOREKEYS.ACTIVE_CLIENT_IDS) || [];
if (!activeClientIDs.length) {
return false;
}
return activeClientIDs[0] === clientID;
}

export {init, removeClient, isClientTheLeader};
export { init, removeClient, isClientTheLeader };
78 changes: 39 additions & 39 deletions js/lib/DateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Str from './Str.js';
* @private
*/
function getTimezone() {
return 'America/Los_Angeles';
return 'America/Los_Angeles';
}

/**
Expand All @@ -25,9 +25,9 @@ function getTimezone() {
* @private
*/
function getLocalMomentFromTimestamp(timestamp) {
// We need a default here for flows where we may not have initialized the TIME_ZONE NVP like generatng PDFs in printablereport.php
const timezone = getTimezone();
return moment.unix(timestamp).tz(timezone);
// We need a default here for flows where we may not have initialized the TIME_ZONE NVP like generatng PDFs in printablereport.php
const timezone = getTimezone();
return moment.unix(timestamp).tz(timezone);
}

/**
Expand All @@ -44,15 +44,15 @@ function getLocalMomentFromTimestamp(timestamp) {
* @returns {String}
*/
function timestampToDateTime(timestamp, includeTimeZone = false) {
const date = getLocalMomentFromTimestamp(timestamp);
let format =
moment().year() !== date.get('year')
? 'MMM D, YYYY [at] LT'
: 'MMM D [at] LT';
if (includeTimeZone) {
format = `${format} [UTC]Z`;
}
return date.format(format);
const date = getLocalMomentFromTimestamp(timestamp);
let format =
moment().year() !== date.get('year')
? 'MMM D, YYYY [at] LT'
: 'MMM D [at] LT';
if (includeTimeZone) {
format = `${format} [UTC]Z`;
}
return date.format(format);
}

/**
Expand All @@ -73,44 +73,44 @@ function timestampToDateTime(timestamp, includeTimeZone = false) {
* @returns {String}
*/
function timestampToRelative(timestamp) {
const date = getLocalMomentFromTimestamp(timestamp);
const durationFromLocalNow = moment.duration(
date.diff(getLocalMomentFromTimestamp(moment().unix())),
);
const round = (num) => Math.floor(Math.abs(num));
const date = getLocalMomentFromTimestamp(timestamp);
const durationFromLocalNow = moment.duration(
date.diff(getLocalMomentFromTimestamp(moment().unix())),
);
const round = (num) => Math.floor(Math.abs(num));

if (date.isAfter(moment().subtract(60, 'seconds'))) {
return '< 1 minute ago';
}
if (date.isAfter(moment().subtract(60, 'seconds'))) {
return '< 1 minute ago';
}

if (date.isAfter(moment().subtract(60, 'minutes'))) {
const minutes = round(durationFromLocalNow.asMinutes());
return `${minutes} ${Str.pluralize('minute', 'minutes', minutes)} ago`;
}
if (date.isAfter(moment().subtract(60, 'minutes'))) {
const minutes = round(durationFromLocalNow.asMinutes());
return `${minutes} ${Str.pluralize('minute', 'minutes', minutes)} ago`;
}

if (date.isAfter(moment().subtract(24, 'hours'))) {
const hours = round(durationFromLocalNow.asHours());
return `${hours} ${Str.pluralize('hour', 'hours', hours)} ago`;
}
if (date.isAfter(moment().subtract(24, 'hours'))) {
const hours = round(durationFromLocalNow.asHours());
return `${hours} ${Str.pluralize('hour', 'hours', hours)} ago`;
}

if (date.isAfter(moment().subtract(30, 'days'))) {
const days = round(durationFromLocalNow.asDays());
return `${days} ${Str.pluralize('day', 'days', days)} ago`;
}
if (date.isAfter(moment().subtract(30, 'days'))) {
const days = round(durationFromLocalNow.asDays());
return `${days} ${Str.pluralize('day', 'days', days)} ago`;
}

if (date.isAfter(moment().subtract(1, 'year'))) {
return date.format('MMM D');
}
if (date.isAfter(moment().subtract(1, 'year'))) {
return date.format('MMM D');
}

return date.format('MMM D, YYYY');
return date.format('MMM D, YYYY');
}

/**
* @namespace DateUtils
*/
const DateUtils = {
timestampToRelative,
timestampToDateTime,
timestampToRelative,
timestampToDateTime,
};

export default DateUtils;
Loading