refactor: route localStorage access through shared LocalStorageService
Sprint 2 high-priority cleanup: cart/language/location services called localStorage directly, bypassing the try/catch safety and core/<domain> pattern used elsewhere (e.g. ProjectEditorDraftStorageService). New core/storage/LocalStorageService centralizes get/set/remove and JSON helpers with private-mode/quota error handling, reused across all three. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ import { Injectable, signal, computed } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Region, GeoIpResponse } from '../models/location.model';
|
||||
import { ApiConfigService } from '../core/config/api-config.service';
|
||||
import { LocalStorageService } from '../core/storage/local-storage.service';
|
||||
|
||||
const STORAGE_KEY = 'selected_region';
|
||||
|
||||
@@ -28,7 +29,8 @@ export class LocationService {
|
||||
|
||||
constructor(
|
||||
private readonly http: HttpClient,
|
||||
private readonly apiConfig: ApiConfigService
|
||||
private readonly apiConfig: ApiConfigService,
|
||||
private readonly storage: LocalStorageService
|
||||
) {
|
||||
this.loadRegions();
|
||||
this.restoreFromStorage();
|
||||
@@ -55,13 +57,13 @@ export class LocationService {
|
||||
/** Set region by user choice */
|
||||
setRegion(region: Region): void {
|
||||
this.regionSignal.set(region);
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(region));
|
||||
this.storage.setJSON(STORAGE_KEY, region);
|
||||
}
|
||||
|
||||
/** Clear region (go global) */
|
||||
clearRegion(): void {
|
||||
this.regionSignal.set(null);
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
this.storage.removeItem(STORAGE_KEY);
|
||||
}
|
||||
|
||||
/** Auto-detect user location via IP geolocation */
|
||||
@@ -111,14 +113,9 @@ export class LocationService {
|
||||
|
||||
/** Restore previously selected region from storage */
|
||||
private restoreFromStorage(): void {
|
||||
try {
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
if (stored) {
|
||||
const region: Region = JSON.parse(stored);
|
||||
this.regionSignal.set(region);
|
||||
}
|
||||
} catch {
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
const region = this.storage.getJSON<Region>(STORAGE_KEY);
|
||||
if (region) {
|
||||
this.regionSignal.set(region);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user