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 / components / ModerationRow.tsx
src/components/ModerationRow.tsx · 82 lines · 1.9 KB← back to folder
| 1 | "use client"; |
| 2 | |
| 3 | import { useState } from "react"; |
| 4 | import Link from "next/link"; |
| 5 | |
| 6 | export function ModerationRow({ |
| 7 | slug, |
| 8 | name, |
| 9 | ownerUsername, |
| 10 | initialStatus, |
| 11 | }: { |
| 12 | slug: string; |
| 13 | name: string; |
| 14 | ownerUsername: string; |
| 15 | initialStatus: string; |
| 16 | }) { |
| 17 | const [status, setStatus] = useState(initialStatus); |
| 18 | const [busy, setBusy] = useState(false); |
| 19 | |
| 20 | async function updateStatus(nextStatus: string) { |
| 21 | setBusy(true); |
| 22 | try { |
| 23 | const res = await fetch(`/api/admin/products/${encodeURIComponent(slug)}`, { |
| 24 | method: "PATCH", |
| 25 | headers: { "Content-Type": "application/json" }, |
| 26 | body: JSON.stringify({ status: nextStatus }), |
| 27 | }); |
| 28 | if (res.ok) { |
| 29 | setStatus(nextStatus); |
| 30 | } |
| 31 | } finally { |
| 32 | setBusy(false); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return ( |
| 37 | <tr> |
| 38 | <td> |
| 39 | <Link href={`/${slug}`}>{name}</Link> |
| 40 | </td> |
| 41 | <td> |
| 42 | <Link href={`/${ownerUsername}`}>{ownerUsername}</Link> |
| 43 | </td> |
| 44 | <td>{status}</td> |
| 45 | <td> |
| 46 | <div style={{ display: "flex", gap: 10, flexWrap: "wrap" }}> |
| 47 | {status !== "published" && ( |
| 48 | <button |
| 49 | type="button" |
| 50 | className="link-button" |
| 51 | disabled={busy} |
| 52 | onClick={() => void updateStatus("published")} |
| 53 | > |
| 54 | approve |
| 55 | </button> |
| 56 | )} |
| 57 | {status !== "hidden" && ( |
| 58 | <button |
| 59 | type="button" |
| 60 | className="link-button" |
| 61 | disabled={busy} |
| 62 | onClick={() => void updateStatus("hidden")} |
| 63 | > |
| 64 | hide |
| 65 | </button> |
| 66 | )} |
| 67 | {status !== "removed" && ( |
| 68 | <button |
| 69 | type="button" |
| 70 | className="link-button" |
| 71 | disabled={busy} |
| 72 | onClick={() => void updateStatus("removed")} |
| 73 | > |
| 74 | remove |
| 75 | </button> |
| 76 | )} |
| 77 | </div> |
| 78 | </td> |
| 79 | </tr> |
| 80 | ); |
| 81 | } |
| 82 |