Browse Source

timezone service refactoring

pull/22619/head
erdemcaygor 1 year ago
parent
commit
7a56fec99a
  1. 4
      npm/ng-packs/packages/core/src/lib/interceptors/timezone.interceptor.ts
  2. 19
      npm/ng-packs/packages/core/src/lib/services/timezone.service.ts

4
npm/ng-packs/packages/core/src/lib/interceptors/timezone.interceptor.ts

@ -3,7 +3,9 @@ import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/c
import { TimezoneService } from '../services';
import { Observable } from 'rxjs';
@Injectable()
@Injectable({
providedIn: 'root',
})
export class TimezoneInterceptor implements HttpInterceptor {
protected readonly timezoneService = inject(TimezoneService);

19
npm/ng-packs/packages/core/src/lib/services/timezone.service.ts

@ -1,10 +1,21 @@
import { Injectable } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { ConfigStateService } from './config-state.service';
@Injectable({
providedIn: 'root',
})
export class TimezoneService {
protected readonly configState = inject(ConfigStateService);
protected readonly document = inject(DOCUMENT);
private readonly cookieKey = '__timezone';
private timeZoneNameFromSettings: string | null | undefined;
constructor() {
this.configState.getOne$('timing').subscribe(timezoneSettings => {
this.timeZoneNameFromSettings = timezoneSettings?.timeZone?.iana?.timeZoneName;
});
}
getBrowserTimezone(): string {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
@ -12,11 +23,11 @@ export class TimezoneService {
getTimezone(): string {
const fromCookie = this.getCookie(this.cookieKey);
return fromCookie || this.getBrowserTimezone();
return this.timeZoneNameFromSettings || fromCookie || this.getBrowserTimezone();
}
setTimezone(timezone: string): void {
document.cookie = `${this.cookieKey}=${timezone}; path=/`;
this.document.cookie = `${this.cookieKey}=${timezone}; path=/`;
}
convertUtcToLocal(date: string | Date): Date {
@ -28,7 +39,7 @@ export class TimezoneService {
}
private getCookie(name: string): string | null {
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
const match = this.document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
return match ? decodeURIComponent(match[2]) : null;
}
}

Loading…
Cancel
Save