feat: add done counter

This commit is contained in:
MAZE 2024-09-01 13:00:10 +04:30
parent c6cc61a17f
commit aa8161aac5
2 changed files with 11 additions and 2 deletions

View file

@ -6,13 +6,16 @@ import styles from './todos.module.css';
export function Todos() { export function Todos() {
const todos = useTodoStore(state => state.todos); const todos = useTodoStore(state => state.todos);
const doneCount = useTodoStore(state => state.doneCount());
return ( return (
<div className={styles.todos}> <div className={styles.todos}>
<header> <header>
<p className={styles.label}>Your Todos</p> <p className={styles.label}>Your Todos</p>
<div className={styles.divider} /> <div className={styles.divider} />
<p className={styles.counter}>0 / 0</p> <p className={styles.counter}>
{doneCount} / {todos.length}
</p>
</header> </header>
{todos.length > 0 ? ( {todos.length > 0 ? (

View file

@ -6,6 +6,7 @@ import { v4 as uuid } from 'uuid';
interface TodoStore { interface TodoStore {
addTodo: (todo: string) => void; addTodo: (todo: string) => void;
deleteTodo: (id: string) => void; deleteTodo: (id: string) => void;
doneCount: () => number;
editTodo: (id: string, newTodo: string) => void; editTodo: (id: string, newTodo: string) => void;
todos: Array<{ todos: Array<{
createdAt: number; createdAt: number;
@ -32,13 +33,18 @@ export const useTodoStore = create<TodoStore>()(
], ],
}); });
}, },
deleteTodo(id) { deleteTodo(id) {
set({ set({
todos: get().todos.filter(todo => todo.id !== id), todos: get().todos.filter(todo => todo.id !== id),
}); });
}, },
doneCount() {
const { todos } = get();
return todos.filter(todo => todo.done).length;
},
editTodo(id, newTodo) { editTodo(id, newTodo) {
set({ set({
todos: get().todos.map(todo => { todos: get().todos.map(todo => {