Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

app.ts

183 lines6.6 KB

1import { Component, signal } from '@angular/core'
2import { injectLiveQuery } from '@tanstack/angular-db'
3import { eq } from '@tanstack/db'
4import { todosCollection } from '../collections/todos-collection'
5import { FormsModule } from '@angular/forms'
6import { CommonModule } from '@angular/common'
7​
8@Component({
9 selector: 'app-root',
10 standalone: true,
11 imports: [FormsModule, CommonModule],
12 template: `
13 <div class="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100">
14 <div class="container mx-auto px-4 py-8 max-w-2xl">
15 <!-- Header -->
16 <div class="text-center mb-8">
17 <h1 class="text-4xl font-bold text-gray-800 mb-2">Todo App</h1>
18 <p class="text-gray-600">Stay organized and productive</p>
19 </div>
20​
21 <!-- Project Selector -->
22 <div class="bg-white rounded-xl shadow-lg p-6 mb-6">
23 <label for="project" class="block text-sm font-medium text-gray-700 mb-2">
24 Select project
25 </label>
26 <select
27 id="project"
28 class="w-full px-4 py-2 border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all"
29 [ngModel]="selectedProjectId()"
30 (ngModelChange)="selectedProjectId.set($event)"
31 name="project"
32 >
33 <option *ngFor="let p of projects" [ngValue]="p.id">
34 {{ p.name }}
35 </option>
36 </select>
37 </div>
38​
39 <!-- Add Todo Form -->
40 <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
41 <form (ngSubmit)="addTodo()" class="flex gap-3">
42 <input
43 [(ngModel)]="newTodoText"
44 name="newTodo"
45 type="text"
46 placeholder="What needs to be done?"
47 class="flex-1 px-4 py-3 border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all"
48 required
49 />
50 <button
51 type="submit"
52 [disabled]="!newTodoText.trim()"
53 class="px-6 py-3 bg-blue-600 text-white font-medium rounded-lg hover:bg-blue-700 disabled:bg-gray-300 disabled:cursor-not-allowed transition-all"
54 >
55 Add
56 </button>
57 </form>
58 </div>
59​
60 <!-- Todos List -->
61 <div class="bg-white rounded-xl shadow-lg overflow-hidden">
62 @if (todoQuery.isReady()) {
63 @if (todoQuery.data().length === 0) {
64 <div class="p-8 text-center text-gray-500">
65 <div class="text-6xl mb-4">📝</div>
66 <p class="text-lg">No todos yet. Add one above to get started!</p>
67 </div>
68 } @else {
69 <div class="divide-y divide-gray-100">
70 @for (todo of todoQuery.data(); track todo.id) {
71 <div class="p-4 flex items-center gap-4 hover:bg-gray-50 transition-colors">
72 <!-- Complete Button -->
73 <button
74 (click)="toggleTodo(todo.id)"
75 [class]="
76 todo.completed
77 ? 'w-6 h-6 rounded-full bg-blue-600 border-2 border-blue-600 flex items-center justify-center text-white'
78 : 'w-6 h-6 rounded-full border-2 border-gray-300 hover:border-blue-500 transition-colors'
79 "
80 >
81 @if (todo.completed) {
82 <svg class="w-3 h-3" fill="currentColor" viewBox="0 0 20 20">
83 <path
84 fill-rule="evenodd"
85 d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
86 clip-rule="evenodd"
87 />
88 </svg>
89 }
90 </button>
91​
92 <!-- Todo Text -->
93 <span
94 [class]="
95 todo.completed
96 ? 'flex-1 text-gray-500 line-through'
97 : 'flex-1 text-gray-800'
98 "
99 >
100 {{ todo.text }}
101 </span>
102​
103 <!-- Delete Button -->
104 <button
105 (click)="deleteTodo(todo.id)"
106 class="p-2 text-gray-400 hover:text-red-500 hover:bg-red-50 rounded-lg transition-all"
107 >
108 <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
109 <path
110 stroke-linecap="round"
111 stroke-linejoin="round"
112 stroke-width="2"
113 d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
114 />
115 </svg>
116 </button>
117 </div>
118 }
119 </div>
120 }
121​
122 <!-- Stats -->
123 <div class="bg-gray-50 px-4 py-3 text-sm text-gray-600 border-t">
124 {{ getCompletedCount() }} of {{ todoQuery.data().length }} completed
125 </div>
126 } @else {
127 <div class="p-8 text-center">
128 <div
129 class="inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"
130 ></div>
131 <p class="mt-2 text-gray-500">Loading todos...</p>
132 </div>
133 }
134 </div>
135 </div>
136 </div>
137 `,
138})
139export class App {
140 projects = [
141 { id: 1, name: 'Work' },
142 { id: 2, name: 'Home' },
143 ]
144​
145 selectedProjectId = signal(2)
146​
147 todoQuery = injectLiveQuery({
148 params: () => ({ projectID: this.selectedProjectId() }),
149 query: ({ params, q }) =>
150 q.from({ todo: todosCollection }).where(({ todo }) => eq(todo.projectID, params.projectID)),
151 })
152​
153 newTodoText = ''
154​
155 addTodo() {
156 if (!this.newTodoText.trim()) return
157​
158 const newTodo = {
159 id: Date.now(),
160 text: this.newTodoText.trim(),
161 projectID: this.selectedProjectId(),
162 completed: false,
163 created_at: new Date(),
164 }
165​
166 todosCollection.insert(newTodo)
167 this.newTodoText = ''
168 }
169​
170 toggleTodo(id: number) {
171 todosCollection.update(id, (draft: any) => {
172 draft.completed = !draft.completed
173 })
174 }
175​
176 deleteTodo(id: number) {
177 todosCollection.delete(id)
178 }
179​
180 getCompletedCount(): number {
181 return this.todoQuery.data().filter((todo) => todo.completed).length
182 }