All files / src/stores items.ts

83.67% Statements 41/49
85.71% Branches 18/21
80% Functions 8/10
86.95% Lines 40/46

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                            2x 48x 48x 48x 48x 48x 48x     8x 6x 6x 6x     8x   8x 8x 8x 8x 2x     8x   8x 6x   2x     8x 8x   8x         1x       1x 1x 1x       2x 2x     2x   2x       3x 3x     3x 3x       3x       1x     1x                           48x                                
import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'
 
export interface Item {
  entityId: string
  id: string
  tenantId: string
  createdBy: string
  createdAt: string
  updatedAt: string
  [key: string]: any
}
 
export const useItemsStore = defineStore('items', () => {
  const items = ref<Item[]>([])
  const currentItem = ref<Item | null>(null)
  const nextToken = ref<string | null>(null)
  const hasMore = ref<boolean>(true)
  const loading = ref<boolean>(false)
  const error = ref<string | null>(null)
 
  async function fetchItems(entityId: string, reset = true) {
    if (reset) {
      items.value = []
      nextToken.value = null
      hasMore.value = true
    }
 
    Iif (loading.value || !hasMore.value) return
 
    loading.value = true
    try {
      const params: any = { limit: 30 }
      if (nextToken.value) {
        params.nextToken = nextToken.value
      }
 
      const response = await api.get(`/v1/entities/${entityId}/items`, { params })
 
      if (reset) {
        items.value = response.data.items || response.data
      } else {
        items.value = [...items.value, ...(response.data.items || response.data)]
      }
 
      nextToken.value = response.data.nextToken || null
      hasMore.value = response.data.hasMore ?? false
    } finally {
      loading.value = false
    }
  }
 
  async function loadMore(entityId: string) {
    await fetchItems(entityId, false)
  }
 
  async function fetchItem(entityId: string, itemId: string) {
    const response = await api.get(`/v1/entities/${entityId}/items/${itemId}`)
    currentItem.value = response.data
    return response.data
  }
 
  async function createItem(entityId: string, data: any) {
    const response = await api.post(`/v1/entities/${entityId}/items`, { entityId, data })
    const newItem = response.data
 
    // Add new item to the beginning of the list
    items.value.unshift(newItem)
 
    return newItem
  }
 
  async function updateItem(entityId: string, itemId: string, data: any) {
    const response = await api.put(`/v1/entities/${entityId}/items/${itemId}`, { data })
    const updatedItem = response.data
 
    // Update item locally instead of refetching all
    const index = items.value.findIndex((i) => i.id === itemId)
    Iif (index !== -1) {
      items.value[index] = updatedItem
    }
 
    return updatedItem
  }
 
  async function deleteItem(entityId: string, itemId: string) {
    await api.delete(`/v1/entities/${entityId}/items/${itemId}`)
 
    // Remove item locally instead of refetching all
    items.value = items.value.filter((i) => i.id !== itemId)
  }
 
  async function clearAllItems(entityId: string) {
    const response = await api.delete(`/v1/entities/${entityId}/items`)
 
    // Clear all items locally
    items.value = []
    nextToken.value = null
    hasMore.value = false
 
    return response
  }
 
  return {
    items,
    currentItem,
    nextToken,
    hasMore,
    loading,
    error,
    fetchItems,
    loadMore,
    fetchItem,
    createItem,
    updateItem,
    deleteItem,
    clearAllItems
  }
})