Skip to content

Commit 5cd5e95

Browse files
committed
Add derived store
1 parent 62e8a56 commit 5cd5e95

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/App.svelte

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import AddTodoItem from './AddTodoItem.svelte'
44
import TodoItem from './TodoItem.svelte'
55
import { todoItems, mousePosition } from './store'
6+
import { todoStats } from './todoStatsStore'
67
7-
$: count = $todoItems.length;
8-
$: checkedCount = $todoItems.filter(({ checked }) => checked).length
9-
108
function handleAddTodoItem(event) {
119
todoItems.update(items => [...items, {
1210
id: uuid(),
@@ -29,11 +27,11 @@
2927
<p>{JSON.stringify($mousePosition, null, 2)}</p>
3028
<AddTodoItem
3129
title='Please type todo here:'
32-
buttonTitle={`Add (${checkedCount}/${count})`}
30+
buttonTitle={`Add (${$todoStats.doneCount}/${$todoStats.totalCount})`}
3331
on:add={handleAddTodoItem}
3432
/>
3533

36-
{#if count === 0}
34+
{#if $todoStats.totalCount === 0}
3735
No items yet
3836
{:else}
3937
<div>

src/todoStatsStore.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { derived } from 'svelte/store'
2+
import { todoItems } from './store'
3+
4+
export const todoStats = derived(
5+
[todoItems],
6+
([todoItems]) => {
7+
const doneCount = todoItems.filter(item => item.checked).length
8+
const totalCount = todoItems.length
9+
return {
10+
doneCount,
11+
totalCount,
12+
leftTodoCount: totalCount - doneCount
13+
}
14+
}
15+
)

0 commit comments

Comments
 (0)