All files / src/stores categories.ts

0% Statements 0/15
0% Branches 0/6
0% Functions 0/6
0% Lines 0/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                                                                           
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { categoryService } from '../services/category'
import type { Category, CreateCategoryRequest } from '../types'
 
export const useCategoriesStore = defineStore('categories', () => {
  // categories keyed by listId
  const categoriesByList = ref<Record<string, Category[]>>({})
 
  async function fetchForList(listId: string): Promise<void> {
    try {
      categoriesByList.value[listId] = await categoryService.getAll(listId)
    } catch {
      // non-critical — silently ignore
    }
  }
 
  function getForList(listId: string): Category[] {
    return categoriesByList.value[listId] ?? []
  }
 
  async function create(listId: string, req: CreateCategoryRequest): Promise<Category> {
    const category = await categoryService.create(listId, req)
    if (!categoriesByList.value[listId]) categoriesByList.value[listId] = []
    categoriesByList.value[listId].push(category)
    return category
  }
 
  async function remove(listId: string, categoryId: string): Promise<void> {
    await categoryService.delete(listId, categoryId)
    if (categoriesByList.value[listId]) {
      categoriesByList.value[listId] = categoriesByList.value[listId].filter(c => c.id !== categoryId)
    }
  }
 
  return { categoriesByList, fetchForList, getForList, create, remove }
})