cg / hackerproducts
63 files · 5,740 lines · 159.1 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.
Quick jump: page.tsxpage.tsxglobals.cssschema.sqlpackage.json
root / src / app / [username] / edit / page.tsx
src/app/[username]/edit/page.tsx · 64 lines · 1.6 KB← back to folder
| 1 | import type { Metadata } from "next"; |
| 2 | import Link from "next/link"; |
| 3 | import { notFound, redirect } from "next/navigation"; |
| 4 | import { getCurrentUser } from "@/lib/auth"; |
| 5 | import { getProductBySlug } from "@/lib/products"; |
| 6 | import { ProductForm } from "@/components/ProductForm"; |
| 7 | |
| 8 | export const dynamic = "force-dynamic"; |
| 9 | |
| 10 | export const metadata: Metadata = { |
| 11 | title: "Edit product", |
| 12 | robots: { index: false, follow: false }, |
| 13 | }; |
| 14 | |
| 15 | export default async function EditProductPage({ |
| 16 | params, |
| 17 | }: { |
| 18 | params: Promise<{ username: string }>; |
| 19 | }) { |
| 20 | const { username: slug } = await params; |
| 21 | const user = await getCurrentUser(); |
| 22 | if (!user) { |
| 23 | redirect(`/login?next=${encodeURIComponent(`/${slug}/edit`)}`); |
| 24 | } |
| 25 | |
| 26 | const product = await getProductBySlug(slug, user.id, true); |
| 27 | if (!product) { |
| 28 | notFound(); |
| 29 | } |
| 30 | |
| 31 | if (product.owner_id !== user.id && !user.is_admin) { |
| 32 | return ( |
| 33 | <div> |
| 34 | <h1>Edit product</h1> |
| 35 | <p className="form-error">You do not have permission to edit this product.</p> |
| 36 | <p> |
| 37 | <Link href={`/${product.slug}`}>← back to {product.name}</Link> |
| 38 | </p> |
| 39 | </div> |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | return ( |
| 44 | <div> |
| 45 | <h1>Edit {product.name}</h1> |
| 46 | <p className="subtitle"> |
| 47 | <Link href={`/${product.slug}`}>← back to product page</Link> |
| 48 | </p> |
| 49 | <ProductForm |
| 50 | mode="edit" |
| 51 | initial={{ |
| 52 | slug: product.slug, |
| 53 | name: product.name, |
| 54 | website_url: product.website_url, |
| 55 | description: product.description, |
| 56 | readme_md: product.readme_md, |
| 57 | icon_url: product.icon_url, |
| 58 | screenshot_url: product.screenshot_url, |
| 59 | }} |
| 60 | /> |
| 61 | </div> |
| 62 | ); |
| 63 | } |
| 64 |