Skip to content

Commit 06cf6f2

Browse files
committed
implemented lodash
1 parent 2dbdfa2 commit 06cf6f2

File tree

7 files changed

+41
-30
lines changed

7 files changed

+41
-30
lines changed

do-it/.angular-cli.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"styles": [
2222
"styles.css"
2323
],
24-
"scripts": [],
24+
"scripts": [
25+
"../node_modules/lodash/lodash.js"
26+
],
2527
"environmentSource": "environments/environment.ts",
2628
"environments": {
2729
"dev": "environments/environment.ts",

do-it/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@
2929
"@angular/cli": "1.0.0-rc.2",
3030
"@angular/compiler-cli": "^2.4.0",
3131
"@types/jasmine": "2.5.38",
32+
"@types/lodash": "^4.14.64",
3233
"@types/node": "~6.0.60",
3334
"codelyzer": "~2.0.0",
3435
"jasmine-core": "~2.5.2",
3536
"jasmine-spec-reporter": "~3.2.0",
3637
"karma": "~1.4.1",
3738
"karma-chrome-launcher": "~2.0.0",
3839
"karma-cli": "~1.0.1",
40+
"karma-coverage-istanbul-reporter": "^0.2.0",
3941
"karma-jasmine": "~1.1.0",
4042
"karma-jasmine-html-reporter": "^0.2.2",
41-
"karma-coverage-istanbul-reporter": "^0.2.0",
4243
"protractor": "~5.1.0",
4344
"ts-node": "~2.0.0",
4445
"tslint": "~4.5.0",

do-it/src/app/archive/archive.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
</div>
77
</form>
88

9-
<app-todo-card [todo]="item" *ngFor="let item of todoService.getArchiveList()"></app-todo-card>
9+
<app-todo-card [todo]="item" *ngFor="let item of archiveList"></app-todo-card>

do-it/src/app/archive/archive.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Component, OnInit } from '@angular/core';
22
import { TodoService } from '../todo.service';
33

4+
import {Todo } from '../models/todo';
5+
46
@Component({
57
selector: 'app-archive',
68
templateUrl: './archive.component.html',
@@ -10,7 +12,12 @@ export class ArchiveComponent implements OnInit {
1012

1113
constructor(private todoService: TodoService) { }
1214

15+
archiveList: Todo[];
1316
ngOnInit() {
17+
this.todoService.todoList$
18+
.subscribe(list => {
19+
this.archiveList = list.filter(todo => todo.isDone);
20+
})
1421
}
1522

1623
}

do-it/src/app/todo-card/todo-card.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
</span>
1212
</div>
1313
<div class="card-action">
14-
<a href="#">Trash this</a>
15-
<a href="#">Roll this back</a>
14+
<a (click)="delete(todo.id)">Trash this</a>
15+
<a (click)="revert(todo)">Roll this back</a>
1616
</div>
1717
</div>
1818

do-it/src/app/todo-card/todo-card.component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Component, OnInit, Input } from '@angular/core';
22

3+
import { TodoService } from '../todo.service';
34
import { Todo } from '../models/todo';
45

56
@Component({
@@ -10,9 +11,13 @@ import { Todo } from '../models/todo';
1011
export class TodoCardComponent implements OnInit {
1112

1213
@Input() todo: Todo;
13-
constructor() { }
14+
constructor(private todoService: TodoService) { }
1415

1516
ngOnInit() {
1617
}
1718

19+
delete(id) {
20+
this.todoService.deleteTodo(id);
21+
}
22+
1823
}

do-it/src/app/todo.service.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { BehaviorSubject, Observable } from 'rxjs';
33
import { Http } from '@angular/http';
44
import { Todo } from './models/todo';
55

6+
declare var _:any;
7+
68
@Injectable()
79
export class TodoService {
810

9-
todoList: Todo[] = [];
1011
baseURL: string = 'https://doit-5db57.firebaseio.com/todo';
1112

1213
private todoListSub: BehaviorSubject<Todo[]>;
@@ -19,7 +20,7 @@ export class TodoService {
1920
}
2021

2122
fetchAllTodos() {
22-
this.todoList = [];
23+
let todoList = [];
2324
return this.http.get(`${this.baseURL}.json`)
2425
.subscribe(data => {
2526
let resp = data.json();
@@ -29,30 +30,12 @@ export class TodoService {
2930
let todoRspObj = resp[key];
3031
let todoModel = new Todo(todoRspObj.title, todoRspObj.category, key,
3132
todoRspObj.isDone, todoRspObj.endDate);
32-
this.todoList.push(todoModel);
33+
todoList.push(todoModel);
3334
}
34-
this.todoListSub.next(this.todoList);
35-
console.log(this.todoList);
35+
this.todoListSub.next(todoList);
3636
});
3737
}
3838

39-
getProjectList() {
40-
return this.todoList
41-
.filter(todo => todo.category === 'project' && !todo.isDone);
42-
}
43-
44-
getPersonalList() {
45-
return this.todoList
46-
.filter(todo => todo.category === 'personal' && !todo.isDone);
47-
}
48-
49-
getArchiveList() {
50-
console.log(this.todoList
51-
.filter(todo => todo.isDone));
52-
return this.todoList
53-
.filter(todo => todo.isDone);
54-
}
55-
5639
addTaskToProjects(taskName: string) {
5740
this.postTask(new Todo(taskName, 'project'));
5841
}
@@ -64,15 +47,28 @@ export class TodoService {
6447
postTask(todo: Todo) {
6548
this.http.post(`${this.baseURL}.json`, todo)
6649
.subscribe(data => {
67-
console.log(data.json());
50+
// console.log(data.json());
6851
todo.id = data.json().name;
69-
this.todoList.push(todo);
52+
let todoList: Todo[] = this.todoListSub.getValue();
53+
todoList.push(todo);
54+
this.todoListSub.next(todoList);
7055
}, err => {
7156
console.log(err);
7257
})
7358

7459
}
7560

61+
deleteTodo(id: string) {
62+
this.http.delete(`${this.baseURL}/${id}.json`)
63+
.subscribe(res => {
64+
console.log(res.json());
65+
// Delete the Object
66+
let todoList: Todo[] = this.todoListSub.getValue();
67+
_.remove(todoList, todo => todo.id === id);
68+
this.todoListSub.next(todoList);
69+
})
70+
}
71+
7672
markTodoAsDone(todo: Todo) {
7773
// todo.isDone = true;
7874
// todo.endDate = Date.now();

0 commit comments

Comments
 (0)