feat(settings): manage public header banner in admin
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
import { isAdminSelfRegistrationEnabled, setAdminSelfRegistrationEnabled } from "@cms/db"
|
||||
import {
|
||||
getPublicHeaderBannerConfig,
|
||||
isAdminSelfRegistrationEnabled,
|
||||
setAdminSelfRegistrationEnabled,
|
||||
setPublicHeaderBannerConfig,
|
||||
} from "@cms/db"
|
||||
import { Button } from "@cms/ui/button"
|
||||
import { revalidatePath } from "next/cache"
|
||||
import Link from "next/link"
|
||||
@@ -79,6 +84,53 @@ async function updateRegistrationPolicyAction(formData: FormData) {
|
||||
)
|
||||
}
|
||||
|
||||
async function updatePublicHeaderBannerAction(formData: FormData) {
|
||||
"use server"
|
||||
|
||||
await requireSettingsPermission()
|
||||
const t = await getSettingsTranslator()
|
||||
const enabled = formData.get("bannerEnabled") === "on"
|
||||
const message = toSingleValue(formData.get("bannerMessage")?.toString())?.trim() ?? ""
|
||||
const ctaLabel = toSingleValue(formData.get("bannerCtaLabel")?.toString())?.trim() ?? ""
|
||||
const ctaHref = toSingleValue(formData.get("bannerCtaHref")?.toString())?.trim() ?? ""
|
||||
|
||||
if (enabled && message.length === 0) {
|
||||
redirect(
|
||||
`/settings?error=${encodeURIComponent(
|
||||
t(
|
||||
"settings.banner.errors.messageRequired",
|
||||
"Banner message is required while banner is enabled.",
|
||||
),
|
||||
)}`,
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
await setPublicHeaderBannerConfig({
|
||||
enabled,
|
||||
message,
|
||||
ctaLabel: ctaLabel || null,
|
||||
ctaHref: ctaHref || null,
|
||||
})
|
||||
} catch {
|
||||
redirect(
|
||||
`/settings?error=${encodeURIComponent(
|
||||
t(
|
||||
"settings.banner.errors.updateFailed",
|
||||
"Saving banner settings failed. Ensure database migrations are applied.",
|
||||
),
|
||||
)}`,
|
||||
)
|
||||
}
|
||||
|
||||
revalidatePath("/settings")
|
||||
redirect(
|
||||
`/settings?notice=${encodeURIComponent(
|
||||
t("settings.banner.success.updated", "Public header banner settings updated."),
|
||||
)}`,
|
||||
)
|
||||
}
|
||||
|
||||
export default async function SettingsPage({ searchParams }: { searchParams: SearchParamsInput }) {
|
||||
const role = await requirePermissionForRoute({
|
||||
nextPath: "/settings",
|
||||
@@ -86,10 +138,11 @@ export default async function SettingsPage({ searchParams }: { searchParams: Sea
|
||||
scope: "global",
|
||||
})
|
||||
|
||||
const [params, locale, isRegistrationEnabled] = await Promise.all([
|
||||
const [params, locale, isRegistrationEnabled, publicBanner] = await Promise.all([
|
||||
searchParams,
|
||||
resolveAdminLocale(),
|
||||
isAdminSelfRegistrationEnabled(),
|
||||
getPublicHeaderBannerConfig(),
|
||||
])
|
||||
const messages = await getAdminMessages(locale)
|
||||
const t = (key: string, fallback: string) => translateMessage(messages, key, fallback)
|
||||
@@ -175,6 +228,72 @@ export default async function SettingsPage({ searchParams }: { searchParams: Sea
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="rounded-xl border border-neutral-200 p-6">
|
||||
<div className="space-y-5">
|
||||
<div className="space-y-2">
|
||||
<h2 className="text-xl font-medium">
|
||||
{t("settings.banner.title", "Public header banner")}
|
||||
</h2>
|
||||
<p className="text-sm text-neutral-600">
|
||||
{t(
|
||||
"settings.banner.description",
|
||||
"Control the top banner shown on the public app header.",
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<form action={updatePublicHeaderBannerAction} className="space-y-4">
|
||||
<label className="flex items-center gap-3 text-sm">
|
||||
<input
|
||||
type="checkbox"
|
||||
name="bannerEnabled"
|
||||
defaultChecked={publicBanner.enabled}
|
||||
className="h-4 w-4 rounded border-neutral-300"
|
||||
/>
|
||||
<span>{t("settings.banner.enabledLabel", "Enable public header banner")}</span>
|
||||
</label>
|
||||
|
||||
<label className="space-y-1 text-sm">
|
||||
<span className="text-xs text-neutral-600">
|
||||
{t("settings.banner.messageLabel", "Message")}
|
||||
</span>
|
||||
<input
|
||||
name="bannerMessage"
|
||||
defaultValue={publicBanner.message}
|
||||
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<div className="grid gap-3 md:grid-cols-2">
|
||||
<label className="space-y-1 text-sm">
|
||||
<span className="text-xs text-neutral-600">
|
||||
{t("settings.banner.ctaLabelLabel", "CTA label (optional)")}
|
||||
</span>
|
||||
<input
|
||||
name="bannerCtaLabel"
|
||||
defaultValue={publicBanner.ctaLabel ?? ""}
|
||||
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
||||
/>
|
||||
</label>
|
||||
<label className="space-y-1 text-sm">
|
||||
<span className="text-xs text-neutral-600">
|
||||
{t("settings.banner.ctaHrefLabel", "CTA URL (optional)")}
|
||||
</span>
|
||||
<input
|
||||
name="bannerCtaHref"
|
||||
defaultValue={publicBanner.ctaHref ?? ""}
|
||||
className="w-full rounded border border-neutral-300 px-3 py-2 text-sm"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<Button type="submit">
|
||||
{t("settings.banner.actions.save", "Save banner settings")}
|
||||
</Button>
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
</AdminShell>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user