Skip to content

Commit 957eff8

Browse files
committed
Simplify playground
1 parent a8f9310 commit 957eff8

19 files changed

+89
-185
lines changed

angular.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@
5858
"polyfills": ["zone.js"],
5959
"tsConfig": "tsconfig.app.json",
6060
"inlineStyleLanguage": "scss",
61-
"assets": ["playground/favicon.ico", "playground/assets"],
62-
"styles": ["playground/styles.scss"],
61+
"assets": ["playground/favicon.ico"],
6362
"scripts": []
6463
},
6564
"configurations": {

playground/app/app-routing.module.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

playground/app/app.component.html

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,50 @@
1-
<h1>Angular State</h1>
2-
<h2>Playground</h2>
3-
4-
<h3>Todos</h3>
5-
6-
<input
7-
type="text"
8-
placeholder="New Todo"
9-
#todoInput
10-
(keydown.enter)="addTodo(todoInput.value); todoInput.value = ''"
11-
/>
12-
13-
@if (uncompletedTodos$().length > 0) {
14-
<h4>Open</h4>
15-
16-
<ul>
17-
@for (todo of uncompletedTodos$(); track todo.id) {
18-
<li>
19-
<div>
20-
<span>{{ todo.description }}</span>
21-
<button (click)="completeTodo(todo)">Complete</button>
1+
<div
2+
class="h-100 w-full flex items-center justify-center bg-teal-lightest font-sans"
3+
>
4+
<div class="bg-white rounded shadow p-6 m-4 w-full lg:w-3/4 lg:max-w-lg">
5+
<div class="mb-4">
6+
<h1 class="text-grey-darkest font-bold">Todo List</h1>
7+
<div class="flex mt-4">
8+
<input
9+
class="shadow appearance-none border rounded w-full py-2 px-3 mr-4 text-grey-darker"
10+
placeholder="What needs to be done?"
11+
(keydown.enter)="addTodoButton.click()"
12+
#todoInput
13+
/>
14+
<button
15+
#addTodoButton
16+
class="flex-no-shrink p-2 border-2 rounded text-teal border-teal hover:text-[#50d71e] hover:bg-teal"
17+
(click)="addTodo(todoInput.value); todoInput.value = ''"
18+
>
19+
Add
20+
</button>
21+
</div>
22+
</div>
23+
<div>
24+
@for (todo of todos$(); track todo.id) {
25+
<div class="flex mb-4 items-center">
26+
<p
27+
class="w-full text-grey-darkest"
28+
[class.line-through]="todo.completed"
29+
>
30+
{{ todo.description }}
31+
</p>
32+
<button
33+
class="flex-no-shrink p-2 ml-4 mr-2 border-2 rounded hover:text-[#50d71e] text-green border-green hover:bg-green"
34+
(click)="toggleTodo(todo)"
35+
>
36+
{{ todo.completed ? 'Undo' : 'Done' }}
37+
</button>
38+
<button
39+
class="flex-no-shrink p-2 ml-2 border-2 rounded text-red border-red hover:text-[#50d71e] hover:bg-red"
40+
(click)="removeTodo(todo)"
41+
>
42+
Remove
43+
</button>
2244
</div>
23-
</li>
24-
} @empty {
25-
No uncompleted todos
26-
}
27-
</ul>
28-
}
29-
30-
@if (completedTodos$().length > 0) {
31-
<h4>Completed</h4>
32-
33-
<ul>
34-
@for (todo of completedTodos$(); track todo.id) {
35-
<li>
36-
<div>
37-
<span>{{ todo.description }}</span>
38-
<button (click)="completeTodo(todo)">Uncomplete</button>
39-
</div>
40-
</li>
41-
} @empty {
42-
No uncompleted todos
43-
}
44-
</ul>
45-
}
45+
} @empty {
46+
Nothing to do.
47+
}
48+
</div>
49+
</div>
50+
</div>

playground/app/app.component.scss

Whitespace-only changes.

playground/app/app.component.spec.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

playground/app/app.component.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,45 @@ import {
22
ChangeDetectionStrategy,
33
Component,
44
Signal,
5-
computed,
5+
effect,
66
} from '@angular/core';
7-
import { Todo, TodoStoreService } from './services/todo-store.service';
7+
import { StateDevToolsModule } from 'angular-reactive-state/dev-tools';
8+
import { Todo } from './todo';
9+
import { TodoStoreService } from './todo-store.service';
810

911
@Component({
1012
selector: 'app-root',
1113
templateUrl: './app.component.html',
12-
styleUrls: ['./app.component.scss'],
1314
changeDetection: ChangeDetectionStrategy.OnPush,
15+
imports: [StateDevToolsModule],
16+
standalone: true,
1417
})
1518
export class AppComponent {
16-
readonly completedTodos$: Signal<Todo[]>;
17-
readonly uncompletedTodos$: Signal<Todo[]>;
19+
readonly todos$: Signal<Todo[]>;
1820

1921
constructor(private todoStore: TodoStoreService) {
20-
const allTodos$ = this.todoStore.selectAsSignal(state => state.todos);
22+
this.todos$ = this.todoStore.selectAsSignal(state => state.todos);
2123

22-
this.completedTodos$ = computed(() =>
23-
allTodos$()?.filter(t => t.completed)
24-
);
25-
this.uncompletedTodos$ = computed(() =>
26-
allTodos$()?.filter(t => !t.completed)
27-
);
24+
effect(() => {
25+
this.todos$().sort((a, b) =>
26+
a.completed === b.completed ? 0 : a.completed ? 1 : -1
27+
);
28+
});
2829
}
2930

3031
addTodo(description: string) {
3132
this.todoStore.addTodo(description);
3233
}
3334

34-
completeTodo(todo: Todo) {
35-
this.todoStore.completeTodo(todo);
35+
removeTodo(todo: Todo) {
36+
this.todoStore.removeTodo(todo);
3637
}
3738

38-
uncompleteTodo(todo: Todo) {
39-
this.todoStore.uncompleteTodo(todo);
39+
toggleTodo(todo: Todo) {
40+
if (todo.completed) {
41+
this.todoStore.uncompleteTodo(todo);
42+
} else {
43+
this.todoStore.completeTodo(todo);
44+
}
4045
}
4146
}

playground/app/app.module.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

playground/app/services/todo-store.service.spec.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

playground/app/shared/shared.module.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

playground/app/shared/window.d.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)