mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[HXCS-2818] CardViewDateItem component ignore timezone when displaying dates (#9304)
* [HXCS-2818] card-view-dateitem interpret dates as local, store them as utc * [HXCS-2818] refactoring --------- Co-authored-by: Adriano Costa <Adriano.Costa@hyland.comgit>
This commit is contained in:
@@ -114,17 +114,17 @@ export class CardViewComponent implements OnInit, OnDestroy {
|
|||||||
}),
|
}),
|
||||||
new CardViewDateItemModel({
|
new CardViewDateItemModel({
|
||||||
label: 'CardView Date Item',
|
label: 'CardView Date Item',
|
||||||
value: new Date(1983, 11, 24, 10, 0, 30),
|
value: new Date('1983-11-24T00:00:00Z'),
|
||||||
key: 'date',
|
key: 'date',
|
||||||
default: new Date(1983, 11, 24, 10, 0, 30),
|
default: new Date('1983-11-24T00:00:00Z'),
|
||||||
format: 'shortDate',
|
format: 'shortDate',
|
||||||
editable: this.isEditable
|
editable: this.isEditable
|
||||||
}),
|
}),
|
||||||
new CardViewDateItemModel({
|
new CardViewDateItemModel({
|
||||||
label: 'CardView Date Item - Multivalue (chips)',
|
label: 'CardView Date Item - Multivalue (chips)',
|
||||||
value: [new Date(1983, 11, 24, 10, 0, 30)],
|
value: [new Date('1983-11-24T00:00:00Z')],
|
||||||
key: 'date',
|
key: 'date',
|
||||||
default: new Date(1983, 11, 24, 10, 0, 30),
|
default: new Date('1983-11-24T00:00:00Z'),
|
||||||
format: 'shortDate',
|
format: 'shortDate',
|
||||||
editable: this.isEditable,
|
editable: this.isEditable,
|
||||||
multivalued: true
|
multivalued: true
|
||||||
|
@@ -27,6 +27,7 @@ import { TranslationService } from '../../../translation/translation.service';
|
|||||||
import { ADF_DATE_FORMATS, AdfDateFnsAdapter } from '../../../common/utils/date-fns-adapter';
|
import { ADF_DATE_FORMATS, AdfDateFnsAdapter } from '../../../common/utils/date-fns-adapter';
|
||||||
import { ADF_DATETIME_FORMATS, AdfDateTimeFnsAdapter } from '../../../common/utils/datetime-fns-adapter';
|
import { ADF_DATETIME_FORMATS, AdfDateTimeFnsAdapter } from '../../../common/utils/datetime-fns-adapter';
|
||||||
import { isValid } from 'date-fns';
|
import { isValid } from 'date-fns';
|
||||||
|
import { DateFnsUtils } from '../../../common/utils/date-fns-utils';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
providers: [
|
providers: [
|
||||||
@@ -99,10 +100,11 @@ export class CardViewDateItemComponent extends BaseCardView<CardViewDateItemMode
|
|||||||
if (event.value) {
|
if (event.value) {
|
||||||
if (isValid(event.value)) {
|
if (isValid(event.value)) {
|
||||||
this.property.value = new Date(event.value);
|
this.property.value = new Date(event.value);
|
||||||
|
this.valueDate = new Date(event.value);
|
||||||
if (this.property.type === 'date') {
|
if (this.property.type === 'date') {
|
||||||
this.property.value.setHours(0, 0, 0, 0);
|
this.property.value = DateFnsUtils.forceUtc(event.value);
|
||||||
|
this.valueDate = DateFnsUtils.forceLocal(event.value);
|
||||||
}
|
}
|
||||||
this.valueDate = event.value;
|
|
||||||
this.update();
|
this.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,9 +127,9 @@ export class CardViewDateItemComponent extends BaseCardView<CardViewDateItemMode
|
|||||||
addDateToList(event: MatDatetimepickerInputEvent<Date>) {
|
addDateToList(event: MatDatetimepickerInputEvent<Date>) {
|
||||||
if (event.value) {
|
if (event.value) {
|
||||||
if (isValid(event.value) && this.property.multivalued && Array.isArray(this.property.value)) {
|
if (isValid(event.value) && this.property.multivalued && Array.isArray(this.property.value)) {
|
||||||
const localDate = event.value;
|
let localDate = new Date(event.value);
|
||||||
if (this.property.type === 'date') {
|
if (this.property.type === 'date') {
|
||||||
localDate.setHours(0, 0, 0, 0);
|
localDate = DateFnsUtils.forceUtc(event.value);
|
||||||
}
|
}
|
||||||
this.property.value.push(localDate);
|
this.property.value.push(localDate);
|
||||||
this.update();
|
this.update();
|
||||||
@@ -148,11 +150,9 @@ export class CardViewDateItemComponent extends BaseCardView<CardViewDateItemMode
|
|||||||
|
|
||||||
private initSingleValueProperty() {
|
private initSingleValueProperty() {
|
||||||
if (this.property.value && !Array.isArray(this.property.value)) {
|
if (this.property.value && !Array.isArray(this.property.value)) {
|
||||||
this.property.value = new Date(this.property.value);
|
const date = new Date(this.property.value);
|
||||||
if (this.property.type === 'date') {
|
this.property.value = date;
|
||||||
this.property.value.setHours(0, 0, 0, 0);
|
this.valueDate = this.property.type === 'date' ? DateFnsUtils.forceLocal(date) : date;
|
||||||
}
|
|
||||||
this.valueDate = this.property.value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,14 +161,10 @@ export class CardViewDateItemComponent extends BaseCardView<CardViewDateItemMode
|
|||||||
this.property.value = [];
|
this.property.value = [];
|
||||||
}
|
}
|
||||||
if (Array.isArray(this.property.value) && this.property.value.length > 0) {
|
if (Array.isArray(this.property.value) && this.property.value.length > 0) {
|
||||||
this.property.value = this.property.value.map((date: Date) => {
|
this.property.value = this.property.value.map((date: Date | string) => new Date(date));
|
||||||
const localDate = new Date(date);
|
this.valueDate = this.property.type === 'date'
|
||||||
if (this.property.type === 'date') {
|
? DateFnsUtils.forceLocal(this.property.value[0])
|
||||||
localDate.setHours(0, 0, 0, 0);
|
: this.property.value[0];
|
||||||
}
|
|
||||||
return localDate;
|
|
||||||
});
|
|
||||||
this.valueDate = this.property.value[0];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -20,6 +20,7 @@ import { DynamicComponentModel } from '../../common/services/dynamic-component-m
|
|||||||
import { CardViewBaseItemModel } from './card-view-baseitem.model';
|
import { CardViewBaseItemModel } from './card-view-baseitem.model';
|
||||||
import { CardViewDateItemProperties } from '../interfaces/card-view.interfaces';
|
import { CardViewDateItemProperties } from '../interfaces/card-view.interfaces';
|
||||||
import { LocalizedDatePipe } from '../../pipes/localized-date.pipe';
|
import { LocalizedDatePipe } from '../../pipes/localized-date.pipe';
|
||||||
|
import { DateFnsUtils } from '../../common/utils/date-fns-utils';
|
||||||
|
|
||||||
type DateItemType = Date | Date[] | null;
|
type DateItemType = Date | Date[] | null;
|
||||||
|
|
||||||
@@ -40,18 +41,19 @@ export class CardViewDateItemModel extends CardViewBaseItemModel<DateItemType> i
|
|||||||
if (cardViewDateItemProperties.locale) {
|
if (cardViewDateItemProperties.locale) {
|
||||||
this.locale = cardViewDateItemProperties.locale;
|
this.locale = cardViewDateItemProperties.locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get displayValue(): string | string[] {
|
get displayValue(): string | string[] {
|
||||||
if (this.multivalued) {
|
if (this.multivalued) {
|
||||||
if (this.value && Array.isArray(this.value)) {
|
if (this.value && Array.isArray(this.value)) {
|
||||||
return this.value.map((date) => this.transformDate(date));
|
return this.value.map((date) => this.transformDate(this.prepareDate(date)));
|
||||||
} else {
|
} else {
|
||||||
return this.default ? [this.default] : [];
|
return this.default ? [this.default] : [];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return this.value && !Array.isArray(this.value) ? this.transformDate(this.value) : this.default;
|
return this.value && !Array.isArray(this.value)
|
||||||
|
? this.transformDate(this.prepareDate(this.value))
|
||||||
|
: this.default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,4 +61,8 @@ export class CardViewDateItemModel extends CardViewBaseItemModel<DateItemType> i
|
|||||||
this.localizedDatePipe = new LocalizedDatePipe();
|
this.localizedDatePipe = new LocalizedDatePipe();
|
||||||
return this.localizedDatePipe.transform(value, this.format, this.locale);
|
return this.localizedDatePipe.transform(value, this.format, this.locale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private prepareDate(date: Date): Date {
|
||||||
|
return this.type === 'date' ? DateFnsUtils.forceLocal(date) : date;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -15,9 +15,10 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { format, parse, parseISO, isValid, isBefore, isAfter } from 'date-fns';
|
import { format, parse, parseISO, isValid, isBefore, isAfter, lightFormat } from 'date-fns';
|
||||||
import { ar, cs, da, de, enUS, es, fi, fr, it, ja, nb, nl, pl, ptBR, ru, sv, zhCN } from 'date-fns/locale';
|
import { ar, cs, da, de, enUS, es, fi, fr, it, ja, nb, nl, pl, ptBR, ru, sv, zhCN } from 'date-fns/locale';
|
||||||
|
|
||||||
|
|
||||||
export class DateFnsUtils {
|
export class DateFnsUtils {
|
||||||
static getLocaleFromString(locale: string): Locale {
|
static getLocaleFromString(locale: string): Locale {
|
||||||
let dateFnsLocale: Locale;
|
let dateFnsLocale: Locale;
|
||||||
@@ -201,4 +202,13 @@ export class DateFnsUtils {
|
|||||||
static localToUtc(date: Date): Date {
|
static localToUtc(date: Date): Date {
|
||||||
return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
|
return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static forceLocal(date: Date): Date {
|
||||||
|
return new Date(lightFormat(date, 'yyyy-MM-dd'));
|
||||||
|
}
|
||||||
|
|
||||||
|
static forceUtc(date: Date): Date {
|
||||||
|
return new Date(lightFormat(date, 'yyyy-MM-dd').concat('T00:00:00.000Z'));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user