feat(portfolio): add grouping visibility and ordering controls
This commit is contained in:
@@ -5,10 +5,12 @@ import {
|
||||
createCategory,
|
||||
createGallery,
|
||||
createTag,
|
||||
deleteGrouping,
|
||||
linkArtworkToGrouping,
|
||||
listArtworks,
|
||||
listMediaAssets,
|
||||
listMediaFoundationGroups,
|
||||
updateGrouping,
|
||||
} from "@cms/db"
|
||||
import { Button } from "@cms/ui/button"
|
||||
import { revalidatePath } from "next/cache"
|
||||
@@ -32,6 +34,21 @@ function readOptionalField(formData: FormData, key: string): string | undefined
|
||||
return value.length > 0 ? value : undefined
|
||||
}
|
||||
|
||||
function readOptionalNullableField(formData: FormData, key: string): string | null {
|
||||
const value = readField(formData, key)
|
||||
return value.length > 0 ? value : null
|
||||
}
|
||||
|
||||
function readNonNegativeInt(formData: FormData, key: string): number {
|
||||
const raw = readField(formData, key)
|
||||
const value = Number(raw)
|
||||
return Number.isFinite(value) && value >= 0 ? Math.floor(value) : 0
|
||||
}
|
||||
|
||||
function readBooleanField(formData: FormData, key: string): boolean {
|
||||
return formData.get(key) === "on" || readField(formData, key) === "true"
|
||||
}
|
||||
|
||||
function readFirstValue(value: string | string[] | undefined): string | null {
|
||||
if (Array.isArray(value)) {
|
||||
return value[0] ?? null
|
||||
@@ -117,23 +134,32 @@ async function createGroupAction(formData: FormData) {
|
||||
name,
|
||||
slug,
|
||||
description: readOptionalField(formData, "description"),
|
||||
sortOrder: readNonNegativeInt(formData, "sortOrder"),
|
||||
isVisible: readBooleanField(formData, "isVisible"),
|
||||
})
|
||||
} else if (type === "album") {
|
||||
await createAlbum({
|
||||
name,
|
||||
slug,
|
||||
description: readOptionalField(formData, "description"),
|
||||
sortOrder: readNonNegativeInt(formData, "sortOrder"),
|
||||
isVisible: readBooleanField(formData, "isVisible"),
|
||||
})
|
||||
} else if (type === "category") {
|
||||
await createCategory({
|
||||
name,
|
||||
slug,
|
||||
description: readOptionalField(formData, "description"),
|
||||
sortOrder: readNonNegativeInt(formData, "sortOrder"),
|
||||
isVisible: readBooleanField(formData, "isVisible"),
|
||||
})
|
||||
} else {
|
||||
await createTag({
|
||||
name,
|
||||
slug,
|
||||
description: readOptionalField(formData, "description"),
|
||||
sortOrder: readNonNegativeInt(formData, "sortOrder"),
|
||||
isVisible: readBooleanField(formData, "isVisible"),
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
@@ -144,6 +170,47 @@ async function createGroupAction(formData: FormData) {
|
||||
redirectWithState({ notice: `${type} created.` })
|
||||
}
|
||||
|
||||
async function updateGroupAction(formData: FormData) {
|
||||
"use server"
|
||||
|
||||
await requireWritePermission()
|
||||
|
||||
try {
|
||||
await updateGrouping({
|
||||
groupType: readField(formData, "groupType"),
|
||||
groupId: readField(formData, "groupId"),
|
||||
name: readField(formData, "name"),
|
||||
slug: slugify(readField(formData, "slug")),
|
||||
description: readOptionalNullableField(formData, "description"),
|
||||
sortOrder: readNonNegativeInt(formData, "sortOrder"),
|
||||
isVisible: readBooleanField(formData, "isVisible"),
|
||||
})
|
||||
} catch {
|
||||
redirectWithState({ error: "Failed to update grouping entity." })
|
||||
}
|
||||
|
||||
revalidatePath("/portfolio")
|
||||
redirectWithState({ notice: "Grouping entity updated." })
|
||||
}
|
||||
|
||||
async function deleteGroupAction(formData: FormData) {
|
||||
"use server"
|
||||
|
||||
await requireWritePermission()
|
||||
|
||||
try {
|
||||
await deleteGrouping({
|
||||
groupType: readField(formData, "groupType"),
|
||||
groupId: readField(formData, "groupId"),
|
||||
})
|
||||
} catch {
|
||||
redirectWithState({ error: "Failed to delete grouping entity." })
|
||||
}
|
||||
|
||||
revalidatePath("/portfolio")
|
||||
redirectWithState({ notice: "Grouping entity deleted." })
|
||||
}
|
||||
|
||||
async function linkArtworkGroupAction(formData: FormData) {
|
||||
"use server"
|
||||
|
||||
@@ -297,7 +364,7 @@ export default async function PortfolioPage({
|
||||
<section className="rounded-xl border border-neutral-200 p-6">
|
||||
<h2 className="text-xl font-medium">Create Group Entity</h2>
|
||||
<form action={createGroupAction} className="mt-4 space-y-3">
|
||||
<div className="grid gap-3 md:grid-cols-3">
|
||||
<div className="grid gap-3 md:grid-cols-5">
|
||||
<select
|
||||
name="groupType"
|
||||
defaultValue="gallery"
|
||||
@@ -319,6 +386,18 @@ export default async function PortfolioPage({
|
||||
placeholder="Slug (optional)"
|
||||
className="rounded border border-neutral-300 px-3 py-2 text-sm"
|
||||
/>
|
||||
<input
|
||||
name="sortOrder"
|
||||
type="number"
|
||||
min={0}
|
||||
defaultValue={0}
|
||||
placeholder="Sort order"
|
||||
className="rounded border border-neutral-300 px-3 py-2 text-sm"
|
||||
/>
|
||||
<label className="flex items-center gap-2 rounded border border-neutral-300 px-3 py-2 text-sm">
|
||||
<input type="checkbox" name="isVisible" defaultChecked />
|
||||
Visible
|
||||
</label>
|
||||
</div>
|
||||
<textarea
|
||||
name="description"
|
||||
@@ -330,6 +409,89 @@ export default async function PortfolioPage({
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section className="rounded-xl border border-neutral-200 p-6">
|
||||
<h2 className="text-xl font-medium">Manage Group Entities</h2>
|
||||
<div className="mt-4 grid gap-4">
|
||||
{(
|
||||
[
|
||||
{ type: "gallery" as const, label: "Gallery", items: groups.galleries },
|
||||
{ type: "album" as const, label: "Album", items: groups.albums },
|
||||
{ type: "category" as const, label: "Category", items: groups.categories },
|
||||
{ type: "tag" as const, label: "Tag", items: groups.tags },
|
||||
] as const
|
||||
).map((groupConfig) => (
|
||||
<section
|
||||
key={`manage-${groupConfig.type}`}
|
||||
className="rounded border border-neutral-200 p-4"
|
||||
>
|
||||
<h3 className="text-sm font-semibold">{groupConfig.label} Entities</h3>
|
||||
<div className="mt-3 space-y-3">
|
||||
{groupConfig.items.length === 0 ? (
|
||||
<p className="text-xs text-neutral-500">No entities created yet.</p>
|
||||
) : (
|
||||
groupConfig.items.map((group) => (
|
||||
<form
|
||||
key={`manage-${groupConfig.type}-${group.id}`}
|
||||
action={updateGroupAction}
|
||||
className="space-y-3 rounded border border-neutral-200 p-3"
|
||||
>
|
||||
<input type="hidden" name="groupType" value={groupConfig.type} />
|
||||
<input type="hidden" name="groupId" value={group.id} />
|
||||
<div className="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
|
||||
<input
|
||||
name="name"
|
||||
required
|
||||
defaultValue={group.name}
|
||||
className="rounded border border-neutral-300 px-3 py-2 text-sm"
|
||||
/>
|
||||
<input
|
||||
name="slug"
|
||||
required
|
||||
defaultValue={group.slug}
|
||||
className="rounded border border-neutral-300 px-3 py-2 text-sm"
|
||||
/>
|
||||
<input
|
||||
name="sortOrder"
|
||||
type="number"
|
||||
min={0}
|
||||
defaultValue={group.sortOrder}
|
||||
className="rounded border border-neutral-300 px-3 py-2 text-sm"
|
||||
/>
|
||||
<label className="flex items-center gap-2 rounded border border-neutral-300 px-3 py-2 text-sm">
|
||||
<input
|
||||
type="checkbox"
|
||||
name="isVisible"
|
||||
defaultChecked={group.isVisible}
|
||||
/>
|
||||
Visible
|
||||
</label>
|
||||
</div>
|
||||
<textarea
|
||||
name="description"
|
||||
rows={2}
|
||||
defaultValue={group.description ?? ""}
|
||||
placeholder="Description"
|
||||
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
||||
/>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button type="submit">Save group</Button>
|
||||
<button
|
||||
type="submit"
|
||||
formAction={deleteGroupAction}
|
||||
className="rounded border border-red-300 px-3 py-2 text-sm text-red-700 hover:bg-red-50"
|
||||
>
|
||||
Delete group
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</section>
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="rounded-xl border border-neutral-200 p-6">
|
||||
<h2 className="text-xl font-medium">Link Artwork To Group</h2>
|
||||
<div className="mt-4 grid gap-4 lg:grid-cols-2">
|
||||
|
||||
Reference in New Issue
Block a user