-
Notifications
You must be signed in to change notification settings - Fork 37
Exercise solved - Sebastián Moreyra #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: services
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,37 @@ | ||
| import { Injectable } from '@angular/core'; | ||
| import { listLazyRoutes } from '@angular/compiler/src/aot/lazy_routes'; | ||
| import { TodoItem } from './model/todo-item'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root' | ||
| }) | ||
| export class LocalStorageService { | ||
|
|
||
| // https://developer.mozilla.org/es/docs/Web/API/Window/localStorage | ||
| constructor() { } | ||
| getName() { | ||
| return 'LocalStorageService' | ||
| } | ||
| getTasks() { | ||
| let storedList = localStorage.getItem("taskList"); | ||
| if (storedList === null) { | ||
| let tasks = []; | ||
| return tasks; | ||
| } | ||
| else { | ||
| let tasks = JSON.parse(localStorage.getItem('taskList')).map(each => new TodoItem(each)); | ||
| return tasks; | ||
| } | ||
| } | ||
|
|
||
| removeTask(list) { | ||
| localStorage.removeItem("tasklist"); | ||
| this.setTasks(list); | ||
| } | ||
|
|
||
| setTasks(list) { | ||
| localStorage.setItem("taskList",JSON.stringify(list)); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,46 @@ | ||
| import { Injectable } from '@angular/core'; | ||
| import { LocalStorageService } from './local-storage.service'; | ||
| import { TodoItem } from './model/todo-item'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root' | ||
| }) | ||
| export class TodoService { | ||
| export class TodoService{ | ||
|
|
||
|
|
||
| list = []; | ||
| list : TodoItem[] = []; | ||
| lastItemId = 0; | ||
|
|
||
| constructor(private storage: LocalStorageService) { } | ||
| constructor(private storage: LocalStorageService) { | ||
| this.list = this.storage.getTasks(); | ||
| console.log(this.list); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. saqquemos los console.log antes de entregar el ¨PR |
||
| } | ||
|
|
||
|
|
||
| add(task) { | ||
| this.lastItemId = this.getLastId(); | ||
| const id = this.lastItemId; | ||
| task.id = id; | ||
| this.list.push(task); | ||
| let taskAdded = new TodoItem(task); | ||
| this.list.push(taskAdded); | ||
| this.lastItemId += 10; | ||
| this.storage.setTasks(this.list); | ||
| console.log(this.storage.getTasks()); | ||
| } | ||
| getLastId() { | ||
| let lastId = 0; | ||
| this.list.forEach(e => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. evitemos totdo tipo de abreviaciones |
||
| if(e.id >= lastId) { | ||
| lastId = e.id; | ||
| } | ||
| }) | ||
| return lastId + 1; | ||
| } | ||
|
|
||
| remove(id) { | ||
| const index = this.list.findIndex((element) => element.id === id); | ||
| this.list.splice(index, 1); | ||
| this.storage.removeTask(this.list); | ||
| } | ||
|
|
||
| incompletedSize() { | ||
|
|
@@ -34,4 +54,10 @@ export class TodoService { | |
| getName() { | ||
| return 'TodoService 123' + this.storage.getName(); | ||
| } | ||
|
|
||
| changeState(item:TodoItem) { | ||
| const index = this.list.findIndex((element) => element.id === item.id); | ||
| this.list[index] = item; | ||
| this.storage.setTasks(this.list); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
usemos comillas simples, seamos consistentes con estos detalles