Skip to content
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

Migrate to Ngrx Singal Store #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@angular/platform-browser": "18.2.5",
"@angular/platform-browser-dynamic": "18.2.5",
"@angular/router": "18.2.5",
"@ngrx/signals": "^18.0.2",
"rxjs": "7.5.0",
"tslib": "2.3.0",
"zone.js": "0.14.10"
Expand Down Expand Up @@ -49,4 +50,4 @@
"karma-jasmine-html-reporter": "1.7.0",
"typescript": "5.4.5"
}
}
}
10 changes: 6 additions & 4 deletions src/app/components/clear-btn/clear-btn.component.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
@if (completedTodos$ | async; as todos) {
@if (todos.length > 0) {
@let completedTodos = store.completedTodos();


@if (completedTodos.length > 0) {
<button
type="button"
(click)="clear()"
(click)="store.clearCompleted()"
class="clear-completed"
>
Clear completed
</button>
}
}

14 changes: 2 additions & 12 deletions src/app/components/clear-btn/clear-btn.component.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { AsyncPipe } from '@angular/common';

import { TodoService } from '@services/todo.service';
import { TodosStore } from '@services/todos.store';

@Component({
standalone: true,
imports: [AsyncPipe],
selector: 'app-clear-btn',
templateUrl: './clear-btn.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ClearBtnComponent {
private todoService = inject(TodoService);


completedTodos$ = this.todoService.getCompletedTodos();

clear() {
this.todoService.clearCompleted();
}

readonly store = inject(TodosStore);
}
23 changes: 12 additions & 11 deletions src/app/components/counter/counter.component.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
@if (pendingTodos$ | async; as pendingTodos) {
<span class="todo-count">
<strong>{{ pendingTodos.length }} </strong>
@if (pendingTodos.length === 1) {
<span>item </span>
} @else {
items
}
left
</span>
}
@let pendingTodos = store.pendingTodos();

<span class="todo-count">
<strong>{{ pendingTodos.length }} </strong>
@if (pendingTodos.length === 1) {
<span>item </span>
} @else {
items
}
left
</span>

8 changes: 2 additions & 6 deletions src/app/components/counter/counter.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { AsyncPipe } from '@angular/common';

import { TodoService } from '@services/todo.service';
import { TodosStore } from '@services/todos.store';

@Component({
standalone: true,
Expand All @@ -11,9 +11,5 @@ import { TodoService } from '@services/todo.service';
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CounterComponent{
private todoService = inject(TodoService);


pendingTodos$ = this.todoService.getPendingTodos();

readonly store = inject(TodosStore);
}
41 changes: 21 additions & 20 deletions src/app/components/footer/footer.component.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
@if (todos$ | async; as todos) {
@if (todos.length > 0) {
<footer class="footer">
<app-counter></app-counter>
@if (filter$ | async; as filter) {
<ul class="filters">
<li>
<a routerLink="/" [class.selected]="filter === 'all'">All</a>
@let todos = store.todos();

@if (todos.length > 0) {
<footer class="footer">
<app-counter />
@let filter = store.filter();

<ul class="filters">
<li>
<a routerLink="/" [class.selected]="filter === 'all'">All</a>
</li>
<li>
<a routerLink="/pending" [class.selected]="filter === 'pending'"
>Pending</a
>
</li>
<li>
<a routerLink="/pending" [class.selected]="filter === 'pending'"
>Pending</a
<a routerLink="/completed" [class.selected]="filter === 'completed'"
>Completed</a
>
</li>
<li>
<a routerLink="/completed" [class.selected]="filter === 'completed'"
>Completed</a
>
</li>
</ul>
}
<app-clear-btn></app-clear-btn>
</footer>
}
</ul>

<app-clear-btn />
</footer>
}
12 changes: 3 additions & 9 deletions src/app/components/footer/footer.component.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { Component, ChangeDetectionStrategy, inject } from '@angular/core';
import { RouterLink } from '@angular/router';
import { AsyncPipe } from '@angular/common';

import { TodoService } from '@services/todo.service';
import { TodosStore } from '@services/todos.store';
import { CounterComponent } from '@components/counter/counter.component';
import { ClearBtnComponent } from '@components/clear-btn/clear-btn.component';

@Component({
standalone: true,
imports: [AsyncPipe, CounterComponent, ClearBtnComponent, RouterLink],
imports: [CounterComponent, ClearBtnComponent, RouterLink],
selector: 'app-footer',
templateUrl: './footer.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FooterComponent {
private todoService = inject(TodoService);


todos$ = this.todoService.getTodos();
filter$ = this.todoService.getFilter();

readonly store = inject(TodosStore);
}
6 changes: 3 additions & 3 deletions src/app/components/header/header.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, ChangeDetectionStrategy, inject } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';

import { TodoService } from '@services/todo.service';
import { TodosStore } from '@services/todos.store';

@Component({
standalone: true,
Expand All @@ -11,14 +11,14 @@ import { TodoService } from '@services/todo.service';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class HeaderComponent {
private todoService = inject(TodoService);
readonly store = inject(TodosStore);

input = new FormControl('', { nonNullable: true });

addTodo() {
const title = this.input.value.trim();
if (title !== '') {
this.todoService.add(title);
this.store.add(title);
this.input.setValue('');
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/app/components/todo/todo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NgClass } from '@angular/common';


import { Todo } from '@models/todo.model';
import { TodoService } from '@services/todo.service';
import { TodosStore } from '@services/todos.store';

@Component({
standalone: true,
Expand All @@ -14,7 +14,7 @@ import { TodoService } from '@services/todo.service';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TodoComponent {
private todoService = inject(TodoService);
readonly store = inject(TodosStore);
private cdRef = inject(ChangeDetectorRef);

_todo!: Todo;
Expand All @@ -28,18 +28,18 @@ export class TodoComponent {
@ViewChild('inputElement') inputElement!: ElementRef<HTMLInputElement>;

toggle() {
this.todoService.toggle(this._todo.id);
this.store.toggle(this._todo.id);
}

update() {
const title = this.input.value.trim();
if (title !== '') {
this.todoService.update(this._todo.id, { title });
this.store.update(this._todo.id, { title });
}
}

remove() {
this.todoService.remove(this._todo.id);
this.store.remove(this._todo.id);
}

escape() {
Expand Down
20 changes: 10 additions & 10 deletions src/app/components/todos/todos.component.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@if (todos$ | async; as todos) {
@if (todos.length > 0) {
<section class="main">
<ul class="todo-list">
@for (todo of todos; track todo) {
<app-todo [todo]="todo"></app-todo>
}
</ul>
</section>
}
@let todos = store.visibleTodos();
@if (todos.length > 0) {
<section class="main">
<ul class="todo-list">
@for (todo of todos; track todo.id) {
<app-todo [todo]="todo" />
}
</ul>
</section>
}

13 changes: 5 additions & 8 deletions src/app/components/todos/todos.component.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
import { Component, OnInit, ChangeDetectionStrategy, inject } from '@angular/core';
import { AsyncPipe } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
import { Filter } from '@models/filter.model';
import { TodoComponent } from '@components/todo/todo.component';

import { TodoService } from '@services/todo.service';
import { TodosStore } from '@services/todos.store';

@Component({
standalone: true,
imports: [TodoComponent, AsyncPipe],
imports: [TodoComponent],
selector: 'app-todos',
templateUrl: './todos.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TodosComponent implements OnInit {
private todoService = inject(TodoService);
readonly store = inject(TodosStore);
private route = inject(ActivatedRoute);

todos$ = this.todoService.getTodosByFilter();

constructor() {
this.route.paramMap.subscribe((params) => {
const filter = params.get('filter') as Filter;
this.todoService.changeFilter(filter || 'all');
this.store.changeFilter(filter || 'all');
});
}

ngOnInit(): void {
this.todoService.readStorage();
// this.store.readStorage();
}
}
6 changes: 3 additions & 3 deletions src/app/pages/home/home.component.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<section class="todoapp">
<app-header></app-header>
<app-header />
<div class="container todoapp-wrapper">
<app-todos></app-todos>
<app-footer></app-footer>
<app-todos />
<app-footer />
</div>
</section>
Loading