Finish admin page

This commit is contained in:
Nông Đức Huy
2026-08-13 23:20:23 +07:00
parent cda5d21d0d
commit 8fd23d162f
107 changed files with 6321 additions and 563 deletions
+10 -8
View File
@@ -1,22 +1,24 @@
import type { Metadata } from 'next';
import { getTranslations } from 'next-intl/server';
import { PageScaffold } from '@/components/layout/page-scaffold';
import { BrandsPanel } from '@/features/taxonomy/panels';
export async function generateMetadata(): Promise<Metadata> {
const t = await getTranslations('pages.brands');
return { title: t('title') };
}
export default async function BrandsPage() {
export default async function Page() {
const t = await getTranslations('pages.brands');
return (
<PageScaffold
title={t('title')}
description={t('body')}
permission="brand.read"
milestone="M3 — admin catalog"
/>
<div className="space-y-6 p-8">
<header>
<h1 className="text-2xl font-bold">{t('title')}</h1>
<p className="text-ink-500 mt-2 max-w-2xl text-sm">{t('body')}</p>
</header>
<BrandsPanel />
</div>
);
}
@@ -1,22 +1,24 @@
import type { Metadata } from 'next';
import { getTranslations } from 'next-intl/server';
import { PageScaffold } from '@/components/layout/page-scaffold';
import { CategoriesPanel } from '@/features/taxonomy/panels';
export async function generateMetadata(): Promise<Metadata> {
const t = await getTranslations('pages.categories');
return { title: t('title') };
}
export default async function CategoriesPage() {
export default async function Page() {
const t = await getTranslations('pages.categories');
return (
<PageScaffold
title={t('title')}
description={t('body')}
permission="category.read"
milestone="M3 — admin catalog"
/>
<div className="space-y-6 p-8">
<header>
<h1 className="text-2xl font-bold">{t('title')}</h1>
<p className="text-ink-500 mt-2 max-w-2xl text-sm">{t('body')}</p>
</header>
<CategoriesPanel />
</div>
);
}
@@ -1,22 +1,24 @@
import type { Metadata } from 'next';
import { getTranslations } from 'next-intl/server';
import { PageScaffold } from '@/components/layout/page-scaffold';
import { CollectionsPanel } from '@/features/taxonomy/panels';
export async function generateMetadata(): Promise<Metadata> {
const t = await getTranslations('pages.collections');
return { title: t('title') };
}
export default async function CollectionsPage() {
export default async function Page() {
const t = await getTranslations('pages.collections');
return (
<PageScaffold
title={t('title')}
description={t('body')}
permission="collection.read"
milestone="M3 — admin catalog"
/>
<div className="space-y-6 p-8">
<header>
<h1 className="text-2xl font-bold">{t('title')}</h1>
<p className="text-ink-500 mt-2 max-w-2xl text-sm">{t('body')}</p>
</header>
<CollectionsPanel />
</div>
);
}
@@ -30,7 +30,7 @@ export function LanguageSwitcher({ className }: { className?: string }) {
title={LOCALE_LABELS[locale]}
className={cn(
'rounded-card px-1.5 py-1 text-xs font-semibold uppercase tracking-widest transition-colors',
locale === activeLocale ? 'bg-ink-100 text-ink-950' : 'text-ink-400 hover:text-ink-950',
locale === activeLocale ? 'bg-ink-100 text-ink-950' : 'text-ink-500 hover:text-ink-950',
)}
>
{locale}
@@ -43,7 +43,7 @@ export function AdminSidebar() {
<nav className="space-y-6 p-5">
{sections.map((section) => (
<div key={section.titleKey}>
<h2 className="text-ink-400 text-[0.625rem] font-semibold uppercase tracking-widest">
<h2 className="text-ink-500 text-eyebrow uppercase">
{t(`sections.${section.titleKey}`)}
</h2>
<ul className="mt-2 space-y-0.5">
@@ -235,7 +235,7 @@ export function ContentEditor({
// A language with no title yet is not written — worth showing,
// because an operator switching tabs needs to know which
// versions will actually be saved.
!draft.translations[key].title.trim() && locale !== key ? 'text-ink-400' : '',
!draft.translations[key].title.trim() && locale !== key ? 'text-ink-500' : '',
)}
>
{key}
@@ -258,7 +258,7 @@ export function ContentEditor({
onChange={(event) => setField('slug', event.target.value)}
placeholder="how-to-layer"
/>
<span className="text-ink-400 text-xs">{t('slugHint')}</span>
<span className="text-ink-500 text-xs">{t('slugHint')}</span>
</label>
</div>
@@ -269,7 +269,7 @@ export function ContentEditor({
value={fields.excerpt}
onChange={(event) => setField('excerpt', event.target.value)}
/>
<span className="text-ink-400 text-xs">{t('excerptHint')}</span>
<span className="text-ink-500 text-xs">{t('excerptHint')}</span>
</label>
) : null}
@@ -282,7 +282,7 @@ export function ContentEditor({
spellCheck
className="border-ink-200 focus:border-ink-950 w-full border px-3 py-2 font-mono text-sm leading-relaxed outline-none"
/>
<span className="text-ink-400 text-xs">{t('bodyHint')}</span>
<span className="text-ink-500 text-xs">{t('bodyHint')}</span>
</label>
<details className="border-ink-200 border p-4">
+77 -79
View File
@@ -7,13 +7,27 @@ import { useCallback, useEffect, useState } from 'react';
import { isApiClientError } from '@sport/api-client';
import { PERMISSIONS, type AdminContentEntry, type ContentType } from '@sport/types';
import { Badge, Button, Input, Skeleton, cn } from '@sport/ui';
import {
Badge,
Button,
DataTable,
DataTableBody,
DataTableCell,
DataTableHead,
DataTableHeader,
DataTableRow,
EmptyState,
Input,
SegmentedControl,
Skeleton,
} from '@sport/ui';
import { useSession } from '@/features/auth/session-provider';
import { browserApi } from '@/lib/api';
import { formatDateTime } from '@/lib/format';
type TypeFilter = ContentType | 'ALL';
const TYPE_FILTERS = ['ALL', 'POST', 'PAGE'] as const satisfies readonly (ContentType | 'ALL')[];
type TypeFilter = (typeof TYPE_FILTERS)[number];
export function ContentTable() {
const t = useTranslations('cms');
@@ -68,22 +82,12 @@ export function ContentTable() {
aria-label={t('searchPlaceholder')}
/>
<div className="border-ink-200 rounded-card flex overflow-hidden border">
{(['ALL', 'POST', 'PAGE'] as const).map((value) => (
<button
key={value}
type="button"
aria-pressed={type === value}
onClick={() => setType(value)}
className={cn(
'px-3 py-2 text-sm transition-colors',
type === value ? 'bg-ink-950 text-white' : 'hover:bg-ink-100 bg-white',
)}
>
{t(`filter.${value}`)}
</button>
))}
</div>
<SegmentedControl
label={t('searchPlaceholder')}
value={type}
onChange={setType}
options={TYPE_FILTERS.map((value) => ({ value, label: t(`filter.${value}`) }))}
/>
{canManage ? (
<div className="ml-auto flex gap-2">
@@ -115,75 +119,69 @@ export function ContentTable() {
<Skeleton className="h-12 w-full" />
</div>
) : entries.length === 0 ? (
<p className="text-ink-500 border-ink-200 border border-dashed p-8 text-center text-sm">
{t('empty')}
</p>
<EmptyState title={t('empty')} />
) : (
<div className="border-ink-200 overflow-x-auto border bg-white">
<table className="min-w-3xl w-full text-sm">
<thead className="border-ink-200 bg-ink-50 border-b text-left">
<tr className="text-ink-500 text-[0.625rem] uppercase tracking-widest">
<th className="px-4 py-3 font-semibold">{t('table.title')}</th>
<th className="px-4 py-3 font-semibold">{t('table.languages')}</th>
<th className="px-4 py-3 font-semibold">{t('table.status')}</th>
<th className="px-4 py-3 font-semibold">{t('table.updated')}</th>
<th className="px-4 py-3" />
</tr>
</thead>
<tbody className="divide-ink-100 divide-y">
{entries.map((entry) => {
const languages = Object.keys(entry.translations);
const primary = entry.translations.en ?? entry.translations.vi;
<DataTable>
<DataTableHead>
<DataTableHeader>{t('table.title')}</DataTableHeader>
<DataTableHeader>{t('table.languages')}</DataTableHeader>
<DataTableHeader>{t('table.status')}</DataTableHeader>
<DataTableHeader>{t('table.updated')}</DataTableHeader>
<DataTableHeader />
</DataTableHead>
<DataTableBody>
{entries.map((entry) => {
const languages = Object.keys(entry.translations);
const primary = entry.translations.en ?? entry.translations.vi;
return (
<tr key={entry.id} className="hover:bg-ink-50/60">
<td className="px-4 py-3">
<div className="flex items-center gap-2">
{entry.type === 'POST' ? (
<Newspaper className="text-ink-400 size-4 shrink-0" />
) : (
<FileText className="text-ink-400 size-4 shrink-0" />
)}
<div>
<div className="font-medium">{primary?.title ?? t('untitled')}</div>
<div className="text-ink-500 font-mono text-xs">
/{primary?.slug ?? '—'}
</div>
return (
<DataTableRow key={entry.id}>
<DataTableCell>
<div className="flex items-center gap-2">
{entry.type === 'POST' ? (
<Newspaper className="text-ink-500 size-4 shrink-0" />
) : (
<FileText className="text-ink-500 size-4 shrink-0" />
)}
<div>
<div className="font-medium">{primary?.title ?? t('untitled')}</div>
<div className="text-ink-500 font-mono text-xs">
/{primary?.slug ?? '—'}
</div>
</div>
</td>
</div>
</DataTableCell>
<td className="px-4 py-3">
<div className="flex gap-1">
{languages.map((language) => (
<Badge key={language} variant="outline">
{language.toUpperCase()}
</Badge>
))}
</div>
</td>
<DataTableCell>
<div className="flex gap-1">
{languages.map((language) => (
<Badge key={language} variant="outline">
{language.toUpperCase()}
</Badge>
))}
</div>
</DataTableCell>
<td className="px-4 py-3">
<Badge variant={entry.status === 'PUBLISHED' ? 'success' : 'neutral'}>
{t(`status.${entry.status}`)}
</Badge>
</td>
<DataTableCell>
<Badge variant={entry.status === 'PUBLISHED' ? 'success' : 'neutral'}>
{t(`status.${entry.status}`)}
</Badge>
</DataTableCell>
<td className="text-ink-500 px-4 py-3 text-xs">
{formatDateTime(entry.updatedAt, format)}
</td>
<DataTableCell className="text-ink-500 text-xs">
{formatDateTime(entry.updatedAt, format)}
</DataTableCell>
<td className="px-4 py-3 text-right">
<Button variant="outline" size="sm" asChild>
<Link href={`/cms/${entry.id}`}>{t('edit')}</Link>
</Button>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
<DataTableCell className="text-right">
<Button variant="outline" size="sm" asChild>
<Link href={`/cms/${entry.id}`}>{t('edit')}</Link>
</Button>
</DataTableCell>
</DataTableRow>
);
})}
</DataTableBody>
</DataTable>
)}
</div>
);
@@ -214,7 +214,7 @@ export function DiscountDialog({
onChange={(event) => set('value', event.target.value)}
required
/>
<span className="text-ink-400 text-xs">
<span className="text-ink-500 text-xs">
{isPercentage ? t('valuePercentHint') : t('valueAmountHint')}
</span>
</label>
@@ -243,7 +243,7 @@ export function DiscountDialog({
'-mb-px border-b-2 px-3 py-1.5 text-xs font-semibold uppercase tracking-widest',
locale === key
? 'border-ink-950 text-ink-950'
: 'text-ink-400 hover:text-ink-600 border-transparent',
: 'text-ink-500 hover:text-ink-600 border-transparent',
)}
>
{key}
@@ -266,7 +266,7 @@ export function DiscountDialog({
}
placeholder={t('namePlaceholder')}
/>
<span className="text-ink-400 text-xs">{t('nameHint')}</span>
<span className="text-ink-500 text-xs">{t('nameHint')}</span>
</label>
</div>
@@ -317,7 +317,7 @@ export function DiscountDialog({
placeholder={t('unlimited')}
/>
{discount ? (
<span className="text-ink-400 text-xs">
<span className="text-ink-500 text-xs">
{t('alreadyUsed', { count: discount.usageCount })}
</span>
) : null}
@@ -337,7 +337,7 @@ export function DiscountDialog({
value={draft.priority}
onChange={(event) => set('priority', event.target.value)}
/>
<span className="text-ink-400 text-xs">{t('priorityHint')}</span>
<span className="text-ink-500 text-xs">{t('priorityHint')}</span>
</label>
<div className="space-y-3 pt-6">
@@ -435,7 +435,7 @@ function Choice({
</button>
))}
</div>
{hint ? <span className="text-ink-400 block text-xs">{hint}</span> : null}
{hint ? <span className="text-ink-500 block text-xs">{hint}</span> : null}
</div>
);
}
@@ -469,7 +469,7 @@ function Toggle({
<label htmlFor={id} className="block text-sm font-medium">
{label}
</label>
<span className="text-ink-400 block text-xs">{hint}</span>
<span className="text-ink-500 block text-xs">{hint}</span>
</div>
</div>
);
@@ -6,7 +6,20 @@ import { useCallback, useEffect, useState } from 'react';
import { isApiClientError } from '@sport/api-client';
import { PERMISSIONS, type AdminDiscount } from '@sport/types';
import { Badge, Button, Input, Skeleton, cn } from '@sport/ui';
import {
Badge,
Button,
DataTable,
DataTableBody,
DataTableCell,
DataTableHead,
DataTableHeader,
DataTableRow,
EmptyState,
Input,
SegmentedControl,
Skeleton,
} from '@sport/ui';
import { useSession } from '@/features/auth/session-provider';
import { browserApi } from '@/lib/api';
@@ -14,7 +27,8 @@ import { formatDateTime, formatMoney } from '@/lib/format';
import { DiscountDialog } from './discount-dialog';
type TriggerFilter = 'ALL' | 'AUTOMATIC' | 'CODE';
const TRIGGER_FILTERS = ['ALL', 'CODE', 'AUTOMATIC'] as const;
type TriggerFilter = (typeof TRIGGER_FILTERS)[number];
export function DiscountsTable() {
const t = useTranslations('discounts');
@@ -87,22 +101,12 @@ export function DiscountsTable() {
aria-label={t('searchPlaceholder')}
/>
<div className="border-ink-200 rounded-card flex overflow-hidden border">
{(['ALL', 'CODE', 'AUTOMATIC'] as const).map((value) => (
<button
key={value}
type="button"
aria-pressed={trigger === value}
onClick={() => setTrigger(value)}
className={cn(
'px-3 py-2 text-sm transition-colors',
trigger === value ? 'bg-ink-950 text-white' : 'hover:bg-ink-100 bg-white',
)}
>
{t(`filter.${value}`)}
</button>
))}
</div>
<SegmentedControl
label={t('table.status')}
value={trigger}
onChange={setTrigger}
options={TRIGGER_FILTERS.map((value) => ({ value, label: t(`filter.${value}`) }))}
/>
<Button className="ml-auto" onClick={() => setEditing({ discount: null })}>
<Plus className="size-4" />
@@ -123,37 +127,41 @@ export function DiscountsTable() {
<Skeleton className="h-12 w-full" />
</div>
) : discounts.length === 0 ? (
<p className="text-ink-500 border-ink-200 border border-dashed p-8 text-center text-sm">
{t('empty')}
</p>
<EmptyState
title={t('empty')}
action={
<Button onClick={() => setEditing({ discount: null })}>
<Plus className="size-4" />
{t('create')}
</Button>
}
/>
) : (
<div className="border-ink-200 overflow-x-auto border bg-white">
<table className="min-w-3xl w-full text-sm">
<thead className="border-ink-200 bg-ink-50 border-b text-left">
<tr className="text-ink-500 text-[0.625rem] uppercase tracking-widest">
<th className="px-4 py-3 font-semibold">{t('table.name')}</th>
<th className="px-4 py-3 font-semibold">{t('table.value')}</th>
<th className="px-4 py-3 font-semibold">{t('table.window')}</th>
<th className="px-4 py-3 font-semibold">{t('table.usage')}</th>
<th className="px-4 py-3 font-semibold">{t('table.stacking')}</th>
<th className="px-4 py-3 font-semibold">{t('table.status')}</th>
<th className="px-4 py-3" />
</tr>
</thead>
<tbody className="divide-ink-100 divide-y">
{discounts.map((discount) => (
<Row
key={discount.id}
discount={discount}
onEdit={() => setEditing({ discount })}
now={loadedAt}
t={t}
format={format}
/>
))}
</tbody>
</table>
</div>
<DataTable>
<DataTableHead>
<DataTableHeader>{t('table.name')}</DataTableHeader>
<DataTableHeader>{t('table.value')}</DataTableHeader>
<DataTableHeader>{t('table.window')}</DataTableHeader>
<DataTableHeader>{t('table.usage')}</DataTableHeader>
<DataTableHeader>{t('table.stacking')}</DataTableHeader>
<DataTableHeader>{t('table.status')}</DataTableHeader>
<DataTableHeader>
<span className="sr-only">{t('edit')}</span>
</DataTableHeader>
</DataTableHead>
<DataTableBody>
{discounts.map((discount) => (
<Row
key={discount.id}
discount={discount}
onEdit={() => setEditing({ discount })}
now={loadedAt}
t={t}
format={format}
/>
))}
</DataTableBody>
</DataTable>
)}
{editing ? (
@@ -188,13 +196,13 @@ function Row({
discount.translations.en?.name ?? discount.translations.vi?.name ?? discount.code ?? '—';
return (
<tr className="hover:bg-ink-50/60">
<td className="px-4 py-3">
<DataTableRow>
<DataTableCell>
<div className="flex items-center gap-2">
{discount.trigger === 'CODE' ? (
<Ticket className="text-ink-400 size-4 shrink-0" />
<Ticket className="text-ink-500 size-4 shrink-0" />
) : (
<Tag className="text-ink-400 size-4 shrink-0" />
<Tag className="text-ink-500 size-4 shrink-0" />
)}
<div>
<div className="font-medium">{name}</div>
@@ -208,39 +216,39 @@ function Row({
</div>
</div>
</div>
</td>
</DataTableCell>
<td className="px-4 py-3 font-medium">
<DataTableCell className="font-medium">
{discount.type === 'PERCENTAGE'
? `${discount.value}%`
: formatMoney({ amount: discount.value, currency: 'VND' }, format)}
</td>
</DataTableCell>
<td className="text-ink-600 px-4 py-3 text-xs">
<DataTableCell className="text-ink-600 text-xs">
<Window discount={discount} t={t} format={format} />
</td>
</DataTableCell>
<td className="px-4 py-3">
<DataTableCell>
{discount.usageLimit === null
? `${discount.usageCount} · ${t('unlimited')}`
: `${discount.usageCount} / ${discount.usageLimit}`}
</td>
</DataTableCell>
<td className="text-ink-600 px-4 py-3 text-xs">
<DataTableCell className="text-ink-600 text-xs">
{discount.stackable ? t('stackable') : t('exclusive')} · {t('priorityShort')}{' '}
{discount.priority}
</td>
</DataTableCell>
<td className="px-4 py-3">
<DataTableCell>
<StatusBadge discount={discount} now={now} t={t} />
</td>
</DataTableCell>
<td className="px-4 py-3 text-right">
<DataTableCell className="text-right">
<Button variant="outline" size="sm" onClick={onEdit}>
{t('edit')}
</Button>
</td>
</tr>
</DataTableCell>
</DataTableRow>
);
}
@@ -5,7 +5,19 @@ import { useCallback, useEffect, useState } from 'react';
import { isApiClientError, type StockAdjustment } from '@sport/api-client';
import { PERMISSIONS, type InventoryLevel } from '@sport/types';
import { Badge, Button, Input, Skeleton, cn } from '@sport/ui';
import {
Badge,
Button,
DataTable,
DataTableBody,
DataTableCell,
DataTableHead,
DataTableHeader,
DataTableRow,
Input,
Skeleton,
cn,
} from '@sport/ui';
import { useSession } from '@/features/auth/session-provider';
import { browserApi } from '@/lib/api';
@@ -106,83 +118,79 @@ export function InventoryTable() {
<Skeleton className="h-12 w-full" />
</div>
) : (
<div className="border-ink-200 overflow-x-auto border bg-white">
<table className="min-w-3xl w-full text-sm">
<thead className="border-ink-200 bg-ink-50 border-b text-left">
<tr className="text-ink-500 text-[0.625rem] uppercase tracking-widest">
<th className="px-4 py-3 font-semibold">{t('table.sku')}</th>
<th className="px-4 py-3 font-semibold">{t('table.product')}</th>
<th className="px-4 py-3 font-semibold">{t('table.onHand')}</th>
<th className="px-4 py-3 font-semibold">{t('table.reserved')}</th>
<th className="px-4 py-3 font-semibold">{t('table.available')}</th>
<th className="px-4 py-3 font-semibold" />
</tr>
</thead>
<tbody className="divide-ink-100 divide-y">
{levels.map((level) => (
<tr key={`${level.variantId}-${level.locationId}`}>
<td className="px-4 py-3 font-mono text-xs">{level.sku}</td>
<td className="px-4 py-3">
<p className="truncate">{level.productName}</p>
<p className="text-ink-400 text-xs">{level.variantTitle}</p>
</td>
<td className="px-4 py-3">{level.onHand}</td>
<td className="text-ink-500 px-4 py-3">{level.reserved}</td>
<td className="px-4 py-3">
<span
className={cn(
'font-medium',
level.available === 0
? 'text-danger'
: level.available <= LOW_STOCK
? 'text-warning'
: '',
)}
>
{level.available}
</span>
{level.available === 0 ? (
<Badge variant="neutral" className="ml-2">
{t('outOfStock')}
</Badge>
) : null}
</td>
<td className="px-4 py-3 text-right">
{!can(PERMISSIONS.INVENTORY_UPDATE) ? null : editing === level.variantId ? (
<div className="flex items-center justify-end gap-2">
<Input
type="number"
value={delta}
onChange={(event) => setDelta(event.target.value)}
placeholder="+10"
className="h-9 w-24"
aria-label={t('deltaLabel')}
/>
<Button size="sm" disabled={busy} onClick={() => void adjust(level)}>
{t('apply')}
</Button>
<Button size="sm" variant="ghost" onClick={() => setEditing(null)}>
{t('cancel')}
</Button>
</div>
) : (
<Button
size="sm"
variant="secondary"
onClick={() => {
setEditing(level.variantId);
setDelta('');
}}
>
{t('adjust')}
</Button>
<DataTable>
<DataTableHead>
<DataTableHeader>{t('table.sku')}</DataTableHeader>
<DataTableHeader>{t('table.product')}</DataTableHeader>
<DataTableHeader>{t('table.onHand')}</DataTableHeader>
<DataTableHeader>{t('table.reserved')}</DataTableHeader>
<DataTableHeader>{t('table.available')}</DataTableHeader>
<DataTableHeader />
</DataTableHead>
<DataTableBody>
{levels.map((level) => (
<DataTableRow key={`${level.variantId}-${level.locationId}`}>
<DataTableCell className="font-mono text-xs">{level.sku}</DataTableCell>
<DataTableCell>
<p className="truncate">{level.productName}</p>
<p className="text-ink-500 text-xs">{level.variantTitle}</p>
</DataTableCell>
<DataTableCell>{level.onHand}</DataTableCell>
<DataTableCell className="text-ink-500">{level.reserved}</DataTableCell>
<DataTableCell>
<span
className={cn(
'font-medium',
level.available === 0
? 'text-danger'
: level.available <= LOW_STOCK
? 'text-warning'
: '',
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
>
{level.available}
</span>
{level.available === 0 ? (
<Badge variant="neutral" className="ml-2">
{t('outOfStock')}
</Badge>
) : null}
</DataTableCell>
<DataTableCell className="text-right">
{!can(PERMISSIONS.INVENTORY_UPDATE) ? null : editing === level.variantId ? (
<div className="flex items-center justify-end gap-2">
<Input
type="number"
value={delta}
onChange={(event) => setDelta(event.target.value)}
placeholder="+10"
className="h-9 w-24"
aria-label={t('deltaLabel')}
/>
<Button size="sm" disabled={busy} onClick={() => void adjust(level)}>
{t('apply')}
</Button>
<Button size="sm" variant="ghost" onClick={() => setEditing(null)}>
{t('cancel')}
</Button>
</div>
) : (
<Button
size="sm"
variant="secondary"
onClick={() => {
setEditing(level.variantId);
setDelta('');
}}
>
{t('adjust')}
</Button>
)}
</DataTableCell>
</DataTableRow>
))}
</DataTableBody>
</DataTable>
)}
</div>
);
@@ -174,7 +174,7 @@ export function MediaLibrary() {
</div>
<div className="p-2">
<p className="text-ink-600 truncate text-xs">{asset.altText ?? asset.storageKey}</p>
<p className="text-ink-400 text-[0.625rem]">
<p className="text-ink-500 text-eyebrow">
{asset.width && asset.height ? `${asset.width}×${asset.height} · ` : ''}
{Math.round(asset.sizeBytes / 1024)} KB
</p>
@@ -183,7 +183,7 @@ export function MediaLibrary() {
<button
type="button"
onClick={() => void remove(asset)}
className="rounded-card bg-ink-950/80 absolute right-1 top-1 px-2 py-1 text-[0.625rem] font-semibold uppercase tracking-widest text-white opacity-0 transition-opacity group-hover:opacity-100"
className="rounded-card bg-ink-950/80 text-eyebrow absolute right-1 top-1 px-2 py-1 uppercase text-white opacity-0 transition-opacity group-hover:opacity-100"
>
{t('delete')}
</button>
@@ -137,7 +137,7 @@ export function OrderDetail({ orderId }: { orderId: string }) {
</div>
{next.includes('FULFILLED') ? (
<p className="text-ink-400 text-xs">{t('fulfilHint')}</p>
<p className="text-ink-500 text-xs">{t('fulfilHint')}</p>
) : null}
</section>
) : null}
@@ -154,7 +154,7 @@ export function OrderDetail({ orderId }: { orderId: string }) {
<td className="px-6 py-3">
<p className="font-medium">{line.productName}</p>
<p className="text-ink-500 text-xs">{line.variantTitle}</p>
<p className="text-ink-400 font-mono text-[0.625rem]">{line.sku}</p>
<p className="text-ink-500 text-eyebrow font-mono">{line.sku}</p>
</td>
<td className="px-6 py-3 text-right tabular-nums">
{money(line.unitPrice.amount)} × {line.quantity}
+56 -46
View File
@@ -6,7 +6,19 @@ import { useEffect, useState } from 'react';
import { isApiClientError } from '@sport/api-client';
import { PERMISSIONS, type OrderListItem, type OrderStatus } from '@sport/types';
import { Badge, Button, Input, Skeleton, cn } from '@sport/ui';
import {
Badge,
Button,
DataTable,
DataTableBody,
DataTableCell,
DataTableHead,
DataTableHeader,
DataTableRow,
Input,
Skeleton,
cn,
} from '@sport/ui';
import { useSession } from '@/features/auth/session-provider';
import { browserApi } from '@/lib/api';
@@ -118,51 +130,49 @@ export function OrdersTable() {
) : orders.length === 0 ? (
<p className="text-ink-500 py-16 text-center text-sm">{t('empty')}</p>
) : (
<div className="border-ink-200 overflow-x-auto border bg-white">
<table className="min-w-3xl w-full text-sm">
<thead className="border-ink-200 bg-ink-50 border-b text-left">
<tr className="text-ink-500 text-[0.625rem] uppercase tracking-widest">
<th className="px-4 py-3 font-semibold">{t('column.order')}</th>
<th className="px-4 py-3 font-semibold">{t('column.customer')}</th>
<th className="px-4 py-3 font-semibold">{t('column.status')}</th>
<th className="px-4 py-3 font-semibold">{t('column.items')}</th>
<th className="px-4 py-3 font-semibold">{t('column.total')}</th>
<th className="px-4 py-3 font-semibold">{t('column.placed')}</th>
<th className="px-4 py-3" />
</tr>
</thead>
<tbody className="divide-ink-100 divide-y">
{orders.map((order) => (
<tr key={order.id} className="hover:bg-ink-50/60">
<td className="px-4 py-3 font-mono text-xs font-semibold">
<Link href={`/orders/${order.id}`} className="hover:underline">
{order.orderNumber}
</Link>
</td>
<td className="px-4 py-3">
<p className="font-medium">{order.customerName}</p>
<p className="text-ink-500 text-xs">{order.email}</p>
</td>
<td className="px-4 py-3">
<Badge variant={STATUS_VARIANT[order.status]}>
{t(`status.${order.status}`)}
</Badge>
</td>
<td className="px-4 py-3 tabular-nums">{order.itemCount}</td>
<td className="px-4 py-3 tabular-nums">{formatMoney(order.total, format)}</td>
<td className="text-ink-500 px-4 py-3 text-xs">
{formatDateTime(order.placedAt, format)}
</td>
<td className="px-4 py-3 text-right">
<Button size="sm" variant="secondary" asChild>
<Link href={`/orders/${order.id}`}>{t('view')}</Link>
</Button>
</td>
</tr>
))}
</tbody>
</table>
</div>
<DataTable>
<DataTableHead>
<DataTableHeader>{t('column.order')}</DataTableHeader>
<DataTableHeader>{t('column.customer')}</DataTableHeader>
<DataTableHeader>{t('column.status')}</DataTableHeader>
<DataTableHeader>{t('column.items')}</DataTableHeader>
<DataTableHeader>{t('column.total')}</DataTableHeader>
<DataTableHeader>{t('column.placed')}</DataTableHeader>
<DataTableHeader />
</DataTableHead>
<DataTableBody>
{orders.map((order) => (
<DataTableRow key={order.id}>
<DataTableCell className="font-mono text-xs font-semibold">
<Link href={`/orders/${order.id}`} className="hover:underline">
{order.orderNumber}
</Link>
</DataTableCell>
<DataTableCell>
<p className="font-medium">{order.customerName}</p>
<p className="text-ink-500 text-xs">{order.email}</p>
</DataTableCell>
<DataTableCell>
<Badge variant={STATUS_VARIANT[order.status]}>
{t(`status.${order.status}`)}
</Badge>
</DataTableCell>
<DataTableCell className="tabular-nums">{order.itemCount}</DataTableCell>
<DataTableCell className="tabular-nums">
{formatMoney(order.total, format)}
</DataTableCell>
<DataTableCell className="text-ink-500 text-xs">
{formatDateTime(order.placedAt, format)}
</DataTableCell>
<DataTableCell className="text-right">
<Button size="sm" variant="secondary" asChild>
<Link href={`/orders/${order.id}`}>{t('view')}</Link>
</Button>
</DataTableCell>
</DataTableRow>
))}
</DataTableBody>
</DataTable>
)}
</div>
);
@@ -149,7 +149,7 @@ export function ProductEditor({ productId }: { productId: string }) {
>
{t(`tab.${value}`)}
{value === 'variants' ? (
<span className="text-ink-400 ml-2 text-xs">{product.variants.length}</span>
<span className="text-ink-500 ml-2 text-xs">{product.variants.length}</span>
) : null}
</button>
))}
@@ -230,7 +230,7 @@ export function ProductForm({
))}
</div>
</div>
<p className="text-ink-400 text-xs">{t('localeHint')}</p>
<p className="text-ink-500 text-xs">{t('localeHint')}</p>
<label className="block space-y-1.5">
<span className="text-xs font-semibold uppercase tracking-widest">{t('name')}</span>
@@ -303,7 +303,7 @@ export function ProductForm({
{t('addOption')}
</Button>
</div>
<p className="text-ink-400 text-xs">{t('optionsHint')}</p>
<p className="text-ink-500 text-xs">{t('optionsHint')}</p>
{options.map((option, optionIndex) => (
<OptionEditor
@@ -343,7 +343,7 @@ export function ProductForm({
/>
</label>
</div>
<p className="text-ink-400 text-xs">{t('generationHint')}</p>
<p className="text-ink-500 text-xs">{t('generationHint')}</p>
</section>
) : null}
@@ -422,9 +422,7 @@ function OptionEditor({
<div className="border-ink-200 space-y-3 border p-4">
<div className="flex flex-wrap items-end gap-3">
<label className="space-y-1.5">
<span className="text-ink-500 text-[0.625rem] font-semibold uppercase tracking-widest">
{t('optionKey')}
</span>
<span className="text-ink-500 text-eyebrow uppercase">{t('optionKey')}</span>
<Input
value={option.key}
onChange={(event) => onChange({ ...option, key: event.target.value })}
@@ -432,9 +430,7 @@ function OptionEditor({
/>
</label>
<label className="space-y-1.5">
<span className="text-ink-500 text-[0.625rem] font-semibold uppercase tracking-widest">
{t('optionName')}
</span>
<span className="text-ink-500 text-eyebrow uppercase">{t('optionName')}</span>
<Input
value={option.names[locale]}
onChange={(event) =>
@@ -7,7 +7,19 @@ import { useCallback, useEffect, useState } from 'react';
import { isApiClientError } from '@sport/api-client';
import { PERMISSIONS, type AdminProductListItem } from '@sport/types';
import { Badge, Button, Input, Skeleton, cn } from '@sport/ui';
import {
Badge,
Button,
DataTable,
DataTableBody,
DataTableCell,
DataTableHead,
DataTableHeader,
DataTableRow,
Input,
Skeleton,
cn,
} from '@sport/ui';
import { useSession } from '@/features/auth/session-provider';
import { browserApi } from '@/lib/api';
@@ -121,90 +133,86 @@ export function ProductsTable() {
) : items.length === 0 ? (
<p className="text-ink-500 py-12 text-center text-sm">{t('empty')}</p>
) : (
<div className="border-ink-200 overflow-x-auto border bg-white">
<table className="min-w-4xl w-full text-sm">
<thead className="border-ink-200 bg-ink-50 border-b text-left">
<tr className="text-ink-500 text-[0.625rem] uppercase tracking-widest">
<th className="px-4 py-3 font-semibold">{t('table.product')}</th>
<th className="px-4 py-3 font-semibold">{t('table.status')}</th>
<th className="px-4 py-3 font-semibold">{t('table.variants')}</th>
<th className="px-4 py-3 font-semibold">{t('table.stock')}</th>
<th className="px-4 py-3 font-semibold">{t('table.price')}</th>
<th className="px-4 py-3 font-semibold" />
</tr>
</thead>
<tbody className="divide-ink-100 divide-y">
{items.map((product) => (
<tr key={product.id}>
<td className="px-4 py-3">
<div className="flex items-center gap-3">
<div className="bg-ink-100 relative size-10 shrink-0 overflow-hidden">
{product.thumbnailUrl ? (
<Image
src={product.thumbnailUrl}
alt=""
fill
sizes="40px"
className="object-cover"
/>
) : null}
</div>
<div className="min-w-0">
<Link
href={`/products/${product.id}`}
className="block truncate font-medium hover:underline"
>
{product.name}
</Link>
<p className="text-ink-400 truncate text-xs">
{product.brandName ?? '—'} · {product.slug}
</p>
</div>
</div>
</td>
<td className="px-4 py-3">
<Badge variant={STATUS_VARIANT[product.status]}>
{t(`status.${product.status}`)}
</Badge>
</td>
<td className="text-ink-600 px-4 py-3">{product.variantCount}</td>
<td className="px-4 py-3">
<span className={cn(product.totalStock === 0 && 'text-danger')}>
{product.totalStock}
</span>
</td>
<td className="text-ink-600 px-4 py-3">
{product.priceRange
? product.priceRange.min.amount === product.priceRange.max.amount
? formatMoney(product.priceRange.min, format)
: `${formatMoney(product.priceRange.min, format)} – ${formatMoney(product.priceRange.max, format)}`
: '—'}
</td>
<td className="px-4 py-3">
{/* Flex, not inline spacing: the labels are translated, and
in Vietnamese "NGỪNG BÁN" is long enough to wrap the two
buttons onto separate lines without an explicit row. */}
<div className="flex items-center justify-end gap-2">
<Button size="sm" variant="secondary" asChild>
<Link href={`/products/${product.id}`}>{t('edit')}</Link>
</Button>
{can(PERMISSIONS.PRODUCT_PUBLISH) ? (
<Button
size="sm"
variant={product.status === 'ACTIVE' ? 'ghost' : 'default'}
disabled={busyId === product.id}
onClick={() => void toggleStatus(product)}
>
{product.status === 'ACTIVE' ? t('unpublish') : t('publish')}
</Button>
<DataTable>
<DataTableHead>
<DataTableHeader>{t('table.product')}</DataTableHeader>
<DataTableHeader>{t('table.status')}</DataTableHeader>
<DataTableHeader>{t('table.variants')}</DataTableHeader>
<DataTableHeader>{t('table.stock')}</DataTableHeader>
<DataTableHeader>{t('table.price')}</DataTableHeader>
<th className="px-4 py-3 font-semibold" />
</DataTableHead>
<DataTableBody>
{items.map((product) => (
<DataTableRow key={product.id}>
<DataTableCell>
<div className="flex items-center gap-3">
<div className="bg-ink-100 relative size-10 shrink-0 overflow-hidden">
{product.thumbnailUrl ? (
<Image
src={product.thumbnailUrl}
alt=""
fill
sizes="40px"
className="object-cover"
/>
) : null}
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
<div className="min-w-0">
<Link
href={`/products/${product.id}`}
className="block truncate font-medium hover:underline"
>
{product.name}
</Link>
<p className="text-ink-500 truncate text-xs">
{product.brandName ?? '—'} · {product.slug}
</p>
</div>
</div>
</DataTableCell>
<DataTableCell>
<Badge variant={STATUS_VARIANT[product.status]}>
{t(`status.${product.status}`)}
</Badge>
</DataTableCell>
<DataTableCell className="text-ink-600">{product.variantCount}</DataTableCell>
<DataTableCell>
<span className={cn(product.totalStock === 0 && 'text-danger')}>
{product.totalStock}
</span>
</DataTableCell>
<DataTableCell className="text-ink-600">
{product.priceRange
? product.priceRange.min.amount === product.priceRange.max.amount
? formatMoney(product.priceRange.min, format)
: `${formatMoney(product.priceRange.min, format)} – ${formatMoney(product.priceRange.max, format)}`
: '—'}
</DataTableCell>
<DataTableCell>
{/* Flex, not inline spacing: the labels are translated, and
in Vietnamese "NGỪNG BÁN" is long enough to wrap the two
buttons onto separate lines without an explicit row. */}
<div className="flex items-center justify-end gap-2">
<Button size="sm" variant="secondary" asChild>
<Link href={`/products/${product.id}`}>{t('edit')}</Link>
</Button>
{can(PERMISSIONS.PRODUCT_PUBLISH) ? (
<Button
size="sm"
variant={product.status === 'ACTIVE' ? 'ghost' : 'default'}
disabled={busyId === product.id}
onClick={() => void toggleStatus(product)}
>
{product.status === 'ACTIVE' ? t('unpublish') : t('publish')}
</Button>
) : null}
</div>
</DataTableCell>
</DataTableRow>
))}
</DataTableBody>
</DataTable>
)}
</div>
);
@@ -140,7 +140,7 @@ export function VariantGrid({
<div className="border-ink-200 overflow-x-auto border bg-white">
<table className="min-w-3xl w-full text-sm">
<thead className="border-ink-200 bg-ink-50 border-b text-left">
<tr className="text-ink-500 text-[0.625rem] uppercase tracking-widest">
<tr className="text-ink-500 text-eyebrow uppercase">
<th className="px-4 py-3 font-semibold">{t('grid.variant')}</th>
<th className="px-4 py-3 font-semibold">{t('grid.sku')}</th>
<th className="px-4 py-3 font-semibold">{t('grid.price')}</th>
@@ -202,7 +202,7 @@ export function VariantGrid({
<span className={cn(variant.available === 0 && 'text-danger')}>
{variant.available}
</span>
<span className="text-ink-400 ml-1 text-xs">/ {variant.onHand}</span>
<span className="text-ink-500 ml-1 text-xs">/ {variant.onHand}</span>
</td>
</tr>
);
@@ -211,7 +211,7 @@ export function VariantGrid({
</table>
</div>
<p className="text-ink-400 text-xs">{t('stockHint')}</p>
<p className="text-ink-500 text-xs">{t('stockHint')}</p>
</div>
);
}
@@ -1,18 +1,20 @@
'use client';
import { Star } from 'lucide-react';
import { useFormatter, useTranslations } from 'next-intl';
import { useCallback, useEffect, useState } from 'react';
import { isApiClientError } from '@sport/api-client';
import { PERMISSIONS, type AdminReview, type ReviewStatus } from '@sport/types';
import { Badge, Button, Input, Skeleton, cn } from '@sport/ui';
import { Badge, Button, EmptyState, Input, Rating, SegmentedControl, Skeleton } from '@sport/ui';
import { useSession } from '@/features/auth/session-provider';
import { browserApi } from '@/lib/api';
import { formatDateTime } from '@/lib/format';
type StatusFilter = ReviewStatus | 'ALL';
const STATUS_FILTERS = ['PENDING', 'APPROVED', 'REJECTED', 'ALL'] as const satisfies readonly (
ReviewStatus | 'ALL'
)[];
type StatusFilter = (typeof STATUS_FILTERS)[number];
const STATUS_VARIANT = {
PENDING: 'warning',
@@ -87,22 +89,12 @@ export function ReviewsQueue() {
aria-label={t('searchPlaceholder')}
/>
<div className="border-ink-200 rounded-card flex overflow-hidden border">
{(['PENDING', 'APPROVED', 'REJECTED', 'ALL'] as const).map((value) => (
<button
key={value}
type="button"
aria-pressed={status === value}
onClick={() => setStatus(value)}
className={cn(
'px-3 py-2 text-sm transition-colors',
status === value ? 'bg-ink-950 text-white' : 'hover:bg-ink-100 bg-white',
)}
>
{t(`filter.${value}`)}
</button>
))}
</div>
<SegmentedControl
label={t('searchPlaceholder')}
value={status}
onChange={setStatus}
options={STATUS_FILTERS.map((value) => ({ value, label: t(`filter.${value}`) }))}
/>
</div>
{error ? (
@@ -117,9 +109,7 @@ export function ReviewsQueue() {
<Skeleton className="h-28 w-full" />
</div>
) : reviews.length === 0 ? (
<p className="text-ink-500 border-ink-200 border border-dashed p-8 text-center text-sm">
{status === 'PENDING' ? t('emptyQueue') : t('empty')}
</p>
<EmptyState title={status === 'PENDING' ? t('emptyQueue') : t('empty')} />
) : (
<ul className="space-y-3">
{reviews.map((review) => (
@@ -159,7 +149,7 @@ function ReviewCard({
<div className="flex flex-wrap items-start justify-between gap-3">
<div className="space-y-1">
<div className="flex items-center gap-2">
<Stars rating={review.rating} />
<Rating value={review.rating} size="sm" />
<span className="text-sm font-semibold">{review.title || t('untitled')}</span>
</div>
<p className="text-ink-500 text-xs">
@@ -173,7 +163,7 @@ function ReviewCard({
{review.body ? (
<p className="text-ink-700 whitespace-pre-line text-sm">{review.body}</p>
) : (
<p className="text-ink-400 text-sm italic">{t('noBody')}</p>
<p className="text-ink-500 text-sm italic">{t('noBody')}</p>
)}
<p className="text-ink-500 text-xs">
@@ -235,17 +225,3 @@ function ReviewCard({
</li>
);
}
function Stars({ rating }: { rating: number }) {
return (
<span className="flex items-center gap-0.5" aria-label={`${rating}/5`}>
{[1, 2, 3, 4, 5].map((star) => (
<Star
key={star}
aria-hidden
className={cn('size-3.5', star <= rating ? 'fill-ink-950 text-ink-950' : 'text-ink-300')}
/>
))}
</span>
);
}
@@ -87,7 +87,7 @@ export function RolesPanel() {
)}
>
<span>{role.name}</span>
<span className="text-ink-400 text-xs">{role.userCount}</span>
<span className="text-ink-500 text-xs">{role.userCount}</span>
</button>
))}
</nav>
@@ -110,9 +110,7 @@ export function RolesPanel() {
<div className="mt-6 space-y-5">
{groups.map((group) => (
<div key={group.resource}>
<h3 className="text-ink-400 text-[0.625rem] font-semibold uppercase tracking-widest">
{group.resource}
</h3>
<h3 className="text-ink-500 text-eyebrow uppercase">{group.resource}</h3>
<ul className="mt-2 flex flex-wrap gap-1.5">
{group.permissions.map((permission) => {
const has = granted.has(permission.key);
@@ -123,7 +121,7 @@ export function RolesPanel() {
'rounded-card inline-flex items-center gap-1.5 border px-2 py-1 text-xs',
has
? 'border-success/40 bg-success/10 text-ink-950'
: 'border-ink-200 text-ink-400',
: 'border-ink-200 text-ink-500',
)}
>
<span aria-hidden>{has ? '✓' : '·'}</span>
@@ -5,7 +5,16 @@ import { useEffect, useState } from 'react';
import { isApiClientError } from '@sport/api-client';
import { PERMISSIONS, type UserSummary } from '@sport/types';
import { Badge, Skeleton } from '@sport/ui';
import {
Badge,
DataTable,
DataTableBody,
DataTableCell,
DataTableHead,
DataTableHeader,
DataTableRow,
Skeleton,
} from '@sport/ui';
import { useSession } from '@/features/auth/session-provider';
import { browserApi } from '@/lib/api';
@@ -65,43 +74,39 @@ export function UsersTable() {
}
return (
<div className="border-ink-200 overflow-x-auto border bg-white">
<table className="min-w-3xl w-full text-sm">
<thead className="border-ink-200 bg-ink-50 border-b text-left">
<tr className="text-ink-500 text-[0.625rem] uppercase tracking-widest">
<th className="px-4 py-3 font-semibold">{t('table.name')}</th>
<th className="px-4 py-3 font-semibold">{t('table.email')}</th>
<th className="px-4 py-3 font-semibold">{t('table.type')}</th>
<th className="px-4 py-3 font-semibold">{t('table.roles')}</th>
<th className="px-4 py-3 font-semibold">{t('table.status')}</th>
<th className="px-4 py-3 font-semibold">{t('table.lastLogin')}</th>
</tr>
</thead>
<tbody className="divide-ink-100 divide-y">
{users.map((user) => (
<tr key={user.id}>
<td className="px-4 py-3 font-medium">{user.displayName}</td>
<td className="text-ink-600 px-4 py-3">{user.email}</td>
<td className="text-ink-600 px-4 py-3">{user.type}</td>
<td className="px-4 py-3">
<div className="flex flex-wrap gap-1">
{user.roles.map((role) => (
<Badge key={role.id} variant="outline">
{role.name}
</Badge>
))}
</div>
</td>
<td className="px-4 py-3">
<Badge variant={STATUS_VARIANT[user.status]}>{user.status}</Badge>
</td>
<td className="text-ink-500 px-4 py-3">
{user.lastLoginAt ? new Date(user.lastLoginAt).toLocaleString() : '—'}
</td>
</tr>
))}
</tbody>
</table>
</div>
<DataTable>
<DataTableHead>
<DataTableHeader>{t('table.name')}</DataTableHeader>
<DataTableHeader>{t('table.email')}</DataTableHeader>
<DataTableHeader>{t('table.type')}</DataTableHeader>
<DataTableHeader>{t('table.roles')}</DataTableHeader>
<DataTableHeader>{t('table.status')}</DataTableHeader>
<DataTableHeader>{t('table.lastLogin')}</DataTableHeader>
</DataTableHead>
<DataTableBody>
{users.map((user) => (
<DataTableRow key={user.id}>
<DataTableCell className="font-medium">{user.displayName}</DataTableCell>
<DataTableCell className="text-ink-600">{user.email}</DataTableCell>
<DataTableCell className="text-ink-600">{user.type}</DataTableCell>
<DataTableCell>
<div className="flex flex-wrap gap-1">
{user.roles.map((role) => (
<Badge key={role.id} variant="outline">
{role.name}
</Badge>
))}
</div>
</DataTableCell>
<DataTableCell>
<Badge variant={STATUS_VARIANT[user.status]}>{user.status}</Badge>
</DataTableCell>
<DataTableCell className="text-ink-500">
{user.lastLoginAt ? new Date(user.lastLoginAt).toLocaleString() : '—'}
</DataTableCell>
</DataTableRow>
))}
</DataTableBody>
</DataTable>
);
}
@@ -0,0 +1,84 @@
'use client';
import { useMemo } from 'react';
import {
PERMISSIONS,
type AdminBrand,
type AdminCategory,
type AdminCollection,
} from '@sport/types';
import { browserApi } from '@/lib/api';
import { TaxonomyManager, toPayload, type TaxonomyAdapter } from './taxonomy-manager';
// The API caps `perPage` at 100 (PAGINATION_DEFAULTS.maxPerPage); asking for
// more is a validation error, not a silent clamp. Taxonomies are small enough
// that one page holds them — a store with more than 100 brands would need
// pagination here, and would notice immediately because the list stops short.
const PER_PAGE = 100;
export function BrandsPanel() {
const adapter = useMemo<TaxonomyAdapter<AdminBrand>>(
() => ({
list: async () => [
...(await browserApi.taxonomyAdmin.brands.list({ perPage: PER_PAGE })).items,
],
create: (draft) => browserApi.taxonomyAdmin.brands.create(toPayload(draft)),
update: (id, draft) => browserApi.taxonomyAdmin.brands.update(id, toPayload(draft)),
remove: (id) => browserApi.taxonomyAdmin.brands.remove(id),
readPermission: PERMISSIONS.BRAND_READ,
managePermission: PERMISSIONS.BRAND_MANAGE,
}),
[],
);
return <TaxonomyManager titleKey="brands" adapter={adapter} />;
}
export function CategoriesPanel() {
const adapter = useMemo<TaxonomyAdapter<AdminCategory>>(
() => ({
list: async () => [
...(await browserApi.taxonomyAdmin.categories.list({ perPage: PER_PAGE })).items,
],
create: (draft) => browserApi.taxonomyAdmin.categories.create(toPayload(draft)),
update: (id, draft) => browserApi.taxonomyAdmin.categories.update(id, toPayload(draft)),
remove: (id) => browserApi.taxonomyAdmin.categories.remove(id),
readPermission: PERMISSIONS.CATEGORY_READ,
managePermission: PERMISSIONS.CATEGORY_MANAGE,
metaHeader: 'Path',
// The materialised path, indented by depth — the list is already sorted
// by path, so the indentation lines up into a readable tree.
renderMeta: (row) => (
<span className="font-mono" style={{ paddingLeft: `${row.depth * 0.75}rem` }}>
{row.path}
</span>
),
}),
[],
);
return <TaxonomyManager titleKey="categories" adapter={adapter} />;
}
export function CollectionsPanel() {
const adapter = useMemo<TaxonomyAdapter<AdminCollection>>(
() => ({
list: async () => [
...(await browserApi.taxonomyAdmin.collections.list({ perPage: PER_PAGE })).items,
],
create: (draft) => browserApi.taxonomyAdmin.collections.create(toPayload(draft)),
update: (id, draft) => browserApi.taxonomyAdmin.collections.update(id, toPayload(draft)),
remove: (id) => browserApi.taxonomyAdmin.collections.remove(id),
readPermission: PERMISSIONS.COLLECTION_READ,
managePermission: PERMISSIONS.COLLECTION_MANAGE,
metaHeader: 'Type',
renderMeta: (row) => row.type,
}),
[],
);
return <TaxonomyManager titleKey="collections" adapter={adapter} />;
}
@@ -0,0 +1,500 @@
'use client';
import { Plus } from 'lucide-react';
import { useTranslations } from 'next-intl';
import { useCallback, useEffect, useId, useState, type FormEvent } from 'react';
import { isApiClientError } from '@sport/api-client';
import { DEFAULT_LOCALE, LOCALES, type Locale, type Permission } from '@sport/types';
import {
Badge,
Button,
DataTable,
DataTableBody,
DataTableCell,
DataTableHead,
DataTableHeader,
DataTableRow,
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
EmptyState,
Field,
Input,
Skeleton,
cn,
} from '@sport/ui';
import { useSession } from '@/features/auth/session-provider';
/**
* The shape every taxonomy row shares, which is most of one.
*
* Brands, categories and collections differ in three fields between them —
* a parent, a date window, a type — and are otherwise the same record with the
* same lifecycle. Three near-identical screens would have been the obvious
* thing to write and the wrong thing to maintain: the same reasoning that made
* promotions and coupons one entity (ADR-0020).
*/
export interface TaxonomyRow {
id: string;
name: string;
slug: string;
description: string | null;
isActive: boolean;
metaTitle: string | null;
metaDescription: string | null;
translations: Record<string, TaxonomyTranslationFields>;
productCount: number;
}
interface TaxonomyTranslationFields {
name: string;
slug: string;
description: string | null;
metaTitle: string | null;
metaDescription: string | null;
}
type Draft = {
name: string;
slug: string;
description: string;
isActive: boolean;
translations: Record<Locale, TaxonomyTranslationFields>;
};
const EMPTY_TRANSLATION: TaxonomyTranslationFields = {
name: '',
slug: '',
description: null,
metaTitle: null,
metaDescription: null,
};
function emptyDraft(): Draft {
return {
name: '',
slug: '',
description: '',
isActive: true,
translations: Object.fromEntries(
LOCALES.map((locale) => [locale, { ...EMPTY_TRANSLATION }]),
) as Draft['translations'],
};
}
function toDraft(row: TaxonomyRow): Draft {
return {
name: row.name,
slug: row.slug,
description: row.description ?? '',
isActive: row.isActive,
translations: Object.fromEntries(
LOCALES.map((locale) => [
locale,
{ ...EMPTY_TRANSLATION, ...(row.translations[locale] ?? {}) },
]),
) as Draft['translations'],
};
}
/** Everything the manager needs to talk to one specific entity's endpoints. */
export interface TaxonomyAdapter<TRow extends TaxonomyRow> {
list(): Promise<TRow[]>;
create(draft: Draft): Promise<unknown>;
update(id: string, draft: Draft): Promise<unknown>;
remove(id: string): Promise<void>;
readPermission: Permission;
managePermission: Permission;
/** Extra column between name and status, e.g. a category's path. */
renderMeta?: (row: TRow) => React.ReactNode;
metaHeader?: string;
}
export function TaxonomyManager<TRow extends TaxonomyRow>({
titleKey,
adapter,
}: {
titleKey: 'brands' | 'categories' | 'collections';
adapter: TaxonomyAdapter<TRow>;
}) {
const t = useTranslations('taxonomy');
const { can } = useSession();
const [rows, setRows] = useState<TRow[] | null>(null);
const [error, setError] = useState<string | null>(null);
const [editing, setEditing] = useState<{ row: TRow | null } | null>(null);
const [nonce, setNonce] = useState(0);
useEffect(() => {
let cancelled = false;
adapter
.list()
.then((result) => {
// Guarded, so a response arriving after unmount — or after a newer
// request — is discarded instead of writing to dead state.
if (!cancelled) {
setRows(result);
setError(null);
}
})
.catch((caught: unknown) => {
if (!cancelled) setError(isApiClientError(caught) ? caught.message : t('loadFailed'));
});
return () => {
cancelled = true;
};
}, [adapter, nonce, t]);
const reload = useCallback(() => setNonce((current) => current + 1), []);
if (!can(adapter.readPermission)) {
return <p className="text-ink-500 text-sm">{t('noPermission')}</p>;
}
const canManage = can(adapter.managePermission);
return (
<div className="space-y-4">
{canManage ? (
<div className="flex justify-end">
<Button onClick={() => setEditing({ row: null })}>
<Plus className="size-4" />
{t(`${titleKey}.create`)}
</Button>
</div>
) : null}
{error ? (
<p role="alert" className="text-danger text-sm">
{error}
</p>
) : null}
{!rows ? (
<div className="space-y-2" aria-busy="true">
<Skeleton className="h-12 w-full" />
<Skeleton className="h-12 w-full" />
<Skeleton className="h-12 w-full" />
</div>
) : rows.length === 0 ? (
<EmptyState
title={t(`${titleKey}.empty`)}
action={
canManage ? (
<Button onClick={() => setEditing({ row: null })}>{t(`${titleKey}.create`)}</Button>
) : undefined
}
/>
) : (
<DataTable minWidth="42rem">
<DataTableHead>
<DataTableHeader>{t('table.name')}</DataTableHeader>
{adapter.metaHeader ? <DataTableHeader>{adapter.metaHeader}</DataTableHeader> : null}
<DataTableHeader>{t('table.products')}</DataTableHeader>
<DataTableHeader>{t('table.status')}</DataTableHeader>
<DataTableHeader>
<span className="sr-only">{t('edit')}</span>
</DataTableHeader>
</DataTableHead>
<DataTableBody>
{rows.map((row) => (
<DataTableRow key={row.id}>
<DataTableCell>
<div className="font-medium">{row.name}</div>
<div className="text-ink-500 font-mono text-xs">/{row.slug}</div>
</DataTableCell>
{adapter.metaHeader ? (
<DataTableCell className="text-ink-600 text-xs">
{adapter.renderMeta?.(row)}
</DataTableCell>
) : null}
<DataTableCell className="tabular-nums">{row.productCount}</DataTableCell>
<DataTableCell>
<Badge variant={row.isActive ? 'success' : 'neutral'}>
{row.isActive ? t('active') : t('inactive')}
</Badge>
</DataTableCell>
<DataTableCell className="text-right">
{canManage ? (
<Button variant="outline" size="sm" onClick={() => setEditing({ row })}>
{t('edit')}
</Button>
) : null}
</DataTableCell>
</DataTableRow>
))}
</DataTableBody>
</DataTable>
)}
{editing ? (
<TaxonomyDialog
key={editing.row?.id ?? 'new'}
row={editing.row}
adapter={adapter}
titleKey={titleKey}
onClose={() => setEditing(null)}
onSaved={reload}
/>
) : null}
</div>
);
}
function TaxonomyDialog<TRow extends TaxonomyRow>({
row,
adapter,
titleKey,
onClose,
onSaved,
}: {
row: TRow | null;
adapter: TaxonomyAdapter<TRow>;
titleKey: string;
onClose: () => void;
onSaved: () => void;
}) {
const t = useTranslations('taxonomy');
const [draft, setDraft] = useState<Draft>(() => (row ? toDraft(row) : emptyDraft()));
const [locale, setLocale] = useState<Locale>(DEFAULT_LOCALE);
const [error, setError] = useState<string | null>(null);
const [saving, setSaving] = useState(false);
const [confirmingDelete, setConfirmingDelete] = useState(false);
const activeId = useId();
async function submit(event: FormEvent) {
event.preventDefault();
setError(null);
setSaving(true);
try {
if (row) {
await adapter.update(row.id, draft);
} else {
await adapter.create(draft);
}
onSaved();
onClose();
} catch (caught) {
setError(isApiClientError(caught) ? caught.message : t('saveFailed'));
} finally {
setSaving(false);
}
}
async function remove() {
if (!row) return;
setSaving(true);
try {
await adapter.remove(row.id);
onSaved();
onClose();
} catch (caught) {
// The API refuses a delete that would strand products; that message is
// the useful one, so it is surfaced verbatim rather than replaced.
setError(isApiClientError(caught) ? caught.message : t('deleteFailed'));
setConfirmingDelete(false);
} finally {
setSaving(false);
}
}
const fields = draft.translations[locale];
return (
<Dialog open onOpenChange={(open) => !open && onClose()}>
<DialogContent className="max-h-[90vh] overflow-y-auto sm:max-w-xl">
<DialogHeader>
<DialogTitle>{row ? t(`${titleKey}.edit`) : t(`${titleKey}.create`)}</DialogTitle>
<DialogDescription>{t('dialogHint')}</DialogDescription>
</DialogHeader>
<form id="taxonomy-form" onSubmit={submit} className="space-y-5">
<div className="grid gap-4 sm:grid-cols-2">
<Field label={t('name')} required>
{(props) => (
<Input
{...props}
value={draft.name}
onChange={(event) => setDraft({ ...draft, name: event.target.value })}
required
/>
)}
</Field>
<Field label={t('slug')} hint={t('slugHint')} required>
{(props) => (
<Input
{...props}
value={draft.slug}
onChange={(event) => setDraft({ ...draft, slug: event.target.value })}
required
/>
)}
</Field>
</div>
<Field label={t('description')}>
{(props) => (
<Input
{...props}
value={draft.description}
onChange={(event) => setDraft({ ...draft, description: event.target.value })}
/>
)}
</Field>
{/* ---- Per-locale copy ---- */}
<div className="space-y-3">
<div className="border-ink-200 flex gap-1 border-b">
{LOCALES.map((key) => (
<button
key={key}
type="button"
onClick={() => setLocale(key)}
aria-pressed={locale === key}
className={cn(
'text-eyebrow -mb-px cursor-pointer border-b-2 px-3 py-1.5 uppercase',
locale === key
? 'border-ink-950 text-ink-950'
: 'text-ink-500 hover:text-ink-700 border-transparent',
)}
>
{key}
{draft.translations[key].name.trim() ? '' : ' ·'}
</button>
))}
</div>
<div className="grid gap-4 sm:grid-cols-2">
<Field label={t('nameLocalised')}>
{(props) => (
<Input
{...props}
value={fields.name}
onChange={(event) =>
setDraft({
...draft,
translations: {
...draft.translations,
[locale]: { ...fields, name: event.target.value },
},
})
}
/>
)}
</Field>
<Field label={t('slugLocalised')} hint={t('slugLocalisedHint')}>
{(props) => (
<Input
{...props}
value={fields.slug}
onChange={(event) =>
setDraft({
...draft,
translations: {
...draft.translations,
[locale]: { ...fields, slug: event.target.value },
},
})
}
/>
)}
</Field>
</div>
</div>
<div className="flex items-start gap-2.5">
{/* useId + htmlFor rather than wrapping: the hint sits inside the
label too, and a screen reader would otherwise announce the
whole paragraph as the checkbox's name. */}
<input
id={activeId}
type="checkbox"
checked={draft.isActive}
onChange={(event) => setDraft({ ...draft, isActive: event.target.checked })}
className="border-ink-300 accent-ink-950 mt-0.5 size-4"
/>
<div>
<label htmlFor={activeId} className="block text-sm font-medium">
{t('active')}
</label>
<span className="text-ink-500 block text-xs">{t('activeHint')}</span>
</div>
</div>
{error ? (
<p role="alert" className="text-danger text-sm">
{error}
</p>
) : null}
</form>
<DialogFooter className="sm:justify-between">
{row ? (
<Button
type="button"
variant={confirmingDelete ? 'destructive' : 'ghost'}
disabled={saving}
onClick={() => (confirmingDelete ? void remove() : setConfirmingDelete(true))}
>
{confirmingDelete ? t('deleteConfirm') : t('delete')}
</Button>
) : (
<span />
)}
<div className="flex gap-2">
<Button type="button" variant="ghost" onClick={onClose}>
{t('cancel')}
</Button>
<Button type="submit" form="taxonomy-form" disabled={saving}>
{saving ? t('saving') : t('save')}
</Button>
</div>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
/** Shared payload mapping — the three entities send the same base fields. */
export function toPayload(draft: Draft) {
return {
name: draft.name.trim(),
slug: draft.slug.trim(),
description: draft.description.trim() || null,
isActive: draft.isActive,
// Only locales actually written; an empty name would fail the schema and
// block saving a brand named in one language, which is how they start.
translations: Object.fromEntries(
LOCALES.filter((locale) => draft.translations[locale].name.trim()).map((locale) => {
const fields = draft.translations[locale];
return [
locale,
{
name: fields.name.trim(),
slug: (fields.slug || draft.slug).trim(),
description: fields.description,
metaTitle: fields.metaTitle,
metaDescription: fields.metaDescription,
},
];
}),
),
};
}
+43
View File
@@ -429,5 +429,48 @@
"unpublish": "Unpublish",
"delete": "Retire",
"deleteConfirm": "Confirm retire"
},
"taxonomy": {
"noPermission": "You do not have permission to manage this.",
"loadFailed": "Could not load.",
"saveFailed": "Could not save.",
"deleteFailed": "Could not delete.",
"dialogHint": "The base name is the fallback; per-language copy overrides it.",
"table": {
"name": "Name",
"products": "Products",
"status": "Status"
},
"active": "Active",
"inactive": "Inactive",
"activeHint": "Inactive entries stay in the admin but disappear from the storefront.",
"name": "Name",
"slug": "URL slug",
"slugHint": "Lowercase, hyphens.",
"nameLocalised": "Name in this language",
"slugLocalised": "Slug in this language",
"slugLocalisedHint": "Leave blank to reuse the base slug.",
"description": "Description",
"edit": "Edit",
"delete": "Delete",
"deleteConfirm": "Confirm delete",
"cancel": "Cancel",
"save": "Save",
"saving": "Saving…",
"brands": {
"create": "New brand",
"edit": "Edit brand",
"empty": "No brands yet."
},
"categories": {
"create": "New category",
"edit": "Edit category",
"empty": "No categories yet."
},
"collections": {
"create": "New collection",
"edit": "Edit collection",
"empty": "No collections yet."
}
}
}
+43
View File
@@ -429,5 +429,48 @@
"unpublish": "Gỡ xuống",
"delete": "Ngừng dùng",
"deleteConfirm": "Xác nhận ngừng"
},
"taxonomy": {
"noPermission": "Bạn không có quyền quản lý mục này.",
"loadFailed": "Không tải được dữ liệu.",
"saveFailed": "Không lưu được.",
"deleteFailed": "Không xoá được.",
"dialogHint": "Tên cơ sở là giá trị dự phòng; nội dung theo ngôn ngữ sẽ ghi đè.",
"table": {
"name": "Tên",
"products": "Sản phẩm",
"status": "Trạng thái"
},
"active": "Đang bật",
"inactive": "Đã tắt",
"activeHint": "Mục đã tắt vẫn còn trong admin nhưng không hiển thị ngoài cửa hàng.",
"name": "Tên",
"slug": "Đường dẫn",
"slugHint": "Chữ thường, gạch nối.",
"nameLocalised": "Tên theo ngôn ngữ này",
"slugLocalised": "Đường dẫn theo ngôn ngữ này",
"slugLocalisedHint": "Để trống để dùng đường dẫn cơ sở.",
"description": "Mô tả",
"edit": "Sửa",
"delete": "Xoá",
"deleteConfirm": "Xác nhận xoá",
"cancel": "Huỷ",
"save": "Lưu",
"saving": "Đang lưu…",
"brands": {
"create": "Thương hiệu mới",
"edit": "Sửa thương hiệu",
"empty": "Chưa có thương hiệu nào."
},
"categories": {
"create": "Danh mục mới",
"edit": "Sửa danh mục",
"empty": "Chưa có danh mục nào."
},
"collections": {
"create": "Bộ sưu tập mới",
"edit": "Sửa bộ sưu tập",
"empty": "Chưa có bộ sưu tập nào."
}
}
}
@@ -0,0 +1,77 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import {
PERMISSIONS,
TOKEN_AUDIENCES,
type AdminBrand,
type AuthenticatedActor,
type OffsetPaginated,
} from '@sport/types';
import {
brandInputSchema,
taxonomyListQuerySchema,
type BrandInput,
type TaxonomyListQuery,
} from '@sport/validation';
import { CurrentActor } from '@/common/decorators/current-actor.decorator';
import {
RequireAudience,
RequirePermissions,
} from '@/common/decorators/require-permissions.decorator';
import { ZodValidationPipe } from '@/common/pipes/zod-validation.pipe';
import { BrandsAdminService } from './brands-admin.service';
@ApiTags('admin/brands')
@ApiBearerAuth()
@RequireAudience(TOKEN_AUDIENCES.ADMIN)
@Controller('admin/brands')
export class BrandsAdminController {
constructor(private readonly service: BrandsAdminService) {}
@Get()
@RequirePermissions(PERMISSIONS.BRAND_READ)
@ApiOperation({ summary: 'All brands, including inactive' })
list(
@Query(new ZodValidationPipe(taxonomyListQuerySchema)) query: TaxonomyListQuery,
): Promise<OffsetPaginated<AdminBrand>> {
return this.service.list(query);
}
@Get(':id')
@RequirePermissions(PERMISSIONS.BRAND_READ)
@ApiOperation({ summary: 'One entry, every locale' })
getById(@Param('id') id: string): Promise<AdminBrand> {
return this.service.getById(id);
}
@Post()
@RequirePermissions(PERMISSIONS.BRAND_MANAGE)
@ApiOperation({ summary: 'Create' })
create(
@Body(new ZodValidationPipe(brandInputSchema)) body: BrandInput,
@CurrentActor() actor: AuthenticatedActor,
): Promise<AdminBrand> {
return this.service.create(body, actor.userId);
}
@Patch(':id')
@RequirePermissions(PERMISSIONS.BRAND_MANAGE)
@ApiOperation({ summary: 'Update' })
update(
@Param('id') id: string,
@Body(new ZodValidationPipe(brandInputSchema)) body: BrandInput,
@CurrentActor() actor: AuthenticatedActor,
): Promise<AdminBrand> {
return this.service.update(id, body, actor.userId);
}
@Delete(':id')
@RequirePermissions(PERMISSIONS.BRAND_MANAGE)
@ApiOperation({ summary: 'Delete, if nothing depends on it' })
remove(@Param('id') id: string, @CurrentActor() actor: AuthenticatedActor): Promise<void> {
return this.service.remove(id, actor.userId);
}
}
@@ -0,0 +1,233 @@
import { Injectable, Logger } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { LOCALES, type AdminBrand, type OffsetPaginated } from '@sport/types';
import type { BrandInput, TaxonomyListQuery } from '@sport/validation';
import { AuditService } from '@/common/audit/audit.service';
import { AppException } from '@/common/errors/app.exception';
import { toDbLocale } from '@/common/i18n';
import { MediaUrlService } from '@/common/media/media-url.service';
import { PrismaService } from '@/infrastructure/prisma/prisma.service';
import { CACHE_KEYS } from '@/infrastructure/redis/cache-keys';
import { RedisService } from '@/infrastructure/redis/redis.service';
const select = {
id: true,
name: true,
slug: true,
description: true,
logoId: true,
isActive: true,
metaTitle: true,
metaDescription: true,
logo: { select: { storageKey: true } },
translations: true,
_count: { select: { products: true } },
} as const;
type Row = Prisma.BrandGetPayload<{ select: typeof select }>;
@Injectable()
export class BrandsAdminService {
private readonly logger = new Logger(BrandsAdminService.name);
constructor(
private readonly prisma: PrismaService,
private readonly audit: AuditService,
private readonly mediaUrl: MediaUrlService,
private readonly redis: RedisService,
) {}
async list(query: TaxonomyListQuery): Promise<OffsetPaginated<AdminBrand>> {
const where: Prisma.BrandWhereInput = {
...(query.activeOnly ? { isActive: true } : {}),
...(query.q ? { name: { contains: query.q, mode: 'insensitive' } } : {}),
};
const [rows, totalItems] = await Promise.all([
this.prisma.brand.findMany({
where,
orderBy: [{ name: 'asc' }],
skip: (query.page - 1) * query.perPage,
take: query.perPage,
select,
}),
this.prisma.brand.count({ where }),
]);
const totalPages = Math.max(1, Math.ceil(totalItems / query.perPage));
return {
items: rows.map((row) => this.toAdmin(row)),
pageInfo: {
page: query.page,
perPage: query.perPage,
totalItems,
totalPages,
hasNextPage: query.page < totalPages,
},
};
}
async getById(id: string): Promise<AdminBrand> {
const row = await this.prisma.brand.findUnique({ where: { id }, select });
if (!row) throw AppException.notFound('Brand');
return this.toAdmin(row);
}
async create(input: BrandInput, actorUserId: string): Promise<AdminBrand> {
await this.assertSlugFree(input.slug, null);
const created = await this.prisma.$transaction(async (tx) => {
const brand = await tx.brand.create({
data: {
name: input.name,
slug: input.slug,
description: input.description ?? null,
logoId: input.logoId ?? null,
isActive: input.isActive,
metaTitle: input.metaTitle ?? null,
metaDescription: input.metaDescription ?? null,
},
select: { id: true },
});
await this.writeTranslations(tx, brand.id, input);
return brand;
});
await this.afterWrite(actorUserId, 'brand.create', created.id, { slug: input.slug });
return this.getById(created.id);
}
async update(id: string, input: BrandInput, actorUserId: string): Promise<AdminBrand> {
const existing = await this.prisma.brand.findUnique({ where: { id }, select: { id: true } });
if (!existing) throw AppException.notFound('Brand');
await this.assertSlugFree(input.slug, id);
await this.prisma.$transaction(async (tx) => {
await tx.brand.update({
where: { id },
data: {
name: input.name,
slug: input.slug,
description: input.description ?? null,
logoId: input.logoId ?? null,
isActive: input.isActive,
metaTitle: input.metaTitle ?? null,
metaDescription: input.metaDescription ?? null,
},
});
await this.writeTranslations(tx, id, input);
});
await this.afterWrite(actorUserId, 'brand.update', id, { slug: input.slug });
return this.getById(id);
}
/**
* Refused while products still point here.
*
* `Product.brand` is `onDelete: SetNull`, so the database would happily
* delete this and quietly strip the brand from every product that used it.
* That is data loss with no warning, which is exactly why the check is here
* rather than left to the schema.
*/
async remove(id: string, actorUserId: string): Promise<void> {
const row = await this.prisma.brand.findUnique({
where: { id },
select: { slug: true, _count: { select: { products: true } } },
});
if (!row) throw AppException.notFound('Brand');
if (row._count.products > 0) {
throw AppException.conflict(
`${row._count.products} product(s) still use this brand. Reassign them first.`,
);
}
await this.prisma.brand.delete({ where: { id } });
await this.afterWrite(actorUserId, 'brand.delete', id, { slug: row.slug });
}
// ---- internals -----------------------------------------------------------
private async assertSlugFree(slug: string, exceptId: string | null): Promise<void> {
const clash = await this.prisma.brand.findFirst({
where: { slug, ...(exceptId ? { id: { not: exceptId } } : {}) },
select: { id: true },
});
if (clash) throw AppException.conflict(`The slug "${slug}" is already used by another brand.`);
}
private async writeTranslations(
tx: Prisma.TransactionClient,
brandId: string,
input: BrandInput,
): Promise<void> {
for (const locale of LOCALES) {
const fields = input.translations[locale];
if (!fields) continue;
const data = {
name: fields.name,
slug: fields.slug,
description: fields.description ?? null,
metaTitle: fields.metaTitle ?? null,
metaDescription: fields.metaDescription ?? null,
};
await tx.brandTranslation.upsert({
where: { brandId_locale: { brandId, locale: toDbLocale(locale) } },
update: data,
create: { brandId, locale: toDbLocale(locale), ...data },
});
}
}
private async afterWrite(
actorUserId: string,
action: string,
resourceId: string,
changes: Prisma.InputJsonValue,
): Promise<void> {
// Brands appear on product cards and in the listing facet rail, both of
// which are cached — see ProductsAdminService.afterWrite.
const dropped = await this.redis.deleteByPrefix(CACHE_KEYS.catalogPrefix());
this.logger.log(`${action} on ${resourceId}; dropped ${dropped} catalog cache key(s)`);
this.audit.record({ actorUserId, action, resourceType: 'Brand', resourceId, changes });
}
private toAdmin(row: Row): AdminBrand {
return {
id: row.id,
name: row.name,
slug: row.slug,
description: row.description,
logoId: row.logoId,
logoUrl: row.logo ? this.mediaUrl.url(row.logo.storageKey) : null,
isActive: row.isActive,
metaTitle: row.metaTitle,
metaDescription: row.metaDescription,
translations: Object.fromEntries(
row.translations.map((translation) => [
translation.locale.toLowerCase(),
{
name: translation.name,
slug: translation.slug,
description: translation.description,
metaTitle: translation.metaTitle,
metaDescription: translation.metaDescription,
},
]),
),
productCount: row._count.products,
};
}
}
+7 -2
View File
@@ -1,5 +1,9 @@
import { Module } from '@nestjs/common';
import { MediaUrlModule } from '@/common/media/media.module';
import { BrandsAdminController } from './brands-admin.controller';
import { BrandsAdminService } from './brands-admin.service';
import { BrandsController } from './brands.controller';
import { BrandsMapper } from './brands.mapper';
import { BrandsRepository } from './brands.repository';
@@ -12,8 +16,9 @@ import { BrandsService } from './brands.service';
* brand filter facet and (later) brand-level commercial terms all hang off it.
*/
@Module({
controllers: [BrandsController],
providers: [BrandsService, BrandsRepository, BrandsMapper],
imports: [MediaUrlModule],
controllers: [BrandsController, BrandsAdminController],
providers: [BrandsService, BrandsRepository, BrandsMapper, BrandsAdminService],
exports: [BrandsService],
})
export class BrandsModule {}
@@ -0,0 +1,77 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import {
PERMISSIONS,
TOKEN_AUDIENCES,
type AdminCategory,
type AuthenticatedActor,
type OffsetPaginated,
} from '@sport/types';
import {
categoryInputSchema,
taxonomyListQuerySchema,
type CategoryInput,
type TaxonomyListQuery,
} from '@sport/validation';
import { CurrentActor } from '@/common/decorators/current-actor.decorator';
import {
RequireAudience,
RequirePermissions,
} from '@/common/decorators/require-permissions.decorator';
import { ZodValidationPipe } from '@/common/pipes/zod-validation.pipe';
import { CategoriesAdminService } from './categories-admin.service';
@ApiTags('admin/categories')
@ApiBearerAuth()
@RequireAudience(TOKEN_AUDIENCES.ADMIN)
@Controller('admin/categories')
export class CategoriesAdminController {
constructor(private readonly service: CategoriesAdminService) {}
@Get()
@RequirePermissions(PERMISSIONS.CATEGORY_READ)
@ApiOperation({ summary: 'All categories, including inactive' })
list(
@Query(new ZodValidationPipe(taxonomyListQuerySchema)) query: TaxonomyListQuery,
): Promise<OffsetPaginated<AdminCategory>> {
return this.service.list(query);
}
@Get(':id')
@RequirePermissions(PERMISSIONS.CATEGORY_READ)
@ApiOperation({ summary: 'One entry, every locale' })
getById(@Param('id') id: string): Promise<AdminCategory> {
return this.service.getById(id);
}
@Post()
@RequirePermissions(PERMISSIONS.CATEGORY_MANAGE)
@ApiOperation({ summary: 'Create' })
create(
@Body(new ZodValidationPipe(categoryInputSchema)) body: CategoryInput,
@CurrentActor() actor: AuthenticatedActor,
): Promise<AdminCategory> {
return this.service.create(body, actor.userId);
}
@Patch(':id')
@RequirePermissions(PERMISSIONS.CATEGORY_MANAGE)
@ApiOperation({ summary: 'Update' })
update(
@Param('id') id: string,
@Body(new ZodValidationPipe(categoryInputSchema)) body: CategoryInput,
@CurrentActor() actor: AuthenticatedActor,
): Promise<AdminCategory> {
return this.service.update(id, body, actor.userId);
}
@Delete(':id')
@RequirePermissions(PERMISSIONS.CATEGORY_MANAGE)
@ApiOperation({ summary: 'Delete, if nothing depends on it' })
remove(@Param('id') id: string, @CurrentActor() actor: AuthenticatedActor): Promise<void> {
return this.service.remove(id, actor.userId);
}
}
@@ -0,0 +1,340 @@
import { Injectable, Logger } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { LOCALES, type AdminCategory, type OffsetPaginated } from '@sport/types';
import type { CategoryInput, TaxonomyListQuery } from '@sport/validation';
import { AuditService } from '@/common/audit/audit.service';
import { AppException } from '@/common/errors/app.exception';
import { toDbLocale } from '@/common/i18n';
import { MediaUrlService } from '@/common/media/media-url.service';
import { PrismaService } from '@/infrastructure/prisma/prisma.service';
import { CACHE_KEYS } from '@/infrastructure/redis/cache-keys';
import { RedisService } from '@/infrastructure/redis/redis.service';
const select = {
id: true,
parentId: true,
name: true,
slug: true,
path: true,
depth: true,
position: true,
description: true,
imageId: true,
isActive: true,
metaTitle: true,
metaDescription: true,
image: { select: { storageKey: true } },
translations: true,
_count: { select: { products: true, children: true } },
} as const;
type Row = Prisma.CategoryGetPayload<{ select: typeof select }>;
@Injectable()
export class CategoriesAdminService {
private readonly logger = new Logger(CategoriesAdminService.name);
constructor(
private readonly prisma: PrismaService,
private readonly audit: AuditService,
private readonly mediaUrl: MediaUrlService,
private readonly redis: RedisService,
) {}
async list(query: TaxonomyListQuery): Promise<OffsetPaginated<AdminCategory>> {
const where: Prisma.CategoryWhereInput = {
...(query.activeOnly ? { isActive: true } : {}),
...(query.q ? { name: { contains: query.q, mode: 'insensitive' } } : {}),
};
const [rows, totalItems] = await Promise.all([
this.prisma.category.findMany({
where,
// Tree order: the materialised path sorts parents immediately before
// their children, so the flat list reads as an indented hierarchy
// without a second pass to nest it.
orderBy: [{ path: 'asc' }],
skip: (query.page - 1) * query.perPage,
take: query.perPage,
select,
}),
this.prisma.category.count({ where }),
]);
const totalPages = Math.max(1, Math.ceil(totalItems / query.perPage));
return {
items: rows.map((row) => this.toAdmin(row)),
pageInfo: {
page: query.page,
perPage: query.perPage,
totalItems,
totalPages,
hasNextPage: query.page < totalPages,
},
};
}
async getById(id: string): Promise<AdminCategory> {
const row = await this.prisma.category.findUnique({ where: { id }, select });
if (!row) throw AppException.notFound('Category');
return this.toAdmin(row);
}
async create(input: CategoryInput, actorUserId: string): Promise<AdminCategory> {
const parent = await this.resolveParent(input.parentId ?? null);
const path = parent ? `${parent.path}/${input.slug}` : input.slug;
await this.assertPathFree(path, null);
const created = await this.prisma.$transaction(async (tx) => {
const category = await tx.category.create({
data: {
parentId: parent?.id ?? null,
name: input.name,
slug: input.slug,
path,
depth: parent ? parent.depth + 1 : 0,
position: input.position,
description: input.description ?? null,
imageId: input.imageId ?? null,
isActive: input.isActive,
metaTitle: input.metaTitle ?? null,
metaDescription: input.metaDescription ?? null,
},
select: { id: true },
});
await this.writeTranslations(tx, category.id, input);
return category;
});
await this.afterWrite(actorUserId, 'category.create', created.id, { path });
return this.getById(created.id);
}
async update(id: string, input: CategoryInput, actorUserId: string): Promise<AdminCategory> {
const existing = await this.prisma.category.findUnique({
where: { id },
select: { id: true, path: true, parentId: true },
});
if (!existing) throw AppException.notFound('Category');
const parent = await this.resolveParent(input.parentId ?? null);
/**
* A category cannot be moved under itself or under one of its own
* descendants.
*
* Checked on the materialised path rather than by walking parents: a
* descendant's path always starts with this category's path, so one string
* comparison catches every depth. Without it the subtree detaches from the
* root entirely — the rows survive but nothing can reach them, and every
* product beneath becomes unbrowsable.
*/
if (parent && (parent.id === id || parent.path.startsWith(`${existing.path}/`))) {
throw AppException.conflict('A category cannot be moved inside itself.');
}
const path = parent ? `${parent.path}/${input.slug}` : input.slug;
await this.assertPathFree(path, id);
await this.prisma.$transaction(async (tx) => {
await tx.category.update({
where: { id },
data: {
parentId: parent?.id ?? null,
name: input.name,
slug: input.slug,
path,
depth: parent ? parent.depth + 1 : 0,
position: input.position,
description: input.description ?? null,
imageId: input.imageId ?? null,
isActive: input.isActive,
metaTitle: input.metaTitle ?? null,
metaDescription: input.metaDescription ?? null,
},
});
await this.writeTranslations(tx, id, input);
/**
* Rewrite every descendant's path and depth.
*
* The whole point of a materialised path is that reads are cheap; the
* cost is that a rename or a move has to pay for it here. Skipping this
* leaves children pointing at a path their parent no longer has, and the
* subtree query — `LIKE 'men/running%'` — silently returns nothing.
*
* One statement rather than a recursive walk: `replace` on the prefix,
* and the depth delta applied uniformly, because every descendant moves
* by exactly the same number of levels as the node itself.
*/
if (existing.path !== path) {
const depthDelta = path.split('/').length - existing.path.split('/').length;
/**
* The `::int` casts are load-bearing.
*
* Prisma binds every JS number as `bigint`, and Postgres has no
* `substring(varchar, bigint)` — the statement fails with 42883
* "function does not exist" rather than a type-coercion warning. The
* transaction rolls back, so the rename appears to do nothing.
*/
await tx.$executeRaw`
UPDATE categories
SET path = ${path} || substring(path from ${existing.path.length + 1}::int),
depth = depth + ${depthDelta}::int
WHERE path LIKE ${`${existing.path}/%`}
`;
this.logger.log(`Re-pathed descendants of ${existing.path} -> ${path}`);
}
});
await this.afterWrite(actorUserId, 'category.update', id, { path });
return this.getById(id);
}
/**
* Hard delete, refused while anything depends on it.
*
* `Category.parent` is `onDelete: Restrict`, so the database would refuse a
* category with children anyway — but with a foreign-key error that means
* nothing to an operator. These checks say which of the two reasons applies.
*/
async remove(id: string, actorUserId: string): Promise<void> {
const row = await this.prisma.category.findUnique({
where: { id },
select: { path: true, _count: { select: { products: true, children: true } } },
});
if (!row) throw AppException.notFound('Category');
if (row._count.children > 0) {
throw AppException.conflict(
'Move or delete the sub-categories first — this one still has children.',
);
}
if (row._count.products > 0) {
throw AppException.conflict(
`${row._count.products} product(s) still use this category. Reassign them first.`,
);
}
await this.prisma.category.delete({ where: { id } });
await this.afterWrite(actorUserId, 'category.delete', id, { path: row.path });
}
// ---- internals -----------------------------------------------------------
private async resolveParent(parentId: string | null) {
if (!parentId) return null;
const parent = await this.prisma.category.findUnique({
where: { id: parentId },
select: { id: true, path: true, depth: true },
});
if (!parent) throw AppException.badRequest('The chosen parent category does not exist.');
return parent;
}
/** `path` is unique in the database; this turns the violation into English. */
private async assertPathFree(path: string, exceptId: string | null): Promise<void> {
const clash = await this.prisma.category.findFirst({
where: { path, ...(exceptId ? { id: { not: exceptId } } : {}) },
select: { id: true },
});
if (clash) {
throw AppException.conflict(`Another category already sits at "${path}".`);
}
}
private async writeTranslations(
tx: Prisma.TransactionClient,
categoryId: string,
input: CategoryInput,
): Promise<void> {
for (const locale of LOCALES) {
const fields = input.translations[locale];
if (!fields) continue;
const data = {
name: fields.name,
slug: fields.slug,
description: fields.description ?? null,
metaTitle: fields.metaTitle ?? null,
metaDescription: fields.metaDescription ?? null,
};
await tx.categoryTranslation.upsert({
where: { categoryId_locale: { categoryId, locale: toDbLocale(locale) } },
update: data,
create: { categoryId, locale: toDbLocale(locale), ...data },
});
}
}
/**
* Drop the catalog cache and record the change.
*
* The category tree backs the storefront's navigation menu, which is cached
* for fifteen minutes. Without this, renaming a category leaves the old label
* in the header until the TTL expires — see the same reasoning in
* ProductsAdminService.afterWrite and ReviewsService.moderate.
*/
private async afterWrite(
actorUserId: string,
action: string,
resourceId: string,
changes: Prisma.InputJsonValue,
): Promise<void> {
const dropped = await this.redis.deleteByPrefix(CACHE_KEYS.catalogPrefix());
this.logger.log(`${action} on ${resourceId}; dropped ${dropped} catalog cache key(s)`);
this.audit.record({
actorUserId,
action,
resourceType: 'Category',
resourceId,
changes,
});
}
private toAdmin(row: Row): AdminCategory {
return {
id: row.id,
parentId: row.parentId,
name: row.name,
slug: row.slug,
path: row.path,
depth: row.depth,
position: row.position,
description: row.description,
imageId: row.imageId,
imageUrl: row.image ? this.mediaUrl.url(row.image.storageKey) : null,
isActive: row.isActive,
metaTitle: row.metaTitle,
metaDescription: row.metaDescription,
translations: Object.fromEntries(
row.translations.map((translation) => [
translation.locale.toLowerCase(),
{
name: translation.name,
slug: translation.slug,
description: translation.description,
metaTitle: translation.metaTitle,
metaDescription: translation.metaDescription,
},
]),
),
productCount: row._count.products,
childCount: row._count.children,
};
}
}
@@ -1,7 +1,10 @@
import { Module } from '@nestjs/common';
import { MediaUrlModule } from '@/common/media/media.module';
import { CollectionsModule } from '@/modules/collections/collections.module';
import { CategoriesAdminController } from './categories-admin.controller';
import { CategoriesAdminService } from './categories-admin.service';
import { CategoriesController } from './categories.controller';
import { CategoriesMapper } from './categories.mapper';
import { CategoriesRepository } from './categories.repository';
@@ -17,9 +20,9 @@ import { CategoriesService } from './categories.service';
* graph rather than hidden behind an ambient global.
*/
@Module({
imports: [CollectionsModule],
controllers: [CategoriesController],
providers: [CategoriesService, CategoriesRepository, CategoriesMapper],
imports: [CollectionsModule, MediaUrlModule],
controllers: [CategoriesController, CategoriesAdminController],
providers: [CategoriesService, CategoriesRepository, CategoriesMapper, CategoriesAdminService],
exports: [CategoriesService],
})
export class CategoriesModule {}
@@ -0,0 +1,77 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import {
PERMISSIONS,
TOKEN_AUDIENCES,
type AdminCollection,
type AuthenticatedActor,
type OffsetPaginated,
} from '@sport/types';
import {
collectionInputSchema,
taxonomyListQuerySchema,
type CollectionInput,
type TaxonomyListQuery,
} from '@sport/validation';
import { CurrentActor } from '@/common/decorators/current-actor.decorator';
import {
RequireAudience,
RequirePermissions,
} from '@/common/decorators/require-permissions.decorator';
import { ZodValidationPipe } from '@/common/pipes/zod-validation.pipe';
import { CollectionsAdminService } from './collections-admin.service';
@ApiTags('admin/collections')
@ApiBearerAuth()
@RequireAudience(TOKEN_AUDIENCES.ADMIN)
@Controller('admin/collections')
export class CollectionsAdminController {
constructor(private readonly service: CollectionsAdminService) {}
@Get()
@RequirePermissions(PERMISSIONS.COLLECTION_READ)
@ApiOperation({ summary: 'All collections, including inactive' })
list(
@Query(new ZodValidationPipe(taxonomyListQuerySchema)) query: TaxonomyListQuery,
): Promise<OffsetPaginated<AdminCollection>> {
return this.service.list(query);
}
@Get(':id')
@RequirePermissions(PERMISSIONS.COLLECTION_READ)
@ApiOperation({ summary: 'One entry, every locale' })
getById(@Param('id') id: string): Promise<AdminCollection> {
return this.service.getById(id);
}
@Post()
@RequirePermissions(PERMISSIONS.COLLECTION_MANAGE)
@ApiOperation({ summary: 'Create' })
create(
@Body(new ZodValidationPipe(collectionInputSchema)) body: CollectionInput,
@CurrentActor() actor: AuthenticatedActor,
): Promise<AdminCollection> {
return this.service.create(body, actor.userId);
}
@Patch(':id')
@RequirePermissions(PERMISSIONS.COLLECTION_MANAGE)
@ApiOperation({ summary: 'Update' })
update(
@Param('id') id: string,
@Body(new ZodValidationPipe(collectionInputSchema)) body: CollectionInput,
@CurrentActor() actor: AuthenticatedActor,
): Promise<AdminCollection> {
return this.service.update(id, body, actor.userId);
}
@Delete(':id')
@RequirePermissions(PERMISSIONS.COLLECTION_MANAGE)
@ApiOperation({ summary: 'Delete, if nothing depends on it' })
remove(@Param('id') id: string, @CurrentActor() actor: AuthenticatedActor): Promise<void> {
return this.service.remove(id, actor.userId);
}
}
@@ -0,0 +1,247 @@
import { Injectable, Logger } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { LOCALES, type AdminCollection, type OffsetPaginated } from '@sport/types';
import type { CollectionInput, TaxonomyListQuery } from '@sport/validation';
import { AuditService } from '@/common/audit/audit.service';
import { AppException } from '@/common/errors/app.exception';
import { toDbLocale } from '@/common/i18n';
import { MediaUrlService } from '@/common/media/media-url.service';
import { PrismaService } from '@/infrastructure/prisma/prisma.service';
import { CACHE_KEYS } from '@/infrastructure/redis/cache-keys';
import { RedisService } from '@/infrastructure/redis/redis.service';
const select = {
id: true,
name: true,
slug: true,
description: true,
type: true,
bannerId: true,
position: true,
startsAt: true,
endsAt: true,
isActive: true,
metaTitle: true,
metaDescription: true,
banner: { select: { storageKey: true } },
translations: true,
_count: { select: { products: true } },
} as const;
type Row = Prisma.CollectionGetPayload<{ select: typeof select }>;
@Injectable()
export class CollectionsAdminService {
private readonly logger = new Logger(CollectionsAdminService.name);
constructor(
private readonly prisma: PrismaService,
private readonly audit: AuditService,
private readonly mediaUrl: MediaUrlService,
private readonly redis: RedisService,
) {}
async list(query: TaxonomyListQuery): Promise<OffsetPaginated<AdminCollection>> {
const where: Prisma.CollectionWhereInput = {
...(query.activeOnly ? { isActive: true } : {}),
...(query.q ? { name: { contains: query.q, mode: 'insensitive' } } : {}),
};
const [rows, totalItems] = await Promise.all([
this.prisma.collection.findMany({
where,
orderBy: [{ position: 'asc' }, { name: 'asc' }],
skip: (query.page - 1) * query.perPage,
take: query.perPage,
select,
}),
this.prisma.collection.count({ where }),
]);
const totalPages = Math.max(1, Math.ceil(totalItems / query.perPage));
return {
items: rows.map((row) => this.toAdmin(row)),
pageInfo: {
page: query.page,
perPage: query.perPage,
totalItems,
totalPages,
hasNextPage: query.page < totalPages,
},
};
}
async getById(id: string): Promise<AdminCollection> {
const row = await this.prisma.collection.findUnique({ where: { id }, select });
if (!row) throw AppException.notFound('Collection');
return this.toAdmin(row);
}
async create(input: CollectionInput, actorUserId: string): Promise<AdminCollection> {
await this.assertSlugFree(input.slug, null);
const created = await this.prisma.$transaction(async (tx) => {
const brand = await tx.collection.create({
data: {
name: input.name,
slug: input.slug,
description: input.description ?? null,
type: input.type,
bannerId: input.bannerId ?? null,
position: input.position,
startsAt: input.startsAt ? new Date(input.startsAt) : null,
endsAt: input.endsAt ? new Date(input.endsAt) : null,
isActive: input.isActive,
metaTitle: input.metaTitle ?? null,
metaDescription: input.metaDescription ?? null,
},
select: { id: true },
});
await this.writeTranslations(tx, brand.id, input);
return brand;
});
await this.afterWrite(actorUserId, 'collection.create', created.id, { slug: input.slug });
return this.getById(created.id);
}
async update(id: string, input: CollectionInput, actorUserId: string): Promise<AdminCollection> {
const existing = await this.prisma.collection.findUnique({
where: { id },
select: { id: true },
});
if (!existing) throw AppException.notFound('Collection');
await this.assertSlugFree(input.slug, id);
await this.prisma.$transaction(async (tx) => {
await tx.collection.update({
where: { id },
data: {
name: input.name,
slug: input.slug,
description: input.description ?? null,
type: input.type,
bannerId: input.bannerId ?? null,
position: input.position,
startsAt: input.startsAt ? new Date(input.startsAt) : null,
endsAt: input.endsAt ? new Date(input.endsAt) : null,
isActive: input.isActive,
metaTitle: input.metaTitle ?? null,
metaDescription: input.metaDescription ?? null,
},
});
await this.writeTranslations(tx, id, input);
});
await this.afterWrite(actorUserId, 'collection.update', id, { slug: input.slug });
return this.getById(id);
}
/**
* Deleting a collection is safe, unlike a brand or a category.
*
* `ProductCollection` cascades, so removing the collection removes only the
* membership rows — the products themselves are untouched and keep their
* category and brand. The product count is still surfaced in the UI so the
* operator knows how many items are about to lose this grouping.
*/
async remove(id: string, actorUserId: string): Promise<void> {
const row = await this.prisma.collection.findUnique({
where: { id },
select: { slug: true, _count: { select: { products: true } } },
});
if (!row) throw AppException.notFound('Collection');
await this.prisma.collection.delete({ where: { id } });
await this.afterWrite(actorUserId, 'collection.delete', id, { slug: row.slug });
}
// ---- internals -----------------------------------------------------------
private async assertSlugFree(slug: string, exceptId: string | null): Promise<void> {
const clash = await this.prisma.collection.findFirst({
where: { slug, ...(exceptId ? { id: { not: exceptId } } : {}) },
select: { id: true },
});
if (clash)
throw AppException.conflict(`The slug "${slug}" is already used by another collection.`);
}
private async writeTranslations(
tx: Prisma.TransactionClient,
collectionId: string,
input: CollectionInput,
): Promise<void> {
for (const locale of LOCALES) {
const fields = input.translations[locale];
if (!fields) continue;
const data = {
name: fields.name,
slug: fields.slug,
description: fields.description ?? null,
metaTitle: fields.metaTitle ?? null,
metaDescription: fields.metaDescription ?? null,
};
await tx.collectionTranslation.upsert({
where: { collectionId_locale: { collectionId, locale: toDbLocale(locale) } },
update: data,
create: { collectionId, locale: toDbLocale(locale), ...data },
});
}
}
private async afterWrite(
actorUserId: string,
action: string,
resourceId: string,
changes: Prisma.InputJsonValue,
): Promise<void> {
// Collections back /collections/* pages and the footer/nav links, all of
// which are cached — see ProductsAdminService.afterWrite.
const dropped = await this.redis.deleteByPrefix(CACHE_KEYS.catalogPrefix());
this.logger.log(`${action} on ${resourceId}; dropped ${dropped} catalog cache key(s)`);
this.audit.record({ actorUserId, action, resourceType: 'Collection', resourceId, changes });
}
private toAdmin(row: Row): AdminCollection {
return {
id: row.id,
name: row.name,
slug: row.slug,
description: row.description,
type: row.type,
bannerId: row.bannerId,
bannerUrl: row.banner ? this.mediaUrl.url(row.banner.storageKey) : null,
position: row.position,
startsAt: row.startsAt?.toISOString() ?? null,
endsAt: row.endsAt?.toISOString() ?? null,
isActive: row.isActive,
metaTitle: row.metaTitle,
metaDescription: row.metaDescription,
translations: Object.fromEntries(
row.translations.map((translation) => [
translation.locale.toLowerCase(),
{
name: translation.name,
slug: translation.slug,
description: translation.description,
metaTitle: translation.metaTitle,
metaDescription: translation.metaDescription,
},
]),
),
productCount: row._count.products,
};
}
}
@@ -1,5 +1,9 @@
import { Module } from '@nestjs/common';
import { MediaUrlModule } from '@/common/media/media.module';
import { CollectionsAdminController } from './collections-admin.controller';
import { CollectionsAdminService } from './collections-admin.service';
import { CollectionsController } from './collections.controller';
import { CollectionsMapper } from './collections.mapper';
import { CollectionsRepository } from './collections.repository';
@@ -12,8 +16,14 @@ import { CollectionsService } from './collections.service';
* Editorial and campaign groupings, including their scheduling window.
*/
@Module({
controllers: [CollectionsController],
providers: [CollectionsService, CollectionsRepository, CollectionsMapper],
imports: [MediaUrlModule],
controllers: [CollectionsController, CollectionsAdminController],
providers: [
CollectionsService,
CollectionsRepository,
CollectionsMapper,
CollectionsAdminService,
],
exports: [CollectionsService],
})
export class CollectionsModule {}
@@ -1,6 +1,7 @@
import { setRequestLocale } from 'next-intl/server';
import { getTranslations, setRequestLocale } from 'next-intl/server';
import type { Locale } from '@sport/types';
import { SkipLink } from '@sport/ui';
import { SiteHeader } from '@/components/commerce/site-header';
import { SiteFooter } from '@/components/layout/site-footer';
@@ -24,12 +25,17 @@ export default async function AccountGroupLayout({
const { locale } = await params;
setRequestLocale(locale);
const t = await getTranslations('common');
const navigation = await fetchNavigation(locale as Locale);
return (
<div className="flex min-h-screen flex-col">
<SkipLink>{t('skipToContent')}</SkipLink>
<SiteHeader navigation={navigation} locale={locale as Locale} />
<main className="flex-1">{children}</main>
<main id="main" className="flex-1">
{children}
</main>
<SiteFooter navigation={navigation} />
</div>
);
@@ -27,7 +27,7 @@ export default async function CheckoutLayout({
<Link href={routes.home()} className="text-lg font-black uppercase tracking-tighter">
Sport<span className="text-volt-600">.</span>
</Link>
<span className="text-ink-400 text-xs font-semibold uppercase tracking-widest">
<span className="text-ink-500 text-xs font-semibold uppercase tracking-widest">
{t('title')}
</span>
</div>
@@ -76,7 +76,7 @@ export default async function PostPage({ params }: PageProps) {
{post.publishedAt ? (
<time
dateTime={post.publishedAt}
className="text-ink-400 text-xs uppercase tracking-widest"
className="text-ink-500 text-xs uppercase tracking-widest"
>
{format.dateTime(new Date(post.publishedAt), { dateStyle: 'long' })}
</time>
@@ -64,7 +64,7 @@ export default async function BlogPage({ params }: PageProps) {
{post.publishedAt ? (
<time
dateTime={post.publishedAt}
className="text-ink-400 mt-4 block text-xs uppercase tracking-widest"
className="text-ink-500 mt-4 block text-xs uppercase tracking-widest"
>
{format.dateTime(new Date(post.publishedAt), { dateStyle: 'medium' })}
</time>
@@ -1,6 +1,7 @@
import { setRequestLocale } from 'next-intl/server';
import { getTranslations, setRequestLocale } from 'next-intl/server';
import type { Locale } from '@sport/types';
import { SkipLink } from '@sport/ui';
import { SiteHeader } from '@/components/commerce/site-header';
import { SiteFooter } from '@/components/layout/site-footer';
@@ -27,12 +28,17 @@ export default async function ShopLayout({
const { locale } = await params;
setRequestLocale(locale);
const t = await getTranslations('common');
const navigation = await fetchNavigation(locale as Locale);
return (
<div className="flex min-h-screen flex-col">
<SkipLink>{t('skipToContent')}</SkipLink>
<SiteHeader navigation={navigation} locale={locale as Locale} />
<main className="flex-1">{children}</main>
<main id="main" className="flex-1">
{children}
</main>
<SiteFooter navigation={navigation} />
</div>
);
@@ -37,7 +37,7 @@ export default async function HomePage({ params }: PageProps) {
/>
<section className="max-w-page px-gutter mx-auto py-16">
<h2 className="text-ink-400 text-xs font-semibold uppercase tracking-widest">
<h2 className="text-ink-500 text-xs font-semibold uppercase tracking-widest">
{t('shopBySport')}
</h2>
<ul className="mt-6 grid grid-cols-2 gap-3 sm:grid-cols-3 lg:grid-cols-6">
@@ -10,7 +10,7 @@ export default function NotFound() {
return (
<div className="max-w-page px-gutter mx-auto flex min-h-screen flex-col items-center justify-center text-center">
<p className="text-ink-400 text-xs font-semibold uppercase tracking-widest">{t('code')}</p>
<p className="text-ink-500 text-xs font-semibold uppercase tracking-widest">{t('code')}</p>
<h1 className="mt-4 text-5xl font-black uppercase">{t('title')}</h1>
<p className="text-ink-500 mt-4 max-w-md">{t('body')}</p>
<Button className="mt-8" asChild>
@@ -28,7 +28,7 @@ export function CartBadge() {
>
<ShoppingBag className="size-5" />
{!loading && count > 0 ? (
<span className="bg-volt-500 text-ink-950 absolute right-0.5 top-0.5 grid min-w-4 place-items-center rounded-full px-1 text-[0.625rem] font-bold leading-4">
<span className="bg-volt-500 text-ink-950 text-eyebrow absolute right-0.5 top-0.5 grid min-w-4 place-items-center rounded-full px-1 font-bold leading-4">
{count}
</span>
) : null}
@@ -105,7 +105,7 @@ export function CartView() {
</Link>
</h2>
<p className="text-ink-500 mt-1 text-xs">{line.variantTitle}</p>
<p className="text-ink-400 mt-0.5 font-mono text-[0.625rem]">{line.sku}</p>
<p className="text-ink-500 text-eyebrow mt-0.5 font-mono">{line.sku}</p>
<div className="mt-3 flex items-center gap-4">
<QuantityStepper
@@ -131,7 +131,7 @@ export function CartView() {
<div className="text-right">
<p className="text-sm font-semibold">{formatMoney(line.lineTotal, format)}</p>
{line.compareAtPrice ? (
<p className="text-ink-400 text-xs line-through">
<p className="text-ink-500 text-xs line-through">
{formatMoney(
{
amount: line.compareAtPrice.amount * line.quantity,
@@ -141,7 +141,7 @@ export function CartView() {
)}
</p>
) : null}
<p className="text-ink-400 mt-1 text-xs">
<p className="text-ink-500 mt-1 text-xs">
{formatMoney(line.unitPrice, format)} × {line.quantity}
</p>
</div>
@@ -189,7 +189,7 @@ function Row({ label, value, muted }: { label: string; value: string; muted?: bo
return (
<div className="flex items-baseline justify-between">
<dt className="text-ink-500">{label}</dt>
<dd className={muted ? 'text-ink-400 text-xs' : ''}>{value}</dd>
<dd className={muted ? 'text-ink-500 text-xs' : ''}>{value}</dd>
</div>
);
}
@@ -295,7 +295,7 @@ export function CheckoutForm({ locale }: { locale: Locale }) {
) : null}
<div className="flex justify-between">
<dt className="text-ink-500">{t('shipping')}</dt>
<dd className="text-ink-400 text-xs">{t('shippingLater')}</dd>
<dd className="text-ink-500 text-xs">{t('shippingLater')}</dd>
</div>
<div className="border-ink-200 flex items-baseline justify-between border-t pt-2">
<dt className="font-semibold">{t('total')}</dt>
@@ -315,7 +315,7 @@ export function CheckoutForm({ locale }: { locale: Locale }) {
</Button>
{/* Honest about scope: payment providers arrive with M9. */}
<p className="text-ink-400 mt-3 text-center text-xs">{t('paymentLater')}</p>
<p className="text-ink-500 mt-3 text-center text-xs">{t('paymentLater')}</p>
</aside>
</form>
);
@@ -339,7 +339,7 @@ function Field({
{required ? <span className="text-danger ml-1">*</span> : null}
</span>
{children}
{hint ? <span className="text-ink-400 block text-xs">{hint}</span> : null}
{hint ? <span className="text-ink-500 block text-xs">{hint}</span> : null}
</label>
);
}
@@ -70,7 +70,7 @@ export function DiscountForm({
disabled={pending}
onClick={() => void removeCode(discount.code as string)}
aria-label={t('removeCode', { code: discount.code })}
className="text-ink-400 hover:text-danger focus-visible:ring-ring/50 outline-none focus-visible:ring-[3px] disabled:opacity-50"
className="text-ink-500 hover:text-danger focus-visible:ring-ring/50 outline-none focus-visible:ring-[3px] disabled:opacity-50"
>
<X className="size-3.5" />
</button>
@@ -34,7 +34,7 @@ export function FilterSheet({
<SlidersHorizontal />
{t('filters')}
{activeCount > 0 ? (
<span className="bg-ink-950 grid size-5 place-items-center rounded-full text-[0.625rem] text-white">
<span className="bg-ink-950 text-eyebrow grid size-5 place-items-center rounded-full text-white">
{activeCount}
</span>
) : null}
@@ -84,7 +84,7 @@ export function LoadMore({
) : null}
<div className="mt-12 flex flex-col items-center gap-3">
<p className="text-ink-400 text-xs uppercase tracking-widest">
<p className="text-ink-500 text-xs uppercase tracking-widest">
{t('showing', { shown, total: totalCount })}
</p>
@@ -103,9 +103,7 @@ export function MegaMenu({
<div className="max-w-page px-gutter mx-auto grid grid-cols-2 gap-10 py-10 md:grid-cols-4">
{entry.columns.map((column) => (
<div key={column.heading}>
<p className="text-volt-500 text-[0.625rem] font-semibold uppercase tracking-widest">
{column.heading}
</p>
<p className="text-volt-500 text-eyebrow uppercase">{column.heading}</p>
<ul className="mt-4 space-y-2.5">
{column.links.map((link) => (
<li key={link.href}>
@@ -84,9 +84,7 @@ export function ProductCard({
<div className="mt-3 space-y-1">
{product.brandName ? (
<p className="text-ink-400 text-[0.625rem] font-semibold uppercase tracking-widest">
{product.brandName}
</p>
<p className="text-ink-500 text-eyebrow uppercase">{product.brandName}</p>
) : null}
<h3 className="text-ink-950 text-sm font-medium">
@@ -107,7 +105,7 @@ export function ProductCard({
{formatMoney(priceRange.min, format)}
</span>
{priceRange.compareAtMax && product.isOnSale ? (
<span className="text-ink-400 text-xs line-through">
<span className="text-ink-500 text-xs line-through">
{formatMoney(priceRange.compareAtMax, format)}
</span>
) : null}
@@ -125,7 +123,7 @@ export function ProductCard({
/>
))}
{product.colorSwatches.length > 5 ? (
<li className="text-ink-400 text-[0.625rem]">+{product.colorSwatches.length - 5}</li>
<li className="text-ink-500 text-eyebrow">+{product.colorSwatches.length - 5}</li>
) : null}
</ul>
) : null}
@@ -118,7 +118,7 @@ export function ProductDetail({ product }: { product: StorefrontProduct }) {
<div className="space-y-8 lg:pt-4">
<div>
{product.brand ? (
<p className="text-ink-400 text-xs font-semibold uppercase tracking-widest">
<p className="text-ink-500 text-xs font-semibold uppercase tracking-widest">
{product.brand.name}
</p>
) : null}
@@ -134,7 +134,7 @@ export function ProductDetail({ product }: { product: StorefrontProduct }) {
</span>
{compareAt && discount > 0 ? (
<>
<span className="text-ink-400 text-base line-through">
<span className="text-ink-500 text-base line-through">
{formatMoney(compareAt, format)}
</span>
<span className="text-sale text-xs font-semibold uppercase tracking-widest">
@@ -182,7 +182,7 @@ export function ProductDetail({ product }: { product: StorefrontProduct }) {
// Reachable but sold out reads differently from
// impossible: struck through, still selectable, so
// stock is discoverable rather than hidden.
reachableValue && !stocked && 'text-ink-400 line-through',
reachableValue && !stocked && 'text-ink-500 line-through',
)}
style={
isColour && value.swatchHex
@@ -207,7 +207,7 @@ export function ProductDetail({ product }: { product: StorefrontProduct }) {
<WishlistButton productId={product.id} productSlug={product.slug} />
{selectedVariant ? (
<p className="text-ink-400 text-center text-xs">
<p className="text-ink-500 text-center text-xs">
{t('sku')}: {selectedVariant.sku}
</p>
) : null}
@@ -144,7 +144,7 @@ export async function ProductFilters({
)}
>
<span>{brand.label}</span>
<span className="text-ink-400 text-xs">{brand.count}</span>
<span className="text-ink-500 text-xs">{brand.count}</span>
</Link>
</li>
);
@@ -59,7 +59,7 @@ export async function ProductListing({
<div className="max-w-page px-gutter mx-auto py-12">
<header className="mb-10">
{eyebrow ? (
<p className="text-ink-400 text-xs font-semibold uppercase tracking-widest">{eyebrow}</p>
<p className="text-ink-500 text-xs font-semibold uppercase tracking-widest">{eyebrow}</p>
) : null}
<h1 className="mt-2 text-4xl font-black uppercase sm:text-5xl">{title}</h1>
{description ? <p className="text-ink-500 mt-4 max-w-2xl text-sm">{description}</p> : null}
@@ -71,7 +71,7 @@ export async function ProductListing({
instead of pushing the products another screen down.
*/}
<div className="border-ink-200 mb-6 flex items-center justify-between gap-4 border-b pb-4">
<p className="text-ink-400 text-xs uppercase tracking-widest">
<p className="text-ink-500 text-xs uppercase tracking-widest">
{t('resultsCount', { count: result.totalCount })}
</p>
<SortMenu basePath={basePath} query={query} />
@@ -1,8 +1,7 @@
import { Star } from 'lucide-react';
import { getFormatter, getTranslations } from 'next-intl/server';
import type { ProductReviews as ProductReviewsPayload } from '@sport/api-client';
import { cn } from '@sport/ui';
import { Rating } from '@sport/ui';
import { getServerApi } from '@/lib/api';
@@ -50,9 +49,9 @@ export async function ProductReviews({ productId }: { productId: string }) {
<span className="text-5xl font-black tabular-nums">
{format.number(summary.average, { minimumFractionDigits: 1 })}
</span>
<span className="text-ink-400 text-sm">/ 5</span>
<span className="text-ink-500 text-sm">/ 5</span>
</div>
<Stars rating={Math.round(summary.average)} className="mt-2" />
<Rating value={Math.round(summary.average)} className="mt-2" />
<p className="text-ink-500 mt-2 text-xs">{t('count', { count: summary.count })}</p>
</div>
@@ -91,7 +90,7 @@ export async function ProductReviews({ productId }: { productId: string }) {
{items.map((review) => (
<li key={review.id} className="py-6">
<div className="flex flex-wrap items-center gap-3">
<Stars rating={review.rating} />
<Rating value={review.rating} />
{review.title ? <h3 className="text-sm font-semibold">{review.title}</h3> : null}
</div>
@@ -126,17 +125,3 @@ export async function ProductReviews({ productId }: { productId: string }) {
</section>
);
}
export function Stars({ rating, className }: { rating: number; className?: string }) {
return (
<span className={cn('flex items-center gap-0.5', className)} aria-label={`${rating}/5`}>
{[1, 2, 3, 4, 5].map((star) => (
<Star
key={star}
aria-hidden
className={cn('size-4', star <= rating ? 'fill-ink-950 text-ink-950' : 'text-ink-300')}
/>
))}
</span>
);
}
@@ -117,7 +117,7 @@ export function SearchDialog({ locale }: { locale: Locale }) {
</DialogHeader>
<form onSubmit={submit} className="border-ink-200 flex items-center gap-3 border-b px-5">
<Search className="text-ink-400 size-5 shrink-0" />
<Search className="text-ink-500 size-5 shrink-0" />
{/* No `autoFocus`: Radix moves focus to the first focusable element
when the dialog opens, which is this input. Setting it as well
fights the dialog's own focus management. */}
@@ -126,9 +126,9 @@ export function SearchDialog({ locale }: { locale: Locale }) {
onChange={(event) => setQuery(event.target.value)}
placeholder={t('placeholder')}
aria-label={t('title')}
className="placeholder:text-ink-400 h-14 w-full bg-transparent text-base outline-none"
className="placeholder:text-ink-500 h-14 w-full bg-transparent text-base outline-none"
/>
{loading ? <Loader2 className="text-ink-400 size-4 animate-spin" /> : null}
{loading ? <Loader2 className="text-ink-500 size-4 animate-spin" /> : null}
</form>
{active && suggestions.length > 0 ? (
@@ -151,7 +151,7 @@ export function SearchDialog({ locale }: { locale: Locale }) {
<p className="text-ink-500 px-5 py-6 text-center text-sm">{t('noSuggestions')}</p>
) : null}
<p className="border-ink-200 text-ink-400 border-t px-5 py-3 text-xs">
<p className="border-ink-200 text-ink-500 border-t px-5 py-3 text-xs">
{t('enterToSearch')}
</p>
</DialogContent>
@@ -35,7 +35,7 @@ export function SortMenu({ basePath, query }: { basePath: string; query: Product
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" size="sm">
<span className="text-ink-400 hidden font-normal normal-case sm:inline">
<span className="text-ink-500 hidden font-normal normal-case sm:inline">
{t('sortBy')}
</span>
{t(`sort.${active}`)}
@@ -58,7 +58,7 @@ export function LanguageSwitcher({ className }: { className?: string }) {
'px-1.5 py-1 text-xs font-semibold uppercase tracking-widest transition-colors',
locale === activeLocale
? 'text-ink-950 underline underline-offset-4'
: 'text-ink-400 hover:text-ink-950',
: 'text-ink-500 hover:text-ink-950',
)}
>
{locale}
@@ -25,7 +25,7 @@ export function PageScaffold({
return (
<div className="max-w-page px-gutter mx-auto py-20">
{eyebrow ? (
<p className="text-ink-400 text-xs font-semibold uppercase tracking-widest">{eyebrow}</p>
<p className="text-ink-500 text-xs font-semibold uppercase tracking-widest">{eyebrow}</p>
) : null}
<h1 className="mt-3 text-4xl font-black uppercase sm:text-6xl">{title}</h1>
@@ -12,7 +12,7 @@ import type {
OrderListItem,
ProductListItem,
} from '@sport/types';
import { Badge, Button, Input, Skeleton } from '@sport/ui';
import { Badge, Button, EmptyState, Input, Skeleton } from '@sport/ui';
import { Link } from '@/i18n/navigation';
import { browserApi } from '@/lib/api';
@@ -124,7 +124,7 @@ export function AccountOrders() {
{!data ? (
<Skeleton className="h-40 w-full" />
) : data.items.length === 0 ? (
<p className="text-ink-500 text-sm">{t('noOrders')}</p>
<EmptyState title={t('noOrders')} />
) : (
<ul className="divide-ink-100 divide-y border-y">
{data.items.map((order) => (
@@ -329,7 +329,7 @@ export function AccountAddresses() {
{!data ? (
<Skeleton className="h-32 w-full" />
) : data.length === 0 && !draft ? (
<p className="text-ink-500 text-sm">{t('noAddresses')}</p>
<EmptyState title={t('noAddresses')} />
) : (
<ul className="grid gap-4 sm:grid-cols-2">
{data.map((address) => (
@@ -454,7 +454,7 @@ export function AccountProfile() {
flow. The API refuses it too — this input simply says so.
*/}
<Input value={data.email} readOnly disabled />
<span className="text-ink-400 text-xs">{t('emailLocked')}</span>
<span className="text-ink-500 text-xs">{t('emailLocked')}</span>
</label>
<Field label={t('phone')} value={data.phone ?? ''} onChange={(v) => patch({ phone: v })} />
@@ -526,7 +526,7 @@ export function AccountWishlist() {
{!data ? (
<Skeleton className="h-32 w-full" />
) : data.length === 0 ? (
<p className="text-ink-500 text-sm">{t('noWishlist')}</p>
<EmptyState title={t('noWishlist')} />
) : (
<ul className="divide-ink-100 divide-y border-y">
{data.map((product) => (
@@ -133,14 +133,14 @@ export function AuthForm({ mode }: { mode: 'signIn' | 'register' }) {
autoComplete={isRegister ? 'new-password' : 'current-password'}
required
/>
{isRegister ? <span className="text-ink-400 text-xs">{t('passwordHint')}</span> : null}
{isRegister ? <span className="text-ink-500 text-xs">{t('passwordHint')}</span> : null}
</label>
{isRegister ? (
<>
<label className="block space-y-1.5">
<span className="text-xs font-semibold uppercase tracking-widest">
{t('phone')} <span className="text-ink-400 normal-case">{t('optional')}</span>
{t('phone')} <span className="text-ink-500 normal-case">{t('optional')}</span>
</span>
<Input
type="tel"
+3
View File
@@ -308,5 +308,8 @@
"failed": "That did not work. Check your details and try again.",
"noAccount": "No account yet?",
"haveAccount": "Already have an account?"
},
"common": {
"skipToContent": "Skip to content"
}
}
+3
View File
@@ -308,5 +308,8 @@
"failed": "Chưa được. Kiểm tra lại thông tin và thử lần nữa.",
"noAccount": "Chưa có tài khoản?",
"haveAccount": "Đã có tài khoản?"
},
"common": {
"skipToContent": "Bỏ qua, tới nội dung chính"
}
}