All files / src/stores items.ts

87.09% Statements 54/62
85.71% Branches 30/35
80% Functions 8/10
89.83% Lines 53/59

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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                            3x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x     10x 2x     10x     8x 8x     10x   10x 10x 10x 10x 10x 2x   10x 2x     10x     10x 1x     9x 7x   2x     9x 10x 10x       10x     10x 9x           1x       1x 1x 1x       2x 2x     2x   2x       3x 3x     3x 3x       3x       1x     1x                           92x                                      
import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'
 
export interface Item {
  entityId: string
  id: string
  flowId: 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)
  const total = ref(0)
  const filteredTotal = ref(0)
  const searchQuery = ref('')
  let latestFetchRequestId = 0
 
  async function fetchItems(entityId: string, reset = true, search?: string) {
    if (search !== undefined) {
      searchQuery.value = search
    }
 
    if (reset) {
      // Do NOT clear items.value eagerly — it causes a visible flash.
      // Items are replaced atomically when the response arrives below.
      nextToken.value = null
      hasMore.value = true
    }
 
    Iif ((!reset && loading.value) || (!reset && !hasMore.value)) return
 
    const requestId = ++latestFetchRequestId
    loading.value = true
    try {
      const params: any = { limit: 30 }
      if (searchQuery.value.trim()) {
        params.q = searchQuery.value.trim()
      }
      if (nextToken.value) {
        params.nextToken = nextToken.value
      }
 
      const response = await api.get(`/v1/entities/${entityId}/items`, { params })
 
      // A newer request has started: ignore stale response.
      if (requestId !== latestFetchRequestId) {
        return
      }
 
      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
      filteredTotal.value =
        typeof response.data.filteredTotal === 'number'
          ? response.data.filteredTotal
          : items.value.length
      total.value =
        typeof response.data.total === 'number' ? response.data.total : filteredTotal.value
    } finally {
      if (requestId === latestFetchRequestId) {
        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,
    total,
    filteredTotal,
    searchQuery,
    fetchItems,
    loadMore,
    fetchItem,
    createItem,
    updateItem,
    deleteItem,
    clearAllItems
  }
})