Skip to content

Commit 0ae06f7

Browse files
committed
🎉 implement storage
1 parent b327912 commit 0ae06f7

18 files changed

+58
-47
lines changed

src/app/add-todo/add-todo.component.css

Whitespace-only changes.

src/app/app.component.css

Whitespace-only changes.

src/app/app.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<section class="todoapp">
22
<header class="header">
33
<h1>todos</h1>
4-
<app-add-todo></app-add-todo>
4+
<app-new-todo></app-new-todo>
55
</header>
66
<router-outlet></router-outlet>
77
<app-footer></app-footer>

src/app/app.component.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
import { Component } from '@angular/core';
2+
import { Store } from '@ngrx/store';
3+
4+
import { AppState } from './../redux/app.reducer';
5+
import { Todo } from './../redux/todo/todo.model';
6+
import * as TodoActions from './../redux/todo/todo.actions';
27

38
@Component({
49
selector: 'app-root',
5-
templateUrl: './app.component.html',
6-
styleUrls: ['./app.component.css']
10+
templateUrl: './app.component.html'
711
})
812
export class AppComponent {
913

10-
constructor() {}
14+
constructor(
15+
private store: Store<AppState>,
16+
) {
17+
this.populateTodos();
18+
this.updateTodos();
19+
}
20+
21+
private populateTodos() {
22+
const todos: Todo[] = JSON.parse(localStorage.getItem('angular-todos') || '[]');
23+
this.store.dispatch(new TodoActions.PopulateTodosAction(todos));
24+
}
25+
26+
private updateTodos() {
27+
this.store.select('todos')
28+
.subscribe(todos => {
29+
localStorage.setItem('angular-todos', JSON.stringify(todos));
30+
});
31+
}
1132

1233
}

src/app/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { AppComponent } from './app.component';
1010
import { TodoComponent } from './todo/todo.component';
1111
import { TodoListComponent } from './todo-list/todo-list.component';
1212
import { FooterComponent } from './footer/footer.component';
13-
import { AddTodoComponent } from './add-todo/add-todo.component';
13+
import { NewTodoComponent } from './new-todo/new-todo.component';
1414

1515
const routes: Routes = [
1616
// basic routes
@@ -24,7 +24,7 @@ const routes: Routes = [
2424
TodoComponent,
2525
TodoListComponent,
2626
FooterComponent,
27-
AddTodoComponent
27+
NewTodoComponent
2828
],
2929
imports: [
3030
BrowserModule,

src/app/footer/footer.component.css

Whitespace-only changes.

src/app/footer/footer.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<footer class="footer" *ngIf="countTodos > 0">
1+
<footer class="footer" *ngIf="showFooter">
22
<span class="todo-count">{{ countTodos }} items left</span>
33
<ul class="filters">
44
<li>

src/app/footer/footer.component.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,35 @@ import { Store } from '@ngrx/store';
44
import { AppState } from './../../redux/app.reducer';
55
import * as FilterActions from './../../redux/filter/filter.actions';
66
import * as TodoActions from './../../redux/todo/todo.actions';
7-
import { getCountTodos, getStateCompleted } from './../../redux/todo/todo.selectors';
7+
import { getStateCompleted } from './../../redux/todo/todo.selectors';
88

99
@Component({
1010
selector: 'app-footer',
11-
templateUrl: './footer.component.html',
12-
styleUrls: ['./footer.component.css']
11+
templateUrl: './footer.component.html'
1312
})
1413
export class FooterComponent implements OnInit {
1514

1615
countTodos: number;
17-
stateCompleted: boolean;
1816
currentFilter: string;
17+
showFooter: boolean;
1918

2019
constructor(
2120
private store: Store<AppState>
2221
) {
23-
this.store.select(getCountTodos)
24-
.subscribe(count => {
25-
this.countTodos = count;
26-
});
27-
this.store.select(getStateCompleted)
28-
.subscribe(state => {
29-
this.stateCompleted = state;
22+
this.store.select('todos')
23+
.subscribe(todos => {
24+
this.countTodos = todos.filter(t => !t.completed).length;
25+
this.showFooter = todos.length > 0;
3026
});
3127
this.store.select('filter')
3228
.subscribe(fitler => {
3329
this.currentFilter = fitler;
3430
});
35-
3631
}
3732

3833
ngOnInit() {
3934
}
4035

41-
showAll() {
42-
this.store.dispatch(new FilterActions.SetFilterAction('SHOW_ALL'));
43-
}
44-
45-
showActive() {
46-
this.store.dispatch(new FilterActions.SetFilterAction('SHOW_ACTIVE'));
47-
}
48-
49-
showCompleted() {
50-
this.store.dispatch(new FilterActions.SetFilterAction('SHOW_COMPLETED'));
51-
}
52-
5336
clearCompleted() {
5437
this.store.dispatch(new TodoActions.ClearCompletedAction());
5538
}

0 commit comments

Comments
 (0)