From fe8057fae0f8f41936caa54c218ce104162c3a7a Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Fri, 20 Aug 2021 22:21:45 +0200 Subject: [PATCH] refactoring date/date-pipe --- .../apps/goofy/src/app/app.component.spec.ts | 4 +++ ...mentar-list-item-in-vorgang.component.html | 2 +- ...tar-list-item-in-vorgang.component.spec.ts | 7 +++- .../lib/build-info/build-info.component.html | 2 +- .../build-info/build-info.component.spec.ts | 13 +++++-- .../incomming-mail.component.html | 2 +- .../incomming-mail.component.spec.ts | 11 ++++-- .../outgoing-mail.component.html | 2 +- .../outgoing-mail.component.spec.ts | 7 +++- .../tech-shared/src/lib/date.util.spec.ts | 34 +++++++++++++++++++ .../libs/tech-shared/src/lib/date.util.ts | 24 ++++++++----- .../lib/pipe/format-date-with-time.pipe.ts | 8 +++-- .../src/lib/pipe/format-list-date.pipe.ts | 9 ++--- .../wiedervorlage.formservice.ts | 4 +-- 14 files changed, 100 insertions(+), 29 deletions(-) create mode 100644 goofy-client/libs/tech-shared/src/lib/date.util.spec.ts diff --git a/goofy-client/apps/goofy/src/app/app.component.spec.ts b/goofy-client/apps/goofy/src/app/app.component.spec.ts index cf7103954e..a9e762566f 100644 --- a/goofy-client/apps/goofy/src/app/app.component.spec.ts +++ b/goofy-client/apps/goofy/src/app/app.component.spec.ts @@ -1,4 +1,6 @@ +import { registerLocaleData } from '@angular/common'; import { HttpClient } from '@angular/common/http'; +import localeDe from '@angular/common/locales/de'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { ApiRootResource, ApiRootService } from '@goofy-client/api-root-shared'; @@ -13,6 +15,8 @@ import { createApiRootResource } from 'libs/api-root-shared/test/api-root'; import { BehaviorSubject, of } from 'rxjs'; import { AppComponent } from './app.component'; +registerLocaleData(localeDe); + describe('AppComponent', () => { let component: AppComponent; let fixture: ComponentFixture<AppComponent>; diff --git a/goofy-client/libs/kommentar/src/lib/kommentar-list-in-vorgang-container/kommentar-list-in-vorgang/kommentar-list-item-in-vorgang/kommentar-list-item-in-vorgang.component.html b/goofy-client/libs/kommentar/src/lib/kommentar-list-in-vorgang-container/kommentar-list-in-vorgang/kommentar-list-item-in-vorgang/kommentar-list-item-in-vorgang.component.html index 88d1c978fe..13b6283332 100644 --- a/goofy-client/libs/kommentar/src/lib/kommentar-list-in-vorgang-container/kommentar-list-in-vorgang/kommentar-list-item-in-vorgang/kommentar-list-item-in-vorgang.component.html +++ b/goofy-client/libs/kommentar/src/lib/kommentar-list-in-vorgang-container/kommentar-list-in-vorgang/kommentar-list-item-in-vorgang/kommentar-list-item-in-vorgang.component.html @@ -3,7 +3,7 @@ class="plain-text"> <goofy-client-user-profile-in-kommentar-container [kommentar]="kommentar" data-test-id="kommentar-list-item-user-profile"></goofy-client-user-profile-in-kommentar-container> - <span data-test-id="kommentar-created-at">{{ kommentar.createdAt | date: 'dd.MM.yy HH:mm' }}</span> + <span data-test-id="kommentar-created-at">{{ kommentar.createdAt | formatDateWithTimePipe: false }}</span> <p>{{ kommentar.text }}</p> </button> diff --git a/goofy-client/libs/kommentar/src/lib/kommentar-list-in-vorgang-container/kommentar-list-in-vorgang/kommentar-list-item-in-vorgang/kommentar-list-item-in-vorgang.component.spec.ts b/goofy-client/libs/kommentar/src/lib/kommentar-list-in-vorgang-container/kommentar-list-in-vorgang/kommentar-list-item-in-vorgang/kommentar-list-item-in-vorgang.component.spec.ts index c31047b981..58df951e0b 100644 --- a/goofy-client/libs/kommentar/src/lib/kommentar-list-in-vorgang-container/kommentar-list-in-vorgang/kommentar-list-item-in-vorgang/kommentar-list-item-in-vorgang.component.spec.ts +++ b/goofy-client/libs/kommentar/src/lib/kommentar-list-in-vorgang-container/kommentar-list-in-vorgang/kommentar-list-item-in-vorgang/kommentar-list-item-in-vorgang.component.spec.ts @@ -1,11 +1,15 @@ +import { registerLocaleData } from '@angular/common'; +import localeDe from '@angular/common/locales/de'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { ConvertForDataTestPipe } from '@goofy-client/tech-shared'; +import { ConvertForDataTestPipe, FormatDateWithTimePipe } from '@goofy-client/tech-shared'; import { UserProfileInKommentarContainerComponent } from '@goofy-client/user-profile'; import { createKommentarResource } from 'libs/kommentar-shared/test/kommentar'; import { MockComponent } from 'ng-mocks'; import { KommentarFormComponent } from '../../kommentar-form/kommentar-form.component'; import { KommentarListItemInVorgangComponent } from './kommentar-list-item-in-vorgang.component'; +registerLocaleData(localeDe); + describe('KommentarListItemInVorgangComponent', () => { let component: KommentarListItemInVorgangComponent; let fixture: ComponentFixture<KommentarListItemInVorgangComponent>; @@ -15,6 +19,7 @@ describe('KommentarListItemInVorgangComponent', () => { declarations: [ KommentarListItemInVorgangComponent, ConvertForDataTestPipe, + FormatDateWithTimePipe, MockComponent(KommentarFormComponent), MockComponent(UserProfileInKommentarContainerComponent) ] diff --git a/goofy-client/libs/navigation/src/lib/build-info/build-info.component.html b/goofy-client/libs/navigation/src/lib/build-info/build-info.component.html index e676042ec8..f4d9c6b132 100644 --- a/goofy-client/libs/navigation/src/lib/build-info/build-info.component.html +++ b/goofy-client/libs/navigation/src/lib/build-info/build-info.component.html @@ -1 +1 @@ -<p>Version: {{ apiRoot.version }} | BuildTime: {{ apiRoot.buildTime | date: "dd.MM.yy HH:mm:ss" }}</p> \ No newline at end of file +<p>Version: {{ apiRoot.version }} | BuildTime: {{ apiRoot.buildTime | formatDateWithTimePipe }}</p> \ No newline at end of file diff --git a/goofy-client/libs/navigation/src/lib/build-info/build-info.component.spec.ts b/goofy-client/libs/navigation/src/lib/build-info/build-info.component.spec.ts index fa9f152a0e..3d07136ba9 100644 --- a/goofy-client/libs/navigation/src/lib/build-info/build-info.component.spec.ts +++ b/goofy-client/libs/navigation/src/lib/build-info/build-info.component.spec.ts @@ -1,20 +1,29 @@ +import { registerLocaleData } from '@angular/common'; +import localeDe from '@angular/common/locales/de'; import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { FormatDateWithTimePipe } from '@goofy-client/tech-shared'; +import { createApiRootResource } from 'libs/api-root-shared/test/api-root'; import { BuildInfoComponent } from './build-info.component'; +registerLocaleData(localeDe); + describe('BuildInfoComponent', () => { let component: BuildInfoComponent; let fixture: ComponentFixture<BuildInfoComponent>; beforeEach(async () => { await TestBed.configureTestingModule({ - declarations: [BuildInfoComponent] + declarations: [ + BuildInfoComponent, + FormatDateWithTimePipe + ] }).compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(BuildInfoComponent); component = fixture.componentInstance; - component.apiRoot = { version: '1', buildTime: '2', javaVersion: '3', _links: null }; + component.apiRoot = createApiRootResource(); fixture.detectChanges(); }); diff --git a/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/incomming-mail/incomming-mail.component.html b/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/incomming-mail/incomming-mail.component.html index 916e039acd..7a6249fa21 100644 --- a/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/incomming-mail/incomming-mail.component.html +++ b/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/incomming-mail/incomming-mail.component.html @@ -4,5 +4,5 @@ <span class="overflow">{{ postfachMail.subject }}</span> </div> -<div class="date" data-test-id="mail-created-at">{{ postfachMail.createdAt | date: "dd.MM.yy HH:mm" }}</div> +<div class="date" data-test-id="mail-created-at">{{ postfachMail.createdAt | formatDateWithTimePipe: false }}</div> <div class="message overflow" data-test-id="mail-text">{{ postfachMail.mailBody }}</div> \ No newline at end of file diff --git a/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/incomming-mail/incomming-mail.component.spec.ts b/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/incomming-mail/incomming-mail.component.spec.ts index 7595569289..87b1ae6a7a 100644 --- a/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/incomming-mail/incomming-mail.component.spec.ts +++ b/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/incomming-mail/incomming-mail.component.spec.ts @@ -1,8 +1,13 @@ +import { registerLocaleData } from '@angular/common'; +import localeDe from '@angular/common/locales/de'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { MatIcon } from '@angular/material/icon'; +import { FormatDateWithTimePipe } from '@goofy-client/tech-shared'; import { createPostfachMailResource } from '../../../../../../../postfach-shared/test/postfach'; import { IncommingMailComponent } from './incomming-mail.component'; +registerLocaleData(localeDe); + describe('IncommingMailComponent', () => { let component: IncommingMailComponent; let fixture: ComponentFixture<IncommingMailComponent>; @@ -11,10 +16,10 @@ describe('IncommingMailComponent', () => { await TestBed.configureTestingModule({ declarations: [ IncommingMailComponent, - MatIcon + MatIcon, + FormatDateWithTimePipe ] - }) - .compileComponents(); + }).compileComponents(); }); beforeEach(() => { diff --git a/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/outgoing-mail/outgoing-mail.component.html b/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/outgoing-mail/outgoing-mail.component.html index 529d962f7f..acadd13da2 100644 --- a/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/outgoing-mail/outgoing-mail.component.html +++ b/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/outgoing-mail/outgoing-mail.component.html @@ -3,5 +3,5 @@ </goofy-client-user-profile-in-postfach-mail-container> <span class="overflow subject" data-test-id="mail-subject">{{ postfachMail.subject }}</span> -<div class="date" data-test-id="mail-created-at">{{ postfachMail.createdAt | date: "dd.MM.yy HH:mm" }}</div> +<div class="date" data-test-id="mail-created-at">{{ postfachMail.createdAt | formatDateWithTimePipe: false }}</div> <div class="message overflow" data-test-id="mail-text">{{ postfachMail.mailBody }}</div> \ No newline at end of file diff --git a/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/outgoing-mail/outgoing-mail.component.spec.ts b/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/outgoing-mail/outgoing-mail.component.spec.ts index 6c0e75a1a6..aca1a5935d 100644 --- a/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/outgoing-mail/outgoing-mail.component.spec.ts +++ b/goofy-client/libs/postfach/src/lib/postfach-mail-list-container/postfach-mail-list/postfach-mail/outgoing-mail/outgoing-mail.component.spec.ts @@ -1,10 +1,14 @@ +import { registerLocaleData } from '@angular/common'; +import localeDe from '@angular/common/locales/de'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { HasLinkPipe } from '@goofy-client/tech-shared'; +import { FormatDateWithTimePipe, HasLinkPipe } from '@goofy-client/tech-shared'; import { UserProfileInPostfachMailContainerComponent } from '@goofy-client/user-profile'; import { MockComponent } from 'ng-mocks'; import { createPostfachMailResource } from '../../../../../../../postfach-shared/test/postfach'; import { OutgoingMailComponent } from './outgoing-mail.component'; +registerLocaleData(localeDe); + describe('OutgoingMailComponent', () => { let component: OutgoingMailComponent; let fixture: ComponentFixture<OutgoingMailComponent>; @@ -14,6 +18,7 @@ describe('OutgoingMailComponent', () => { declarations: [ OutgoingMailComponent, HasLinkPipe, + FormatDateWithTimePipe, MockComponent(UserProfileInPostfachMailContainerComponent) ] }) diff --git a/goofy-client/libs/tech-shared/src/lib/date.util.spec.ts b/goofy-client/libs/tech-shared/src/lib/date.util.spec.ts new file mode 100644 index 0000000000..9cec0f0821 --- /dev/null +++ b/goofy-client/libs/tech-shared/src/lib/date.util.spec.ts @@ -0,0 +1,34 @@ +import { registerLocaleData } from '@angular/common'; +import localeDe from '@angular/common/locales/de'; +import { formatFullDate, formatFullDateWithTimeAndDay, formatFullDateWithTimeWithoutSeconds, formatWithoutYear } from './date.util'; + +registerLocaleData(localeDe); + +describe('Date Util', () => { + + const dateToFormat: Date = new Date('1010-01-01:01:01:01'); + + it('formatFullDateWithTimeWithoutSeconds', () => { + const formattedDate: string = formatFullDateWithTimeWithoutSeconds(dateToFormat); + + expect(formattedDate).toEqual('01.01.10 01:01'); + }) + + it('formatFullDateWithTimeAndDay', () => { + const formattedDate: string = formatFullDateWithTimeAndDay(dateToFormat); + + expect(formattedDate).toEqual('Montag, 01.01.1010, 01:01:01'); + }) + + it('formatFullDate', () => { + const formattedDate: string = formatFullDate(dateToFormat); + + expect(formattedDate).toEqual('01.01.1010'); + }) + + it('formatWithoutYear', () => { + const formattedDate: string = formatWithoutYear(dateToFormat); + + expect(formattedDate).toEqual('1.Jan.'); + }) +}) \ No newline at end of file diff --git a/goofy-client/libs/tech-shared/src/lib/date.util.ts b/goofy-client/libs/tech-shared/src/lib/date.util.ts index 44e1672aaf..243da7d685 100644 --- a/goofy-client/libs/tech-shared/src/lib/date.util.ts +++ b/goofy-client/libs/tech-shared/src/lib/date.util.ts @@ -2,7 +2,7 @@ import { formatDate } from '@angular/common'; import { isNil } from 'lodash-es'; import * as moment from 'moment'; -export function format(date: Date | moment.Moment): string { +export function formatForDatabase(date: Date | moment.Moment): string { const dateFormat: string = 'YYYY-MM-DD'; if (isNil(date)) { @@ -15,22 +15,26 @@ export function format(date: Date | moment.Moment): string { } } -export function formatWithTimeAndDay(date: Date): string { - return formatDate(date, 'EEEE, dd.MM.y, H:mm:ss', 'de'); -} -export function formatWithTime(date: Date): string { - return formatDate(date, 'dd.MM.y, H:mm:ss', 'de'); + +export function formatFullDateWithTimeWithoutSeconds(date: Date): string { + return formatDate(date, 'dd.MM.yy HH:mm', 'de'); } -export function formatForThisYear(date: Date): string { - return formatDate(date, 'd.MMM', 'de'); +export function formatFullDateWithTimeAndDay(date: Date): string { + return formatDate(date, 'EEEE, dd.MM.yyyy, HH:mm:ss', 'de'); } -export function formatDMYDate(date: Date): string { +export function formatFullDate(date: Date): string { return formatDate(date, 'dd.MM.yyyy', 'de'); } +export function formatWithoutYear(date: Date): string { + return formatDate(date, 'd.MMM', 'de'); +} + + + export function isToday(date: Date): boolean { return moment().isSame(date, 'day'); } @@ -59,6 +63,8 @@ export function isThisYear(date: Date): boolean { return moment().isSame(date, 'year') } + + export function createFutureDate(todayPlusDays: number): Date { const date = new Date(); date.setDate(date.getDate() + todayPlusDays); diff --git a/goofy-client/libs/tech-shared/src/lib/pipe/format-date-with-time.pipe.ts b/goofy-client/libs/tech-shared/src/lib/pipe/format-date-with-time.pipe.ts index ff2511896a..a278705ca8 100644 --- a/goofy-client/libs/tech-shared/src/lib/pipe/format-date-with-time.pipe.ts +++ b/goofy-client/libs/tech-shared/src/lib/pipe/format-date-with-time.pipe.ts @@ -1,11 +1,13 @@ import { Pipe, PipeTransform } from '@angular/core'; import { isNil } from 'lodash-es'; -import { formatWithTimeAndDay } from '../date.util'; +import { formatFullDateWithTimeAndDay, formatFullDateWithTimeWithoutSeconds } from '../date.util'; @Pipe({ name: 'formatDateWithTimePipe' }) export class FormatDateWithTimePipe implements PipeTransform { - transform(date: Date): string { + transform(date: Date, withSeconds: boolean = true): string { if (isNil(date)) return undefined; - return formatWithTimeAndDay(date); + return withSeconds + ? formatFullDateWithTimeAndDay(date) + : formatFullDateWithTimeWithoutSeconds(date); } } \ No newline at end of file diff --git a/goofy-client/libs/tech-shared/src/lib/pipe/format-list-date.pipe.ts b/goofy-client/libs/tech-shared/src/lib/pipe/format-list-date.pipe.ts index dbb515fe1f..ae1e7f4f2f 100644 --- a/goofy-client/libs/tech-shared/src/lib/pipe/format-list-date.pipe.ts +++ b/goofy-client/libs/tech-shared/src/lib/pipe/format-list-date.pipe.ts @@ -1,9 +1,10 @@ import { Pipe, PipeTransform } from '@angular/core'; -import { formatDMYDate, formatForThisYear, isDayAfterTomorrow, isDayBeforeYesterday, isThisYear, isToday, isTomorrow, isYesterday } from '../date.util'; +import { formatFullDate, formatWithoutYear, isDayAfterTomorrow, isDayBeforeYesterday, isThisYear, isToday, isTomorrow, isYesterday } from '../date.util'; +//TODO: rename @Pipe({ name: 'formatListDate' }) export class FormatListDatePipe implements PipeTransform { - transform(date: Date) { + transform(date: Date): string { if (isToday(date)) return 'Heute'; if (isYesterday(date)) return 'Gestern'; if (isDayBeforeYesterday(date)) return 'Vorgestern'; @@ -11,9 +12,9 @@ export class FormatListDatePipe implements PipeTransform { if (isDayAfterTomorrow(date)) return 'Übermorgen'; if (isThisYear(date)) { - return formatForThisYear(date) + return formatWithoutYear(date) } - return formatDMYDate(date); + return formatFullDate(date); } } \ No newline at end of file diff --git a/goofy-client/libs/wiedervorlage/src/lib/wiedervorlage-page-container/wiedervorlage-page/wiedervorlage-form/wiedervorlage.formservice.ts b/goofy-client/libs/wiedervorlage/src/lib/wiedervorlage-page-container/wiedervorlage-page/wiedervorlage-form/wiedervorlage.formservice.ts index 787bf584ae..2421e3233f 100644 --- a/goofy-client/libs/wiedervorlage/src/lib/wiedervorlage-page-container/wiedervorlage-page/wiedervorlage-form/wiedervorlage.formservice.ts +++ b/goofy-client/libs/wiedervorlage/src/lib/wiedervorlage-page-container/wiedervorlage-page/wiedervorlage-form/wiedervorlage.formservice.ts @@ -1,7 +1,7 @@ import { Injectable, OnDestroy } from '@angular/core'; import { FormBuilder, FormControl, FormGroup } from '@angular/forms'; import { OzgFileListLinkRel, OzgFileListResource, OzgFileResource } from '@goofy-client/ozg-file-shared'; -import { AbstractFormService, createFutureDate, createStateResource, format, StateResource } from '@goofy-client/tech-shared'; +import { AbstractFormService, createFutureDate, createStateResource, formatForDatabase, StateResource } from '@goofy-client/tech-shared'; import { Wiedervorlage, WiedervorlageLinkRel, WiedervorlageResource, WiedervorlageService } from '@goofy-client/wiedervorlage-shared'; import { getEmbeddedResource, getUrl, hasLink } from '@ngxp/rest'; import { isNil, isNull } from 'lodash-es'; @@ -75,7 +75,7 @@ export class WiedervorlageFormService extends AbstractFormService implements OnD getValue(): Wiedervorlage { const formValue = super.getFormValue(); - return { ...formValue, frist: format(formValue.frist), attachments: this.attachments.value.resource.map(ozgFile => getUrl(ozgFile)) }; + return { ...formValue, frist: formatForDatabase(formValue.frist), attachments: this.attachments.value.resource.map(ozgFile => getUrl(ozgFile)) }; } public getAttachments(): Observable<StateResource<OzgFileResource[]>> { -- GitLab