All files / src/stores users.ts

96.87% Statements 31/32
94.44% Branches 17/18
100% Functions 3/3
96.87% Lines 31/32

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                      2x 60x 60x 60x 60x     19x 13x 13x 13x     19x 2x     17x 17x 17x 17x 5x     17x 16x     16x 1x 1x       1x   15x 10x   5x   15x 15x   16x   17x         1x     60x                  
import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'
 
export interface User {
  id: string
  email: string
  name: string
  status: string
}
 
export const useUsersStore = defineStore('users', () => {
  const users = ref<User[]>([])
  const loading = ref(false)
  const nextToken = ref<string | null>(null)
  const hasMore = ref(true)
 
  async function searchUsers(query: string, reset = true) {
    if (reset) {
      users.value = []
      nextToken.value = null
      hasMore.value = true
    }
 
    if (loading.value || (!reset && !hasMore.value)) {
      return
    }
 
    loading.value = true
    try {
      const params: any = { search: encodeURIComponent(query), limit: 30 }
      if (!reset && nextToken.value) {
        params.nextToken = nextToken.value
      }
 
      const response = await api.get('/v1/users', { params })
      const data = response.data
 
      // Handle both old format (array) and new format (object with items)
      if (Array.isArray(data)) {
        if (reset) {
          users.value = data
        } else E{
          users.value = [...users.value, ...data]
        }
        hasMore.value = false
      } else {
        if (reset) {
          users.value = data.items
        } else {
          users.value = [...users.value, ...data.items]
        }
        nextToken.value = data.nextToken
        hasMore.value = data.hasMore
      }
      return users.value
    } finally {
      loading.value = false
    }
  }
 
  async function loadMoreUsers(query: string) {
    await searchUsers(query, false)
  }
 
  return {
    users,
    loading,
    nextToken,
    hasMore,
    searchUsers,
    loadMoreUsers
  }
})