From 0a6a83c83d548fb40b09ecf787eb455c8feaa170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=B6neberg?= Date: Thu, 28 May 2020 14:07:21 +0200 Subject: [PATCH] Add fixes for dates depending on the locale #6712 (#6715) (#6749) cherry-picked commit c388007feb18de1c5084058a99a544b7c8d8ccf1 --- frontend/src/components/filters/Filters.js | 8 ++-- frontend/src/components/table/Table.js | 3 ++ frontend/src/components/table/TableCell.js | 46 ++++++++++++++-------- frontend/src/components/table/TableItem.js | 5 +++ 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/frontend/src/components/filters/Filters.js b/frontend/src/components/filters/Filters.js index 2852b834cf3..dac69c81df2 100644 --- a/frontend/src/components/filters/Filters.js +++ b/frontend/src/components/filters/Filters.js @@ -476,12 +476,12 @@ export default class Filters extends PureComponent { : parameter && parameter.widgetType; const captionValue = activeParameter - ? TableCell.fieldValueToString( - activeParameter.valueTo + ? TableCell.fieldValueToString({ + fieldValue: activeParameter.valueTo ? [activeParameter.value, activeParameter.valueTo] : activeParameter.value, - filterType - ) + fieldType: filterType, + }) : ''; return { diff --git a/frontend/src/components/table/Table.js b/frontend/src/components/table/Table.js index 98b47e7a3a6..489d117ac8d 100644 --- a/frontend/src/components/table/Table.js +++ b/frontend/src/components/table/Table.js @@ -995,6 +995,7 @@ class Table extends Component { focusOnFieldName, modalVisible, isGerman, + activeLocale, } = this.props; const { @@ -1025,6 +1026,7 @@ class Table extends Component { {...item} {...{ entity, + activeLocale, cols, windowId, mainTable, @@ -1354,6 +1356,7 @@ const mapStateToProps = state => ({ allowShortcut: state.windowHandler.allowShortcut, allowOutsideClick: state.windowHandler.allowOutsideClick, modalVisible: state.windowHandler.modal.visible, + activeLocale: state.appHandler.me.language, isGerman: state.appHandler.me.language ? state.appHandler.me.language.key.includes('de') : false, diff --git a/frontend/src/components/table/TableCell.js b/frontend/src/components/table/TableCell.js index 2c602c258f8..60ec5351fff 100644 --- a/frontend/src/components/table/TableCell.js +++ b/frontend/src/components/table/TableCell.js @@ -27,13 +27,16 @@ class TableCell extends PureComponent { static getDateFormat = fieldType => DATE_FIELD_FORMATS[fieldType]; - static createDate = (fieldValue, fieldType) => { + static createDate = ({ fieldValue, fieldType, activeLocale }) => { + const languageKey = activeLocale ? activeLocale.key : null; if (fieldValue) { return !Moment.isMoment(fieldValue) && fieldValue.match(TIME_REGEX_TEST) - ? Moment.utc(Moment.duration(fieldValue).asMilliseconds()).format( - TIME_FORMAT - ) - : Moment(fieldValue).format(TableCell.getDateFormat(fieldType)); + ? Moment.utc(Moment.duration(fieldValue).asMilliseconds()) + .locale(languageKey) + .format(TIME_FORMAT) + : Moment(fieldValue) + .locale(languageKey) + .format(TableCell.getDateFormat(fieldType)); } return ''; @@ -82,12 +85,13 @@ class TableCell extends PureComponent { // @TODO: THIS NEEDS URGENT REFACTORING, WHY THE HECK ARE WE RETURNING // SIX DIFFERENT TYPES OF VALUES HERE ? UBER-BAD DESIGN ! - static fieldValueToString = ( + static fieldValueToString = ({ fieldValue, fieldType = 'Text', precision = null, - isGerman - ) => { + isGerman, + activeLocale, + }) => { if (fieldValue === null) { return ''; } @@ -102,7 +106,7 @@ class TableCell extends PureComponent { return DATE_FIELD_TYPES.includes(fieldType) || TIME_FIELD_TYPES.includes(fieldType) - ? TableCell.createDate(fieldValue, fieldType) + ? TableCell.createDate({ fieldValue, fieldType, activeLocale }) : fieldValue.caption; } case 'boolean': { @@ -117,7 +121,7 @@ class TableCell extends PureComponent { DATE_FIELD_TYPES.includes(fieldType) || TIME_FIELD_TYPES.includes(fieldType) ) { - return TableCell.createDate(fieldValue, fieldType); + return TableCell.createDate({ fieldValue, fieldType, activeLocale }); } else if (AMOUNT_FIELD_TYPES.includes(fieldType)) { return TableCell.createAmount(fieldValue, precision, isGerman); } else if (SPECIAL_FIELD_TYPES.includes(fieldType)) { @@ -258,18 +262,19 @@ class TableCell extends PureComponent { modalVisible, onClickOutside, isGerman, + activeLocale, } = this.props; const widgetData = getWidgetData(item, isEditable, supportFieldEdit); - const docId = `${this.props.docId}`; const { tooltipToggled } = this.state; const tdValue = !isEdited - ? TableCell.fieldValueToString( - widgetData[0].value, - item.widgetType, - widgetData[0].precision, - isGerman - ) + ? TableCell.fieldValueToString({ + fieldValue: widgetData[0].value, + fieldType: item.widgetType, + precision: widgetData[0].precision, + isGerman, + activeLocale, + }) : null; const description = widgetData[0].value && widgetData[0].value.description @@ -407,6 +412,13 @@ TableCell.propTypes = { onCellExtend: PropTypes.func, isEdited: PropTypes.bool, isGerman: PropTypes.bool, + entity: PropTypes.any, + getSizeClass: PropTypes.func, + mainTable: PropTypes.bool, + viewId: PropTypes.string, + modalVisible: PropTypes.bool, + docId: PropTypes.any, + activeLocale: PropTypes.object, }; export default TableCell; diff --git a/frontend/src/components/table/TableItem.js b/frontend/src/components/table/TableItem.js index b88095e77af..f34dde06a90 100644 --- a/frontend/src/components/table/TableItem.js +++ b/frontend/src/components/table/TableItem.js @@ -363,6 +363,7 @@ class TableItem extends PureComponent { isGerman, isSelected, focusOnFieldName, + activeLocale, } = this.props; const { edited, @@ -407,6 +408,7 @@ class TableItem extends PureComponent { return (