Edit task-persistence service to handle task-lists.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CookieService } from 'ngx-cookie-service';
|
||||
import { Task } from '../entity/task';
|
||||
import { TaskList } from '../entity/taskList';
|
||||
|
||||
const COOKIE_NAME = 'todo-data';
|
||||
|
||||
@@ -12,17 +13,17 @@ export class TaskPersistenceService {
|
||||
private _cookieService: CookieService
|
||||
) {}
|
||||
|
||||
save(tasks: Task[]): void {
|
||||
this._cookieService.set(COOKIE_NAME, JSON.stringify(tasks));
|
||||
save(taskLists: TaskList[]): void {
|
||||
this._cookieService.set(COOKIE_NAME, JSON.stringify(taskLists));
|
||||
}
|
||||
|
||||
getAll(): Task[] {
|
||||
const serializedTasks = this._cookieService.get(COOKIE_NAME);
|
||||
let result: Task[] = [];
|
||||
getAll(): TaskList[] {
|
||||
const serializedTaskLists = this._cookieService.get(COOKIE_NAME);
|
||||
let result: TaskList[] = [];
|
||||
try {
|
||||
result = JSON.parse(serializedTasks);
|
||||
result = JSON.parse(serializedTaskLists);
|
||||
} catch(jsonParseError) {
|
||||
console.log(`Unable to parse tasks from cookie: ${serializedTasks}`);
|
||||
console.log(`Unable to parse task lists from cookie: ${serializedTaskLists}`);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ export class TaskService {
|
||||
constructor(
|
||||
private _taskPersistenceService: TaskPersistenceService
|
||||
) {
|
||||
const tasks = this._taskPersistenceService.getAll() || [];
|
||||
this._tasks.next(tasks);
|
||||
const taskLists = this._taskPersistenceService.getAll() || [];
|
||||
// this._tasks.next(taskLists);
|
||||
}
|
||||
|
||||
get tasks(): Observable<Task[]> {
|
||||
@@ -28,11 +28,11 @@ export class TaskService {
|
||||
}
|
||||
|
||||
private _saveTasks(): void {
|
||||
this._taskPersistenceService.save(this._tasks.value);
|
||||
// this._taskPersistenceService.save(this._tasks.value);
|
||||
}
|
||||
|
||||
refresh() {
|
||||
const tasks = this._taskPersistenceService.getAll();
|
||||
this._tasks.next(tasks);
|
||||
// this._tasks.next(tasks);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user