All files / src/views CatalogReadOnlyView.vue

85.41% Statements 41/48
86.56% Branches 58/67
78.12% Functions 25/32
84.09% Lines 37/44

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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466                                              1x 1x 1x 1x 1x     1x             1x       1x       1x 1x     1x       1x 1x 1x         1x                     7x           1x       1x   2x             1x             3x                     1x                             7x               7x               7x   1x                                                 2x     2x                                             5x                                   1x     6x 2x     2x         2x     2x                                         1x               1x                                                                                                                                                                                                                                                                                                                                                                                                                                      
<script setup lang="ts">
import { computed, onMounted, ref, watch } from "vue";
 
import {
  formatMoney,
  type BusinessId,
} from "@plain-journal/foundation";
 
import {
  useAdminCatalogStore,
} from "../entities/admin-catalog";
import { useStaffSessionStore } from "../stores/session";
import {
  PjActionGroup,
  PjButton,
  PjField,
  PjPageContainer,
  PjResponsiveImage,
  PjStatusNotice,
  PjSurface,
  resolveCatalogImageDelivery,
} from "@plain-journal/ui";
 
const session = useStaffSessionStore();
const catalog = useAdminCatalogStore();
const failedImageIds = ref<Set<BusinessId>>(new Set());
const roles = computed(() => session.profile?.roles ?? []);
const accessContext = computed(() => ({
  authorized: Boolean(
    session.authenticated
    && roles.value.some((role) => ["ADMIN", "OPERATOR"].includes(role)),
  ),
  operatorId: session.profile?.id ?? null,
  accessToken: session.accessToken,
}));
 
function loadProducts() {
  return catalog.loadProducts(accessContext.value);
}
 
function markImageFailed(productId: BusinessId) {
  failedImageIds.value = new Set([...failedImageIds.value, productId]);
}
 
function refresh() {
  failedImageIds.value = new Set();
  return loadProducts();
}
 
watch(accessContext, (context) => {
  catalog.synchronizeAccess(context);
});
 
onMounted(() => {
  catalog.synchronizeAccess(accessContext.value);
  void catalog.loadWorkspace(accessContext.value);
});
</script>
 
<template>
  <PjPageContainer as="section" size="wide" class="catalog-page">
    <header class="catalog-hero">
      <div>
        <p class="eyebrow">Catalog 公开投影</p>
        <h1>商品目录</h1>
        <p>
          这里展示公开接口返回的 ACTIVE 商品摘要。它是浏览侧投影,不等同于 Catalog
          内部的草稿、下架商品或完整经营后台。
        </p>
      </div>
      <span class="status-label">ADMIN / OPERATOR</span>
    </header>
 
    <PjStatusNotice tone="neutral" title="只读边界">
      <p>
        商品、分类、品牌和图片来自公开读取契约;本页不猜测草稿或下架状态,也不提供
        管理写操作。分页总数只代表当前公开 ACTIVE 投影。
      </p>
    </PjStatusNotice>
 
    <PjSurface tone="soft" padding="large">
      <form class="catalog-filters" @submit.prevent="catalog.applyFilters(accessContext)">
        <PjField
          v-slot="{ describedBy }"
          label="关键词"
          for-id="catalog-keyword"
          hint="匹配商品标题或副标题。"
        >
          <input
            id="catalog-keyword"
            v-model="catalog.query.keyword"
            class="pj-control"
            maxlength="80"
            :aria-describedby="describedBy"
          />
        </PjField>
        <PjField
          v-slot="{ describedBy }"
          label="分类"
          for-id="catalog-category"
        >
          <select
            id="catalog-category"
            v-model="catalog.query.categoryId"
            class="pj-control"
            :aria-describedby="describedBy"
          >
            <option value="">全部公开分类</option>
            <option
              v-for="category in catalog.categories"
              :key="category.id"
              :value="category.id"
            >
              {{ category.name }}
            </option>
          </select>
        </PjField>
        <PjActionGroup :stack-on-compact="true">
          <PjButton
            type="submit"
            :loading="catalog.loadingProducts"
          >
            应用筛选
          </PjButton>
          <PjButton
            type="button"
            variant="text"
            :disabled="catalog.loadingProducts"
            @click="catalog.clearFilters(accessContext)"
          >
            清除筛选
          </PjButton>
          <PjButton
            type="button"
            variant="text"
            :loading="catalog.refreshing"
            @click="refresh"
          >
            重新读取
          </PjButton>
        </PjActionGroup>
      </form>
    </PjSurface>
 
    <PjStatusNotice
      v-if="catalog.categoriesError"
      tone="danger"
      title="分类投影读取未完成"
      assertive
    >
      <p>{{ catalog.categoriesError }}</p>
    </PjStatusNotice>
 
    <PjStatusNotice
      v-if="catalog.productsError"
      tone="danger"
      title="商品投影读取未完成"
      assertive
    >
      <p>{{ catalog.productsError }}</p>
      <p v-if="catalog.products.length > 0">
        页面保留上一次已显示的商品事实,没有把读取失败伪装成空目录。
      </p>
    </PjStatusNotice>
 
    <section class="catalog-section" aria-labelledby="catalog-products-title">
      <header class="catalog-section__header">
        <div>
          <p class="eyebrow">公开 ACTIVE 商品</p>
          <h2 id="catalog-products-title">
            {{ catalog.total }} 条当前投影
          </h2>
        </div>
        <span v-if="catalog.refreshedAt">
          最近读取 {{ new Date(catalog.refreshedAt).toLocaleTimeString() }}
        </span>
      </header>
 
      <div
        v-if="catalog.loadingProducts && catalog.products.length === 0"
        class="catalog-state"
        role="status"
      >
        正在读取商品事实…
      </div>
      <div
        v-else-if="!catalog.loadingProducts && catalog.products.length === 0"
        class="catalog-state"
      >
        当前筛选条件下没有公开商品。
      </div>
      <div v-else class="catalog-product-list">
        <PjSurface
          v-for="product in catalog.products"
          :key="product.id"
          as="article"
          tone="plain"
          padding="medium"
          class="catalog-product"
        >
          <div
            class="catalog-product__media"
            :class="{ 'catalog-product__media--empty': !product.coverUrl || failedImageIds.has(product.id) }"
          >
            <PjResponsiveImage
              v-if="product.coverUrl && !failedImageIds.has(product.id)"
              v-bind="resolveCatalogImageDelivery(product.coverUrl)"
              :alt="`${product.title} 商品图`"
              sizes="112px"
              loading="lazy"
              @error="markImageFailed(product.id)"
            />
            <span v-else aria-hidden="true">无图片</span>
          </div>
          <div class="catalog-product__body">
            <header>
              <div>
                <p class="eyebrow">{{ product.brand.name }}</p>
                <h3>{{ product.title }}</h3>
              </div>
              <strong>{{ formatMoney(product.minimumPrice) }}</strong>
            </header>
            <p class="catalog-product__subtitle">
              {{ product.subtitle || "未填写副标题" }}
            </p>
            <dl class="catalog-product__facts">
              <div><dt>分类</dt><dd>{{ product.category.name }}</dd></div>
              <div><dt>商品 ID</dt><dd><code>{{ product.id }}</code></dd></div>
            </dl>
          </div>
        </PjSurface>
      </div>
 
      <footer v-if="catalog.total > 0" class="catalog-pagination">
        <span>
          显示 {{ catalog.visibleStart }}–{{ catalog.visibleEnd }},
          第 {{ catalog.query.page }} / {{ catalog.pageCount }} 页
        </span>
        <PjActionGroup :stack-on-compact="true">
          <PjButton
            type="button"
            variant="text"
            :disabled="!catalog.hasPreviousPage || catalog.loadingProducts"
            @click="catalog.goToPage(accessContext, catalog.query.page - 1)"
          >
            上一页
          </PjButton>
          <PjButton
            type="button"
            variant="text"
            :disabled="!catalog.hasNextPage || catalog.loadingProducts"
            @click="catalog.goToPage(accessContext, catalog.query.page + 1)"
          >
            下一页
          </PjButton>
        </PjActionGroup>
      </footer>
    </section>
  </PjPageContainer>
</template>
 
<style scoped>
.catalog-page {
  min-width: 0;
  display: grid;
  gap: var(--pj-space-7);
  padding-block: var(--pj-space-7) var(--pj-space-8);
}
 
.catalog-hero,
.catalog-section__header,
.catalog-pagination {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--pj-space-5);
}
 
.catalog-hero h1,
.catalog-section__header h2,
.catalog-product h3 {
  margin: 0;
}
 
.catalog-hero h1 {
  font-size: var(--pj-font-size-xl);
  font-weight: 520;
  letter-spacing: -0.04em;
}
 
.catalog-hero p:last-child {
  max-width: var(--pj-layout-reading);
  margin-bottom: 0;
  color: var(--pj-text-secondary);
}
 
.catalog-filters {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(12rem, 0.65fr) auto;
  align-items: end;
  gap: var(--pj-space-5);
}
 
.catalog-section {
  min-width: 0;
  display: grid;
  gap: var(--pj-space-5);
}
 
.catalog-section__header > span,
.catalog-pagination {
  color: var(--pj-text-secondary);
  font-size: var(--pj-font-size-sm);
}
 
.catalog-product-list {
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--pj-space-4);
}
 
.catalog-product {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(6rem, 7rem) minmax(0, 1fr);
  gap: var(--pj-space-5);
  border-left: 0.2rem solid var(--pj-brand-primary);
}
 
.catalog-product__media {
  min-width: 0;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  overflow: hidden;
  background: var(--pj-color-surface-soft);
  color: var(--pj-text-secondary);
  font-size: var(--pj-font-size-xs);
}
 
.catalog-product__media img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
 
.catalog-product__media--empty {
  border: 1px dashed var(--pj-border-subtle);
}
 
.catalog-product__body {
  min-width: 0;
}
 
.catalog-product__body > header {
  align-items: flex-start;
}
 
.catalog-product__body h3 {
  overflow-wrap: anywhere;
  font-size: var(--pj-font-size-lg);
}
 
.catalog-product__body > header > strong {
  flex: 0 0 auto;
  color: var(--pj-brand-primary);
  font-size: var(--pj-font-size-lg);
  font-weight: 560;
}
 
.catalog-product__subtitle {
  min-height: 3em;
  margin: var(--pj-space-3) 0 0;
  color: var(--pj-text-secondary);
  font-size: var(--pj-font-size-sm);
}
 
.catalog-product__facts {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--pj-space-3);
  margin: var(--pj-space-4) 0 0;
}
 
.catalog-product__facts div {
  min-width: 0;
  padding-top: var(--pj-space-3);
  border-top: 1px solid var(--pj-border-subtle);
}
 
.catalog-product__facts dt {
  color: var(--pj-text-secondary);
  font-size: var(--pj-font-size-xs);
}
 
.catalog-product__facts dd {
  margin: var(--pj-space-1) 0 0;
  overflow-wrap: anywhere;
}
 
.catalog-state {
  min-height: 10rem;
  display: grid;
  place-items: center;
  border-block: 1px solid var(--pj-border-subtle);
  color: var(--pj-text-secondary);
}
 
.catalog-pagination {
  align-items: center;
}
 
@media (max-width: 64rem) {
  .catalog-filters {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
 
  .catalog-filters > .pj-action-group {
    grid-column: 1 / -1;
  }
}
 
@media (max-width: 52rem) {
  .catalog-product-list {
    grid-template-columns: minmax(0, 1fr);
  }
}
 
@media (max-width: 48rem) {
  .catalog-hero,
  .catalog-section__header,
  .catalog-pagination {
    align-items: flex-start;
    flex-direction: column;
  }
 
  .catalog-filters,
  .catalog-product__facts {
    grid-template-columns: minmax(0, 1fr);
  }
}
 
@media (max-width: 32rem) {
  .catalog-page {
    gap: var(--pj-space-6);
    padding-block: var(--pj-space-6);
  }
 
  .catalog-section > .pj-surface {
    padding: var(--pj-space-5);
  }
 
  .catalog-product {
    grid-template-columns: minmax(4.5rem, 5.5rem) minmax(0, 1fr);
    gap: var(--pj-space-4);
  }
 
  .catalog-product__body > header {
    flex-direction: column;
    gap: var(--pj-space-2);
  }
}
</style>