Skip to content

Commit

Permalink
feat: initial localStorage in app
Browse files Browse the repository at this point in the history
  • Loading branch information
Kleostro committed Aug 15, 2024
1 parent 6ac50a1 commit 7ce34b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { Component } from '@angular/core';
import { Component, inject, OnInit } from '@angular/core';
import { RouterOutlet } from '@angular/router';

import LocalStorageService from './core/services/local-storage.service';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrl: './app.component.scss',
standalone: true,
imports: [RouterOutlet],
providers: [LocalStorageService],
})
export class AppComponent {}
export class AppComponent implements OnInit {
private localStorageService = inject(LocalStorageService);

public ngOnInit(): void {
this.localStorageService.init();
}
}
4 changes: 2 additions & 2 deletions src/app/core/services/local-storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import LocalStorageData from '../models/store.model';
providedIn: 'root',
})
export default class LocalStorageService {
private storage: LocalStorageData = this.init();
private storage: LocalStorageData = {};

public getValueByKey(key: string): unknown {
if (key in this.storage) {
Expand Down Expand Up @@ -38,7 +38,7 @@ export default class LocalStorageService {
this.storage = this.init();
}

private init(): LocalStorageData {
public init(): LocalStorageData {
const storedData = localStorage.getItem(STORE_KEYS.LS_NAME);

const isLocalStorageData = (data: unknown): data is LocalStorageData => {
Expand Down

0 comments on commit 7ce34b3

Please sign in to comment.