All files / src/views StorePage.vue

98.59% Statements 70/71
95.94% Branches 71/74
96.55% Functions 28/29
98.41% Lines 62/63

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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290            2x     1x 78x             1x       1x                                         2x                       3x           78x       2x                           61x                                                       56x                     22x                           1x                                                                     21x 21x 21x 21x 21x 21x 21x   21x       21x   25x             21x 25x       21x         23x     2x     21x 30x           21x 26x           21x 7x                   30x 28x   24x 24x       3x 2x 1x       5x 5x       21x 142x   21x 142x           3x 3x       2x 2x   2x     25x 21x       1x 1x 1x 1x       1x 1x   1x       21x    
<template>
  <PageLayout :title="t('store.title')" :description="t('store.description')" class="store-page">
    <template #action>
      <button
        v-if="canPublish"
        class="btn btn-outline-secondary me-2 publish-pack-btn"
        @click="publishOpen = true"
      >
        <font-awesome-icon icon="fa-solid fa-store" class="me-md-2" />
        <span class="d-none d-md-inline">{{ t('store.publishAction') }}</span>
      </button>
      <button
        class="btn btn-outline-secondary refresh-store-btn"
        :disabled="isLoading"
        @click="load"
      >
        <font-awesome-icon icon="fa-solid fa-rotate-right" class="me-md-2" />
        <span class="d-none d-md-inline">{{ t('common.refresh') }}</span>
      </button>
    </template>
 
    <div class="container-fluid">
      <ul class="nav nav-tabs mb-3" role="tablist">
        <li class="nav-item" role="presentation">
          <button
            type="button"
            class="nav-link store-tab-all"
            :class="{ active: tab === 'all' }"
            role="tab"
            :aria-selected="tab === 'all'"
            @click="selectTab('all')"
          >
            {{ t('store.allPacks') }}
          </button>
        </li>
        <li class="nav-item" role="presentation">
          <button
            type="button"
            class="nav-link store-tab-mine"
            :class="{ active: tab === 'mine' }"
            role="tab"
            :aria-selected="tab === 'mine'"
            @click="selectTab('mine')"
          >
            {{ t('store.myPacks') }}
          </button>
        </li>
        <li v-if="canSeeInstalled" class="nav-item" role="presentation">
          <button
            type="button"
            class="nav-link store-tab-installed"
            :class="{ active: tab === 'installed' }"
            role="tab"
            :aria-selected="tab === 'installed'"
            @click="selectTab('installed')"
          >
            {{ t('store.installedPacks') }}
          </button>
        </li>
      </ul>
    </div>
 
    <div v-if="tab === 'all'" class="container-fluid">
      <SearchFilterHeader
        v-model="searchQuery"
        :placeholder="t('store.searchPlaceholder')"
        :show-counts="false"
      >
        <template #extra>
          <SortControl
            :options="sortOptions"
            :field-value="packsStore.sort"
            direction="desc"
            :show-direction="false"
            :label="t('common.sortBy')"
            :disabled="isSearching"
            :disabled-title="t('store.sortDuringSearch')"
            @update:field-value="changeSort"
          />
        </template>
      </SearchFilterHeader>
      <p v-if="packsStore.search && packsStore.truncated" class="small text-muted store-truncated">
        {{ t('store.searchTruncated') }}
      </p>
    </div>
 
    <div v-if="packsStore.error" class="alert alert-danger" role="alert">
      {{ packsStore.error }}
    </div>
 
    <LoadingSpinner
      v-if="isLoading && shown.length === 0"
      :grid="false"
      :padding="5"
      :message="t('common.loading')"
    />
 
    <EmptyState v-else-if="shown.length === 0" variant="info">
      {{
        tab === 'mine'
          ? t('store.emptyMine')
          : tab === 'installed'
            ? t('store.emptyInstalled')
            : packsStore.search
              ? t('common.noResults')
              : t('store.empty')
      }}
    </EmptyState>
 
    <VirtualCardGrid
      v-else
      :items="shown"
      :has-more="hasMore"
      :is-loading="isLoading"
      :offset-height="280"
      @load-more="loadMore"
    >
      <template #card="{ item: pack }">
        <PackCard :pack="pack" @open="open" @toggle-like="toggleLike" />
      </template>
    </VirtualCardGrid>
  </PageLayout>
 
  <FloatingPageActions :scroll-top-title="t('common.backToTop')" />
 
  <!-- The pack opened, over the Store: /store?pack={packId} -->
  <PackSidebar :pack-id="openPackId" :section="openSection" @close="close" @published="load" />
 
  <!-- A new pack of the active flow; its next versions are published from the pack -->
  <PublishPackSidebar
    :is-open="publishOpen"
    @cancel="publishOpen = false"
    @published="handlePublished"
  />
</template>
 
<script setup lang="ts">
// @implements UC-STORE-001.1, UC-STORE-002.1, UC-STORE-006.1, UC-STORE-007.1, UC-STORE-010.1, UC-STORE-011.1, UC-STORE-012.1, UC-STORE-015.1
import { computed, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import PageLayout from '@/components/PageLayout.vue'
import VirtualCardGrid from '@/components/VirtualCardGrid.vue'
import PackCard from '@/components/PackCard.vue'
import PackSidebar from '@/components/PackSidebar.vue'
import PublishPackSidebar from '@/components/PublishPackSidebar.vue'
import FloatingPageActions from '@/components/FloatingPageActions.vue'
import LoadingSpinner from '@/components/LoadingSpinner.vue'
import EmptyState from '@/components/EmptyState.vue'
import SearchFilterHeader from '@/components/SearchFilterHeader.vue'
import SortControl, { type SortOption } from '@/components/SortControl.vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { useDebouncedSearch } from '@/composables/useDebouncedSearch'
import { usePermissions } from '@/composables/usePermissions'
import {
  hasUpdate,
  usePacksStore,
  type InstalledPack,
  type Pack,
  type PackSort
} from '@/stores/packs'
import { useAuthStore } from '@/stores/auth'
import { useToastStore } from '@/stores/toast'
import { getErrorMessage } from '@/utils/errors'
 
type StoreTab = 'all' | 'mine' | 'installed'
 
const route = useRoute()
const router = useRouter()
const { t } = useUILanguage()
const { hasPermission, userPermissions } = usePermissions()
const packsStore = usePacksStore()
const authStore = useAuthStore()
const toastStore = useToastStore()
 
const tab = ref<StoreTab>('all')
 
// What the active flow installed: for those who may create its entities or
// read some, as GET /v1/store/installed allows
const canSeeInstalled = computed(
  () =>
    !!authStore.currentFlow &&
    (hasPermission('entities:create') ||
      hasPermission('entities:read') ||
      userPermissions.value.some((granted) => /^entities-[^:]+:(read|\*)$/.test(granted)))
)
 
// Searches the name and description of every pack, as the Catalog does its entities
const { searchQuery, appliedQuery, runSearchNow } = useDebouncedSearch((query) =>
  packsStore.fetchPacks(false, query)
)
 
// The whole Store is sorted, never a search: its matches come in no order
const sortOptions = computed<SortOption[]>(() => [
  { value: 'recent', label: t('store.sortRecent') },
  { value: 'likes', label: t('store.sortLikes') },
  { value: 'imports', label: t('store.sortImports') }
])
const isSearching = computed(() => searchQuery.value.trim() !== '')
 
function changeSort(value: string) {
  return packsStore.fetchPacks(false, undefined, value as PackSort)
}
 
const shown = computed<Pack[]>(() =>
  tab.value === 'all'
    ? packsStore.packs
    : tab.value === 'mine'
      ? packsStore.myPacks
      : packsStore.installed
)
const isLoading = computed(() =>
  tab.value === 'all'
    ? packsStore.loading
    : tab.value === 'mine'
      ? packsStore.loadingMine
      : packsStore.loadingInstalled
)
const hasMore = computed(() =>
  Boolean(
    tab.value === 'all'
      ? packsStore.nextToken
      : tab.value === 'mine'
        ? packsStore.myNextToken
        : packsStore.installedNextToken
  )
)
 
function load() {
  if (tab.value === 'mine') return packsStore.fetchMyPacks()
  if (tab.value === 'installed') return packsStore.fetchInstalled()
  // Refresh keeps the search the list shows, dropping text still being typed
  searchQuery.value = appliedQuery.value
  return runSearchNow()
}
 
function loadMore() {
  if (tab.value === 'mine') return packsStore.fetchMyPacks(true)
  if (tab.value === 'installed') return packsStore.fetchInstalled(true)
  return packsStore.fetchPacks(true)
}
 
function selectTab(next: StoreTab) {
  tab.value = next
  return load()
}
 
// The pack opened over the Store, from the URL so that it can be shared
const openPackId = computed(() =>
  typeof route.query.pack === 'string' && route.query.pack ? route.query.pack : null
)
const openSection = computed(() =>
  typeof route.query.section === 'string' ? route.query.section : null
)
 
// Pushed, so that Back closes it; an installed pack behind opens on its update
function open(pack: Pack) {
  const section =
    tab.value === 'installed' && hasUpdate(pack as InstalledPack) ? 'update' : undefined
  router.push({ query: { ...route.query, pack: pack.id, section } })
}
 
function close() {
  const { pack: _pack, section: _section, ...query } = route.query
  router.replace({ query })
  // What changed in the pack (an update, a deletion) shows on the tab
  if (tab.value === 'installed') packsStore.fetchInstalled()
}
 
const canPublish = computed(() => !!authStore.currentFlow && hasPermission('packs:publish'))
const publishOpen = ref(false)
 
// The new pack opens over the Store, which lists it at once
async function handlePublished({ id, version }: { id: string; version: number }) {
  publishOpen.value = false
  toastStore.success(t('store.publishSuccess').replace('{version}', String(version)))
  await router.push({ query: { ...route.query, pack: id, section: undefined } })
  await load()
}
 
async function toggleLike(pack: Pack) {
  try {
    await packsStore.toggleLike(pack.id, pack.likedByMe)
  } catch (err) {
    toastStore.error(getErrorMessage(err, t('store.likeError'), t))
  }
}
 
onMounted(load)
</script>