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

[ACS-5859] Changing time-ago.pie from moment to date-fns. #8796

Merged
merged 1 commit into from
Aug 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('CommentListComponent', () => {
await fixture.whenStable();

element = fixture.nativeElement.querySelector('.adf-comment-message-time');
expect(element.innerText).toContain('a few seconds ago');
expect(element.innerText).toContain('less than a minute ago');
});

it('comment date time should start with Yesterday when comment date is yesterday', async () => {
Expand All @@ -130,7 +130,7 @@ describe('CommentListComponent', () => {
await fixture.whenStable();

element = fixture.nativeElement.querySelector('.adf-comment-message-time');
expect(element.innerText).toContain('a day ago');
expect(element.innerText).toContain('1 day ago');
});

it('comment date time should not start with Today/Yesterday when comment date is before yesterday', async () => {
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/lib/pipes/time-ago.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('TimeAgoPipe', () => {

it('should return time difference for a given date', () => {
const date = new Date();
expect(pipe.transform(date)).toBe('a few seconds ago');
expect(pipe.transform(date)).toBe('less than a minute ago');
});

it('should return exact date if given date is more than seven days ', () => {
Expand All @@ -61,7 +61,7 @@ describe('TimeAgoPipe', () => {
const date = new Date();
const transformedDate = pipe.transform(date, 'de');
/* cspell:disable-next-line */
expect(transformedDate).toBe('vor ein paar Sekunden');
expect(transformedDate).toBe('vor weniger als 1 Minute');
});
});
});
8 changes: 4 additions & 4 deletions lib/core/src/lib/pipes/time-ago.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
* limitations under the License.
*/

import moment from 'moment';
import { Pipe, PipeTransform, OnDestroy } from '@angular/core';
import { AppConfigService } from '../app-config/app-config.service';
import { UserPreferenceValues, UserPreferencesService } from '../common/services/user-preferences.service';
import { DatePipe } from '@angular/common';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { differenceInDays, formatDistance } from 'date-fns';
import * as Locales from 'date-fns/locale';

@Pipe({
name: 'adfTimeAgo'
Expand Down Expand Up @@ -50,13 +51,12 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy {
transform(value: Date, locale?: string) {
if (value !== null && value !== undefined ) {
const actualLocale = locale || this.defaultLocale;
const then = moment(value);
const diff = moment().locale(actualLocale).diff(then, 'days');
const diff = differenceInDays(new Date(), new Date(value));
if ( diff > 7) {
const datePipe: DatePipe = new DatePipe(actualLocale);
return datePipe.transform(value, this.defaultDateTimeFormat);
} else {
return then.locale(actualLocale).fromNow();
return formatDistance(new Date(value) , new Date(), { addSuffix: true , locale: Locales[actualLocale] });
}
}
return '';
Expand Down
Loading