All files / src/components AdminUsersPanel.vue

100% Statements 17/17
95.83% Branches 23/24
100% Functions 5/5
100% Lines 15/15

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  17x 2x   2x                                                                 2x           2x         6x                                 2x                       1x                                   13x 13x 13x       13x 3x           3x     13x    
<template>
  <div class="admin-users-panel">
    <p class="text-muted">{{ t('admin.usersDirectoryDescription') }}</p>
 
    <div class="mb-3">
      <label for="admin-users-search" class="visually-hidden">{{ t('common.search') }}</label>
      <input
        id="admin-users-search"
        :value="searchQuery"
        type="search"
        class="form-control"
        :placeholder="t('admin.searchUsersPlaceholder')"
        @input="onSearchInput"
      />
    </div>
 
    <ErrorAlert
      v-if="adminStore.platformUsers.error"
      :title="t('common.error')"
      :message="adminStore.platformUsers.error"
    />
    <LoadingSpinner v-if="adminStore.platformUsers.loading" />
 
    <template v-else-if="!adminStore.platformUsers.error">
      <div v-if="adminStore.platformUsers.truncated" class="alert alert-warning">
        {{ t('admin.searchTruncated') }}
      </div>
 
      <p v-if="adminStore.platformUsers.items.length === 0" class="text-muted">
        <!-- A search reads a bounded number of candidates per request, so an empty page
             with a cursor means "nothing yet", not "nothing" -->
        {{
          adminStore.platformUsers.nextCursor ? t('admin.noMatchesYet') : t('admin.noUsersFound')
        }}
      </p>
 
      <div v-else class="table-responsive">
        <table class="table table-hover align-middle">
          <thead>
            <tr>
              <th>{{ t('admin.colUser') }}</th>
              <th>{{ t('admin.colEmail') }}</th>
              <th>{{ t('admin.colStatus') }}</th>
              <th class="text-end">{{ t('admin.colCredits') }}</th>
              <th>{{ t('admin.colJoined') }}</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="user in adminStore.platformUsers.items" :key="user.id">
              <td>
                <strong>{{ user.name || user.email }}</strong>
                <span v-if="user.nickname" class="text-muted"> @{{ user.nickname }}</span>
                <span v-if="user.isPlatformAdmin" class="badge bg-primary ms-2">
                  {{ t('admin.platformAdminBadge') }}
                </span>
              </td>
              <td>{{ user.email }}</td>
              <td>
                <span
                  class="badge"
                  :class="user.status === 'active' ? 'bg-success' : 'bg-secondary'"
                >
                  {{ user.status }}
                </span>
              </td>
              <td class="text-end">{{ user.creditsBalance.toLocaleString() }}</td>
              <td>{{ formatDate(user.createdAt) || '—' }}</td>
            </tr>
          </tbody>
        </table>
      </div>
 
      <button
        v-if="adminStore.platformUsers.nextCursor"
        type="button"
        class="btn btn-outline-secondary"
        :disabled="adminStore.platformUsers.loadingMore"
        @click="adminStore.platformUsers.loadMore()"
      >
        {{ t('admin.loadMore') }}
      </button>
    </template>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-ADMIN-006.1
import { onMounted } from 'vue'
import { useAdminStore } from '@/stores/admin'
import { useUILanguage } from '@/composables/useUILanguage'
import { useDateFormat } from '@/composables/useDateFormat'
import { useDebouncedSearch } from '@/composables/useDebouncedSearch'
import LoadingSpinner from '@/components/LoadingSpinner.vue'
import ErrorAlert from '@/components/ErrorAlert.vue'
 
const adminStore = useAdminStore()
const { t } = useUILanguage()
const { formatDate } = useDateFormat()
 
// Unlike the Credits picker there is no minimum length: clearing the box goes
// back to the unfiltered list.
const { searchQuery, scheduleSearch } = useDebouncedSearch(
  (query) => adminStore.platformUsers.load(query),
  300,
  { auto: false }
)
 
function onSearchInput(event: Event) {
  scheduleSearch((event.target as HTMLInputElement).value)
}
 
onMounted(() => adminStore.platformUsers.load())
</script>