From 7a56fec99a7a9ce9695b3b0fdb5ae61a962582d2 Mon Sep 17 00:00:00 2001 From: erdemcaygor Date: Tue, 8 Apr 2025 18:45:02 +0300 Subject: [PATCH] timezone service refactoring --- .../lib/interceptors/timezone.interceptor.ts | 4 +++- .../core/src/lib/services/timezone.service.ts | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/interceptors/timezone.interceptor.ts b/npm/ng-packs/packages/core/src/lib/interceptors/timezone.interceptor.ts index 1a6d7be8a3..aa6320f206 100644 --- a/npm/ng-packs/packages/core/src/lib/interceptors/timezone.interceptor.ts +++ b/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); diff --git a/npm/ng-packs/packages/core/src/lib/services/timezone.service.ts b/npm/ng-packs/packages/core/src/lib/services/timezone.service.ts index 5862b5be04..de112023d0 100644 --- a/npm/ng-packs/packages/core/src/lib/services/timezone.service.ts +++ b/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; } }