Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 38 ยท Projects and Practice

Project: A Todo App with Filters

Build a complete todo app with Pinia: a setup store with state, getters and actions, filters, a derived counter, an action log and tests for the store itself.

Time to put the pieces together. In this project you build a todo app where all the logic lives in one Pinia store and the component is only a thin view. You will use state, getters, actions, $subscribe and $onAction, and you will test the store without any component, the way the testing chapter taught.

What we build

  • Add, toggle, edit and delete todos.
  • Filters: all, active, completed.
  • A counter of remaining items and a "clear completed" button.
  • An action log, so you can see what the store is doing.

Design first. Decide what the store owns:

Piece Kind Why
todos state the list of { id, title, done }
filter state the current filter: all, active or completed
nextId state the next id (kept in state so the store can be serialized)
visibleTodos getter the list after filtering
remaining getter how many are not done
allDone getter whether everything is done
add, toggle, edit, remove, clearCompleted, toggleAll actions the only ways to change todos
flowchart LR
  V["Component (view)"] -->|"calls"| A["Actions"]
  A --> S["State: todos, filter"]
  S --> G["Getters: visibleTodos, remaining"]
  G --> V

The component never changes todos itself; it calls actions and reads getters.

The store

This version uses the options syntax; the setup-store variant is shown after the app.

JavaScript
const useTodoStore = defineStore("todos", {
  state: () => ({
    todos: [],
    filter: "all",
    nextId: 1,
  }),
  getters: {
    visibleTodos(state) {
      if (state.filter === "active") return state.todos.filter((t) => !t.done);
      if (state.filter === "completed") return state.todos.filter((t) => t.done);
      return state.todos;
    },
    remaining: (state) => state.todos.filter((t) => !t.done).length,
    allDone: (state) => state.todos.length > 0 && state.todos.every((t) => t.done),
  },
  actions: {
    add(title) {
      const clean = title.trim();
      if (!clean) return false;
      this.todos.push({ id: this.nextId++, title: clean, done: false });
      return true;
    },
    toggle(id) {
      const todo = this.todos.find((t) => t.id === id);
      if (todo) todo.done = !todo.done;
    },
    edit(id, title) {
      const todo = this.todos.find((t) => t.id === id);
      if (todo && title.trim()) todo.title = title.trim();
    },
    remove(id) { this.todos = this.todos.filter((t) => t.id !== id); },
    clearCompleted() { this.todos = this.todos.filter((t) => !t.done); },
    toggleAll() {
      const target = !this.allDone;
      this.todos.forEach((t) => (t.done = target));
    },
  },
});

Points worth noticing: add rejects empty titles inside the store (validation belongs with the data, not in the template), and it returns a boolean so the view can decide whether to clear the input.

The app

Run it, add a few todos, switch filters and watch the log under the list. The log is fed by $onAction and shows each action with its arguments, which is how you debug a store without opening DevTools.

Todo app: one store, thin component

The view has no business rules: every decision (what counts as visible, what to do with an empty title) is in the store. That is why the next section can test everything without a browser.

Testing the store

Because the store is separate from the component, a test is only a few lines. The runner below is the stand-in from the testing chapter; the store is a compact copy of the one above.

Store tests for the todo logic

The same store as a setup store

The setup syntax is the same logic with refs and computeds:

JavaScript
export const useTodoStore = defineStore("todos", () => {
  const todos = ref([]);
  const filter = ref("all");
  let nextId = 1;

  const remaining = computed(() => todos.value.filter((t) => !t.done).length);
  const visibleTodos = computed(() =>
    filter.value === "active" ? todos.value.filter((t) => !t.done)
    : filter.value === "completed" ? todos.value.filter((t) => t.done)
    : todos.value);

  function add(title) { /* same body, with todos.value */ }

  return { todos, filter, remaining, visibleTodos, add };
});

Careful: nextId as a plain let is not returned, so it would not be part of the state (not serialized, not in DevTools, not restored on SSR). Make it a ref and return it if it must survive.

Extensions to try

  1. Persist the todos with the plugin from the persistence lesson.
  2. Add due dates and a getter that groups todos into overdue, today and later.
  3. Add undo: keep a history array and an undo() action using $subscribe.
  4. Add a search box that filters visibleTodos further.
  5. Write a test for edit that rejects blank titles.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should the rule 'ignore empty titles' live?

    Business rules belong with the data in the store, so every caller gets them and tests can check them.
  2. 2. Which is the right kind of member for the filtered list?

    The filtered list is derived from todos and filter, so it is a getter (computed).
  3. 3. What does $onAction give the log in the example?

    $onAction callbacks receive the action name and args before it runs.
  4. 4. Why return nextId as a ref from a setup store if it must be kept?

    A variable that is not returned is private and not part of the store's state.

Summary

  • Put state, derived values and all rules in the store; keep the component thin.
  • Getters for filtered lists and counters; actions for every change.
  • Validate inside actions and return a result the view can use.
  • $onAction makes a cheap debugging log.
  • A store separate from the view is testable with a few lines.

Next: Project: A Shopping Cart