Commit 5ff26dff authored by ZeHiro's avatar ZeHiro
Browse files

Uniformize display of dates. Fixes #159

parent c48da03d
Pipeline #13860 passed with stage
in 2 minutes and 45 seconds
......@@ -2,7 +2,8 @@ import React from 'react';
import { connect } from 'react-redux';
import { get, actions } from '../../store';
import { translate as $t } from '../../helpers';
import { translate as $t,
formatDate } from '../../helpers';
export default connect((state, ownProps) => {
let categoryA = get.categoryById(state, ownProps.a.categoryId);
......@@ -68,7 +69,7 @@ export default connect((state, ownProps) => {
<tbody>
<tr>
<td>{ props.a.date.toLocaleDateString() }</td>
<td>{ formatDate.toShortString(props.a.date) }</td>
<td>
{ props.a.title }
{ customLabelA }
......@@ -76,7 +77,7 @@ export default connect((state, ownProps) => {
<td>{ props.formatCurrency(props.a.amount) }</td>
<td>{ props.categoryA.title }</td>
<td>{ $t(`client.${props.a.type}`) }</td>
<td>{ new Date(props.a.dateImport).toLocaleString() }</td>
<td>{ formatDate.toLongString(props.a.dateImport) }</td>
<td rowSpan={ 2 }>
<button
className="btn btn-primary"
......@@ -90,7 +91,7 @@ export default connect((state, ownProps) => {
</tr>
<tr>
<td>{ props.b.date.toLocaleDateString() }</td>
<td>{ formatDate.toShortString(props.b.date) }</td>
<td>
{ props.b.title }
{ customLabelB }
......@@ -98,7 +99,7 @@ export default connect((state, ownProps) => {
<td>{ props.formatCurrency(props.b.amount) }</td>
<td>{ props.categoryB.title }</td>
<td>{ $t(`client.${props.b.type}`) }</td>
<td>{ new Date(props.b.dateImport).toLocaleString() }</td>
<td>{ formatDate.toLongString(props.b.dateImport) }</td>
</tr>
</tbody>
......
......@@ -4,7 +4,9 @@ import ReactDOM from 'react-dom';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import { translate as $t, wellsColors } from '../../helpers';
import { translate as $t,
wellsColors,
formatDate } from '../../helpers';
import { get } from '../../store';
......@@ -110,7 +112,7 @@ class OperationsComponent extends React.Component {
render() {
let asOf = $t('client.operations.as_of');
let lastCheckedDate = new Date(this.props.account.lastChecked).toLocaleDateString();
let lastCheckedDate = formatDate.toShortString(this.props.account.lastChecked);
let lastCheckDate = `${asOf} ${lastCheckedDate}`;
let wellOperations, filteredSub;
......
......@@ -3,7 +3,7 @@ import { connect } from 'react-redux';
import { actions } from '../../store';
import { translate as $t } from '../../helpers';
import { translate as $t, formatDate } from '../../helpers';
import { computeAttachmentLink } from './details';
import { OperationListViewLabel } from './label';
......@@ -78,7 +78,7 @@ let Operation = props => {
</a>
</td>
<td>
{ op.date.toLocaleDateString() }
{ formatDate.toShortString(op.date) }
</td>
<td className="hidden-xs">
{ typeSelect }
......
import React from 'react';
import { connect } from 'react-redux';
import { assertHas, translate as $t } from '../../helpers';
import { assertHas,
translate as $t,
formatDate } from '../../helpers';
import { actions } from '../../store';
export default connect(null, dispatch => {
......@@ -21,7 +23,7 @@ export default connect(null, dispatch => {
<span className="option-legend">
{ $t('client.operations.last_sync') }
&nbsp;
{ new Date(props.account.lastChecked).toLocaleString() }
{ formatDate.fromNow(props.account.lastChecked) }
</span>
<a
href="#"
......
......@@ -12,7 +12,9 @@ import { assert as assert_,
translate as translate_,
currency as currency_,
localeComparator as localeComparator_,
UNKNOWN_OPERATION_TYPE as UNKNOWN_OPERATION_TYPE_ } from '../shared/helpers.js';
UNKNOWN_OPERATION_TYPE as UNKNOWN_OPERATION_TYPE_,
formatDate as formatDate_
} from '../shared/helpers.js';
export const assert = assert_;
export const assertHas = assertHas_;
......@@ -23,6 +25,7 @@ export const translate = translate_;
export const localeComparator = localeComparator_;
export const currency = currency_;
export const UNKNOWN_OPERATION_TYPE = UNKNOWN_OPERATION_TYPE_;
export const formatDate = formatDate_;
export const AlertTypes = [
'balance',
......
......@@ -6,18 +6,19 @@ import {
setupTranslator as setupTranslator_,
translate as translate_,
currency as currency_,
UNKNOWN_OPERATION_TYPE as UNKNOWN_OPERATION_TYPE_
UNKNOWN_OPERATION_TYPE as UNKNOWN_OPERATION_TYPE_,
formatDate as formatDate_
} from './shared/helpers.js';
import errors from './shared/errors.json';
import moment from 'moment';
export const has = maybeHas_;
export const assert = assert_;
export const translate = translate_;
export const currency = currency_;
export const UNKNOWN_OPERATION_TYPE = UNKNOWN_OPERATION_TYPE_;
export const setupTranslator = setupTranslator_;
export const formatDate = formatDate_;
export function makeLogger(prefix) {
return printit({
......@@ -128,19 +129,6 @@ export function isCredentialError(err) {
err.errCode === getErrorCode('NO_PASSWORD');
}
export function setupTranslator(locale) {
setupTranslator_(locale);
if (locale) {
moment.locale(locale);
} else {
moment.locale('en');
}
}
export function formatDateToLocaleString(date) {
return moment(date).format('L');
}
// Minimum hour of the day at which the automatic poll can occur.
export const POLLER_START_LOW_HOUR = 2;
......
......@@ -3,7 +3,7 @@ import {
KError,
translate as $t,
currency,
formatDateToLocaleString,
formatDate,
POLLER_START_LOW_HOUR,
POLLER_START_HIGH_HOUR
} from '../helpers';
......@@ -147,7 +147,7 @@ class ReportManager {
default: log.error('unexpected frequency in getTextContent');
}
let today = formatDateToLocaleString();
let today = formatDate.toShortString();
let content;
content = $t('server.email.hello');
......@@ -156,7 +156,7 @@ class ReportManager {
content += '\n';
for (let account of accounts) {
let lastCheck = formatDateToLocaleString(account.lastCheck);
let lastCheck = formatDate.toShortString(account.lastCheck);
let balance = await account.computeBalance();
content += `\t* ${account.title} : `;
content += `${account.formatCurrency(balance)} (`;
......@@ -183,7 +183,7 @@ class ReportManager {
content += `\n${pair.account.title}:\n`;
for (let op of operations) {
let date = formatDateToLocaleString(op.date);
let date = formatDate.toShortString(op.date);
content += `\t* ${date} - ${op.title} : `;
content += `${pair.account.formatCurrency(op.amount)}\n`;
}
......
......@@ -5,7 +5,7 @@ import {
promisify,
promisifyModel,
translate as $t,
formatDateToLocaleString
formatDate
} from '../helpers';
let log = makeLogger('models/alert');
......@@ -113,7 +113,7 @@ Alert.prototype.formatOperationMessage = function(operation, accountName, format
$t('server.alert.operation.greaterThan');
let amount = formatCurrency(operation.amount);
let date = formatDateToLocaleString(operation.date);
let date = formatDate.toShortString(operation.date);
let limit = formatCurrency(this.limit);
return $t('server.alert.operation.content', {
......
/* eslint no-console: 0 */
// Locales
// It is necessary to load the locale files statically,
// otherwise the files are not inclued in the client
Please register or sign in to reply
const FR_LOCALE = require('./locales/fr');
const EN_LOCALE = require('./locales/en');
import Polyglot from 'node-polyglot';
import { format as currencyFormatter, findCurrency } from 'currency-formatter';
import moment from 'moment';
/* eslint import/no-unassigned-import: 0 */
/*
There is a bug when used with browserify :
http://momentjs.com/docs/#/use-it/browserify/
Then it is necessary to import the locales file.
*/
import 'moment/min/locales.min';
/* eslint import/no-unassigned-import: 1*/
const ASSERTS = true;
......@@ -42,8 +54,12 @@ let alertMissing = null;
export function setupTranslator(locale) {
let p = new Polyglot({ allowMissing: true });
// If no locale is defined, default to 'en';
let language = locale ? locale : 'en';
let found = true;
switch (locale) {
switch (language) {
case 'fr':
p.extend(FR_LOCALE);
break;
......@@ -51,17 +67,29 @@ export function setupTranslator(locale) {
p.extend(EN_LOCALE);
break;
default:
console.log("Didn't find locale", locale, 'using en-us instead.');
console.log("Didn't find locale", language, 'using en-us instead.');
found = false;
p.extend(EN_LOCALE);
break;
}
translator = p.t.bind(p);
appLocale = locale;
appLocale = language;
  • There's a small flow in the control-flow: if locale is defined, but with an unknown value, then language === locale === appLocale.

    How about:

    • renaming language to checkedLocale
    • checkedLocale = locale before the switch, and checkedLocale = 'en' in the default case.
    • then we can remove let language = locale ? locale : 'en';

    Thanks for doing this refactoring, by the way.

Please register or sign in to reply
alertMissing = found;
moment.locale(language);
}
const toShortString = date => moment(date).format('L');
const toLongString = date => moment(date).format('LLLL');
const fromNow = date => moment(date).calendar();
export const formatDate = {
toShortString,
toLongString,
fromNow
};
export function translate(format, bindings = {}) {
let augmentedBindings = bindings;
augmentedBindings._ = '';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment