diff --git a/app/tenants/[id]/page.tsx b/app/tenants/[id]/page.tsx new file mode 100644 index 0000000..12673cf --- /dev/null +++ b/app/tenants/[id]/page.tsx @@ -0,0 +1,95 @@ +import Link from "next/link"; +import { notFound, redirect } from "next/navigation"; +import { getSession } from "../../../lib/auth"; +import { getAccount } from "../../../lib/platform-db"; +import { updateTenant } from "../actions"; + +export default async function EditTenantPage({ + params, + searchParams, +}: { + params: Promise<{ id: string }>; + searchParams: Promise<{ error?: string }>; +}) { + const session = await getSession(); + if (!session) redirect("/login"); + + const { id } = await params; + const { error } = await searchParams; + const account = getAccount(Number(id)); + if (!account) notFound(); + + const errorMsg = + error === "slug" + ? "Slug is al in gebruik." + : error === "missing" + ? "Vul alle velden in." + : null; + + const inputClass = + "rounded-lg border border-zinc-300 dark:border-zinc-700 bg-white dark:bg-zinc-800 px-3 py-2 text-sm text-zinc-900 dark:text-zinc-100 focus:outline-none focus:ring-2 focus:ring-zinc-500"; + + return ( +
+
+
+ Rikx Beheer + {session.email} +
+
+ +
+ + ← Terug naar overzicht + +

+ Sociaal ondernemer bewerken +

+ {errorMsg &&

{errorMsg}

} +
+ +
+ + +
+
+ + +

+ Let op: een andere slug wijzigt de URL ({account.slug}.rikxplatform.nl). +

+
+
+ + + Annuleren + +
+
+
+
+ ); +} diff --git a/app/tenants/actions.ts b/app/tenants/actions.ts index 2196dd9..50cb5d8 100644 --- a/app/tenants/actions.ts +++ b/app/tenants/actions.ts @@ -57,3 +57,25 @@ export async function deleteTenant(formData: FormData) { redirect("/tenants"); } + +export async function updateTenant(formData: FormData) { + const session = await getSession(); + if (!session) redirect("/login"); + + const accountId = Number(formData.get("account_id")); + const name = ((formData.get("name") as string) ?? "").trim(); + const slug = ((formData.get("slug") as string) ?? "").trim().toLowerCase().replace(/[^a-z0-9-]/g, "-"); + + if (!accountId) redirect("/tenants"); + if (!name || !slug) redirect(`/tenants/${accountId}?error=missing`); + + const db = getPlatformDb(); + const existing = db + .prepare("SELECT id FROM accounts WHERE slug = ? AND id != ?") + .get(slug, accountId); + if (existing) redirect(`/tenants/${accountId}?error=slug`); + + db.prepare("UPDATE accounts SET name = ?, slug = ? WHERE id = ?").run(name, slug, accountId); + + redirect("/tenants"); +} diff --git a/app/tenants/page.tsx b/app/tenants/page.tsx index 0c9ab17..493b955 100644 --- a/app/tenants/page.tsx +++ b/app/tenants/page.tsx @@ -57,7 +57,7 @@ export default async function TenantsPage({ URL Gebruikers Aangemaakt - + @@ -89,6 +89,14 @@ export default async function TenantsPage({ Inloggen ↗ + + + Bewerken + + diff --git a/lib/platform-db.ts b/lib/platform-db.ts index 7336fde..7c3ad16 100644 --- a/lib/platform-db.ts +++ b/lib/platform-db.ts @@ -40,3 +40,9 @@ export function listAccounts(): (PlatformAccount & { user_count: number })[] { ) .all() as (PlatformAccount & { user_count: number })[]; } + +export function getAccount(id: number): PlatformAccount | undefined { + return getPlatformDb() + .prepare("SELECT * FROM accounts WHERE id = ?") + .get(id) as PlatformAccount | undefined; +}