|
| 1 | +import { redirect } from "next/navigation"; |
| 2 | +import { |
| 3 | + getListById, |
| 4 | + getUserChecklists, |
| 5 | +} from "@/app/_server/actions/checklist"; |
| 6 | +import { getCategories } from "@/app/_server/actions/category"; |
| 7 | +import { getCurrentUser, canAccessAllContent } from "@/app/_server/actions/users"; |
| 8 | +import { ChecklistClient } from "@/app/_components/FeatureComponents/Checklists/Parts/ChecklistClient"; |
| 9 | +import { Modes } from "@/app/_types/enums"; |
| 10 | +import type { Metadata } from "next"; |
| 11 | +import { getMedatadaTitle } from "@/app/_server/actions/config"; |
| 12 | +import { PermissionsProvider } from "@/app/_providers/PermissionsProvider"; |
| 13 | +import { MetadataProvider } from "@/app/_providers/MetadataProvider"; |
| 14 | + |
| 15 | +interface AdminChecklistPageProps { |
| 16 | + params: { |
| 17 | + uuid: string; |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +export const dynamic = "force-dynamic"; |
| 22 | + |
| 23 | +export async function generateMetadata({ |
| 24 | + params, |
| 25 | +}: AdminChecklistPageProps): Promise<Metadata> { |
| 26 | + const { uuid } = params; |
| 27 | + return getMedatadaTitle(Modes.CHECKLISTS, uuid, "Admin"); |
| 28 | +} |
| 29 | + |
| 30 | +export default async function AdminChecklistPage({ params }: AdminChecklistPageProps) { |
| 31 | + const { uuid } = params; |
| 32 | + const user = await getCurrentUser(); |
| 33 | + const hasContentAccess = await canAccessAllContent(); |
| 34 | + |
| 35 | + if (!hasContentAccess) { |
| 36 | + redirect("/"); |
| 37 | + } |
| 38 | + |
| 39 | + const [listsResult, categoriesResult] = await Promise.all([ |
| 40 | + getUserChecklists({ username: user?.username }), |
| 41 | + getCategories(Modes.CHECKLISTS), |
| 42 | + ]); |
| 43 | + |
| 44 | + if (!listsResult.success || !listsResult.data) { |
| 45 | + redirect("/"); |
| 46 | + } |
| 47 | + |
| 48 | + const checklist = await getListById(uuid); |
| 49 | + |
| 50 | + if (!checklist) { |
| 51 | + redirect("/"); |
| 52 | + } |
| 53 | + |
| 54 | + const categories = |
| 55 | + categoriesResult.success && categoriesResult.data |
| 56 | + ? categoriesResult.data |
| 57 | + : []; |
| 58 | + |
| 59 | + const metadata = { |
| 60 | + id: checklist.id, |
| 61 | + uuid: checklist.uuid, |
| 62 | + title: checklist.title, |
| 63 | + category: checklist.category || "Uncategorized", |
| 64 | + owner: checklist.owner, |
| 65 | + createdAt: checklist.createdAt, |
| 66 | + updatedAt: checklist.updatedAt, |
| 67 | + type: "checklist" as const, |
| 68 | + }; |
| 69 | + |
| 70 | + return ( |
| 71 | + <MetadataProvider metadata={metadata}> |
| 72 | + <PermissionsProvider item={checklist}> |
| 73 | + <ChecklistClient |
| 74 | + checklist={checklist} |
| 75 | + categories={categories} |
| 76 | + user={user} |
| 77 | + /> |
| 78 | + </PermissionsProvider> |
| 79 | + </MetadataProvider> |
| 80 | + ); |
| 81 | +} |
0 commit comments