LocalStorage

Elrakjuk a tasks-t localStorage-be, konstruktorba vizsgáljuk hogy van-e, ha van akkor betoltjuk

import {NewTaskData} from './task/new-task.module';
import {Injectable} from '@angular/core';

@Injectable({providedIn: "root"})
export class TasksService {
  private tasks = [
    {
      id: 't1',
      userId: 'u1',
      title: "Vmi1",
      summary: "vmi summary 1",
      dueDate: '2025-12-31'
    },
    {
      id: 't2',
      userId: 'u1',
      title: "Vmi2",
      summary: "vmi summary 2",
      dueDate: '2025-12-31'
    },{
      id: 't3',
      userId: 'u1',
      title: "Vmi3",
      summary: "vmi summary 3",
      dueDate: '2025-12-31'
    },{
      id: 't4',
      userId: 'u1',
      title: "Vmi4",
      summary: "vmi summary 4",
      dueDate: '2025-12-31'
    }
  ];
  
  constructor() {
    const tasks = localStorage.getItem("tasks");
    if (tasks) {
      this.tasks = JSON.parse(tasks);
    }
  }

  getUserTasks(userId: string) {
    return this.tasks.filter((task => task.userId === userId));
  }

  addTask(taskData: NewTaskData, userId: string) {
    this.tasks.unshift({
      id: new Date().getTime().toString(),
      userId:userId,
      title: taskData.title,
      summary: taskData.summary,
      dueDate: taskData.date
    })
    this.saveTasks();
  }

  removeTask(id: string) {
    this.tasks = this.tasks.filter( (t) => t.id !== id); //Újraépíti a tömböt, a kapott id nélkül!!
    this.saveTasks();
  }
  
  private saveTasks() {
    localStorage.setItem('tasks', JSON.stringify(this.tasks));
  }
}