Example: a todo app
A complete todo app with a collection store for todos and a single-entity store for filters, in three files you can copy.
Applies to zenty v1.1.0
A todo list with categories, priorities and filters. It uses both store types: a collection store for the todos and a single-entity store for the current filter settings. The three files compile as shown.
Types(link to this section)
export interface Todo {
id: number;
title: string;
completed: boolean;
category: 'work' | 'personal' | 'shopping';
priority: 'low' | 'medium' | 'high';
dueDate?: Date;
createdAt: Date;
}
export interface TodoFilters {
category: string;
showCompleted: boolean;
priority: string;
}Stores(link to this section)
Two calls. Todo has a numeric id, so no idKey is needed. The filter store starts with an initialState, which makes it loaded from the start.
import { createEntitiesStore, createEntityStore } from 'zenty';
import type { Todo, TodoFilters } from '@/types/todo';
// All todos with full CRUD operations
export const useTodoStore = createEntitiesStore<Todo>();
// Filter settings
export const useFilterStore = createEntityStore<TodoFilters>({
initialState: {
category: 'all',
showCompleted: true,
priority: 'all'
}
});Component(link to this section)
Everything below the store setup is UI you'd write anyway. TodoForm and TodoFilterBar are your own components and not part of Zenty.
import { useTodoStore, useFilterStore } from '@/stores/todo';
import type { Todo } from '@/types/todo';
// Your own form and filter UI, not part of Zenty
import { TodoForm } from './TodoForm';
import { TodoFilterBar } from './TodoFilterBar';
export function TodoApp() {
const {
entities: todos,
add,
update,
delete: deleteTodo,
loading,
error
} = useTodoStore();
const {
entity: filters,
update: updateFilters
} = useFilterStore();
// Add new todo
const handleAddTodo = (title: string, category: Todo['category']) => {
add({
id: Date.now(),
title,
completed: false,
category,
priority: 'medium',
createdAt: new Date()
});
};
// Toggle completion
const handleToggleComplete = (id: number) => {
const todo = todos.find(t => t.id === id);
if (todo) {
update(id, { completed: !todo.completed });
}
};
// Filter todos based on current filters (entity is TodoFilters | null)
const filteredTodos = todos.filter(todo => {
if (!filters) return true;
if (filters.category !== 'all' && todo.category !== filters.category) return false;
if (!filters.showCompleted && todo.completed) return false;
if (filters.priority !== 'all' && todo.priority !== filters.priority) return false;
return true;
});
// Statistics
const stats = {
total: todos.length,
completed: todos.filter(t => t.completed).length,
pending: todos.filter(t => !t.completed).length,
highPriority: todos.filter(t => t.priority === 'high' && !t.completed).length
};
return (
<div className="max-w-4xl mx-auto p-6">
<header className="mb-8">
<h1 className="text-3xl font-bold mb-2">My Todo App</h1>
<div className="flex gap-4 text-sm text-muted-foreground">
<span>Total: {stats.total}</span>
<span>Completed: {stats.completed}</span>
<span>Pending: {stats.pending}</span>
<span className="text-red-500">High Priority: {stats.highPriority}</span>
</div>
</header>
{/* Add Todo Form */}
<TodoForm onAdd={handleAddTodo} />
{/* Filters */}
<TodoFilterBar
filters={filters}
onUpdateFilters={updateFilters}
/>
{/* Todo List */}
{loading && <div>Loading todos...</div>}
{error && <div className="text-red-500">Error: {error}</div>}
<div className="space-y-2">
{filteredTodos.map(todo => (
<div key={todo.id} className="flex items-center gap-3 p-3 border rounded">
<input
type="checkbox"
checked={todo.completed}
onChange={() => handleToggleComplete(todo.id)}
/>
<span className={todo.completed ? 'line-through' : ''}>
{todo.title}
</span>
<button
onClick={() => deleteTodo(todo.id)}
className="ml-auto text-red-500"
>
Delete
</button>
</div>
))}
</div>
{filteredTodos.length === 0 && (
<div className="text-center py-12 text-muted-foreground">
No todos found. Add some tasks to get started!
</div>
)}
</div>
);
}add,updateanddeletecome straight from the store, typed againstTodo.filtersisTodoFilters | nullbecause an entity store can be empty. The filter code handles that case.
Found a mistake? Open an issue.