| 280 | |
| 281 | // Helper to create offline actions |
| 282 | export function createTodoActions( |
| 283 | offline: ReturnType<typeof createTodos>['executor'], |
| 284 | ) { |
| 285 | const addTodo = offline.createOfflineAction({ |
| 286 | mutationFnName: 'syncTodos', |
| 287 | onMutate: (text: string) => { |
| 288 | const now = new Date().toISOString() |
| 289 | const newTodo: Todo = { |
| 290 | id: crypto.randomUUID(), |
| 291 | text: text.trim(), |
| 292 | completed: false, |
| 293 | createdAt: now, |
| 294 | updatedAt: now, |
| 295 | } |
| 296 | collection.insert(newTodo) |
| 297 | return newTodo |
| 298 | }, |
| 299 | }) |
| 300 | |
| 301 | const toggleTodo = offline.createOfflineAction({ |
| 302 | mutationFnName: 'syncTodos', |
| 303 | onMutate: (id: string) => { |
| 304 | const todo = collection.get(id) |
| 305 | if (!todo) return |
| 306 | collection.update(id, (draft) => { |
| 307 | draft.completed = !draft.completed |
| 308 | draft.updatedAt = new Date().toISOString() |
| 309 | }) |
| 310 | return todo |
| 311 | }, |
| 312 | }) |
| 313 | |
| 314 | const deleteTodo = offline.createOfflineAction({ |
| 315 | mutationFnName: 'syncTodos', |
| 316 | onMutate: (id: string) => { |
| 317 | const todo = collection.get(id) |
| 318 | if (todo) { |
| 319 | collection.delete(id) |
| 320 | } |
| 321 | return todo |
| 322 | }, |
| 323 | }) |
| 324 | |
| 325 | return { addTodo, toggleTodo, deleteTodo } |
| 326 | } |