All files / src/stores labels.ts

100% Statements 15/15
66.66% Branches 4/6
100% Functions 6/6
100% Lines 13/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          2x   7x     4x 4x             5x       2x 2x 2x 2x       2x 2x 3x       7x    
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { labelService } from '../services/label'
import type { Label, CreateLabelRequest } from '../types'
 
export const useLabelsStore = defineStore('labels', () => {
  // labels keyed by listId
  const labelsByList = ref<Record<string, Label[]>>({})
 
  async function fetchForList(listId: string): Promise<void> {
    try {
      labelsByList.value[listId] = await labelService.getAll(listId)
    } catch {
      // non-critical — silently ignore
    }
  }
 
  function getForList(listId: string): Label[] {
    return labelsByList.value[listId] ?? []
  }
 
  async function create(listId: string, req: CreateLabelRequest): Promise<Label> {
    const label = await labelService.create(listId, req)
    Eif (!labelsByList.value[listId]) labelsByList.value[listId] = []
    labelsByList.value[listId].push(label)
    return label
  }
 
  async function remove(listId: string, labelId: string): Promise<void> {
    await labelService.delete(listId, labelId)
    Eif (labelsByList.value[listId]) {
      labelsByList.value[listId] = labelsByList.value[listId].filter(l => l.id !== labelId)
    }
  }
 
  return { labelsByList, fetchForList, getForList, create, remove }
})