Files
bookmanager/src/app/books/[bookId]/relations/[relationId]/page.tsx
T
bobbanandClaude Sonnet 5.5 5c58f70bdd Inbjudningslänkar, adressuppslag för kartregistret och närvaromärke
Böcker kan delas med en inbjudningslänk (roll, giltighetstid, en eller
flera personer, bara en hash sparas). Inloggningen går tillbaka till
inbjudan. Kartregistrets markeringar kan få position genom uppslag mot
karttjänsten, ett i sekunden. Redigeringsformulär och listor visar vem
som har något öppet för redigering. Migrering 0009.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-09 22:18:29 +01:00

180 lines
7.3 KiB
TypeScript

import Link from "next/link";
import { notFound } from "next/navigation";
import { Fragment } from "react";
import { IconTrash } from "@tabler/icons-react";
import { deleteRelation, saveRelation } from "@/actions/relations";
import { ConfirmSubmit } from "@/components/confirm-submit";
import { ConflictForm } from "@/components/conflict-form";
import { EntitySelect } from "@/components/entity-select";
import { FieldInput } from "@/components/entity-form";
import { RelationBondFields } from "@/components/relation-bond-fields";
import { PageBody, PageHeader } from "@/components/page";
import { getT } from "@/i18n/server";
import { versionOf } from "@/lib/conflict";
import { whoIsEditing } from "@/lib/presence";
import { prisma } from "@/lib/db";
import { requireBook } from "@/lib/session";
import { getTemplate } from "@/lib/templates";
export default async function RelationPage({
params,
searchParams,
}: {
params: Promise<{ bookId: string; relationId: string }>;
searchParams: Promise<{ saved?: string }>;
}) {
const { bookId, relationId } = await params;
const { saved } = await searchParams;
const access = await requireBook(bookId, "EDITOR");
const { t, locale } = await getT();
const relation = await prisma.relation.findFirst({
where: { id: relationId, bookId },
include: { from: true, to: true, members: { include: { entity: true } } },
});
if (!relation) notFound();
const editing = await whoIsEditing(bookId, "relation", relation.id, access.userId);
const candidates = await prisma.entity.findMany({
where: { bookId, id: { notIn: [relation.fromId, relation.toId] } },
select: { id: true, name: true, kind: true },
orderBy: { name: "asc" },
});
const refEntities = await prisma.entity.findMany({
where: { bookId },
select: { id: true, name: true, kind: true, code: true },
orderBy: { name: "asc" },
});
const memberEntities = relation.members.map((m) => m.entity);
const isGroup = memberEntities.length > 0;
const { fields } = await getTemplate(bookId, "RELATION", locale);
const data = (relation.data ?? {}) as Record<string, unknown>;
const suggestions = t("relation.suggestions").split(",");
return (
<>
<PageHeader
pretitle={
<Link href={`/books/${bookId}/relations`} className="text-reset">
{t("relation.title")}
{relation.code ? <span className="badge ms-2">{relation.code}</span> : null}
</Link>
}
title={
isGroup ? (
<span>
{[relation.from, relation.to, ...memberEntities].map((e, i) => (
<span key={e.id}>
{i > 0 ? ", " : ""}
<Link href={`/books/${bookId}/entity/${e.id}`} className="text-reset">
{e.name}
</Link>
</span>
))}
<span className="text-secondary fw-normal"> · {relation.type}</span>
</span>
) : (
<span>
<Link href={`/books/${bookId}/entity/${relation.fromId}`} className="text-reset">
{relation.from.name}
</Link>
<span className="text-secondary fw-normal"> · {relation.type} · </span>
<Link href={`/books/${bookId}/entity/${relation.toId}`} className="text-reset">
{relation.to.name}
</Link>
</span>
)
}
actions={
<form action={deleteRelation}>
<input type="hidden" name="bookId" value={bookId} />
<input type="hidden" name="id" value={relation.id} />
<input type="hidden" name="redirect" value="list" />
<ConfirmSubmit className="btn btn-outline-danger">
<IconTrash size={18} className="me-1" />
{t("common.delete")}
</ConfirmSubmit>
</form>
}
/>
<PageBody>
{saved ? <div className="alert alert-success">{t("common.saved")}</div> : null}
<ConflictForm
key={versionOf(relation.updatedAt)}
action={saveRelation}
version={versionOf(relation.updatedAt)}
presence={{ bookId, kind: "relation", targetId: relation.id, others: editing }}
>
<input type="hidden" name="bookId" value={bookId} />
<input type="hidden" name="id" value={relation.id} />
<div className="card mb-3">
<div className="card-body">
<div className="row g-3">
<div className="col-md-6">
<label className="form-label required">{t("relation.type")}</label>
<input name="type" className="form-control" list="relation-types" maxLength={100} defaultValue={relation.type} />
<datalist id="relation-types">
{suggestions.map((s) => (
<option key={s} value={s} />
))}
</datalist>
</div>
<div className="col-md-6">
<label className="form-label">{t("common.note")}</label>
<input name="description" className="form-control" maxLength={2000} defaultValue={relation.description ?? ""} />
</div>
<div className="col-12">
<label className="form-label">{t("relation.members")}</label>
<EntitySelect
name="memberIds"
entities={candidates}
t={t}
multiple
defaultValues={memberEntities.map((e) => e.id)}
/>
<div className="form-hint">{t("relation.membersHelp")}</div>
</div>
<RelationBondFields t={t} bond={relation.bond} startDate={relation.startDate} endDate={relation.endDate} />
</div>
</div>
</div>
{fields.length > 0 ? (
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">{t("entity.details")}</h3>
</div>
<div className="card-body">
<div className="row g-3">
{fields.map((field) => (
<Fragment key={field.key}>
{field.section ? (
<div className="col-12">
<h4 className="subheader border-bottom pb-2 mb-0 mt-2">{field.section}</h4>
</div>
) : null}
<div className={field.type === "textarea" ? "col-12" : "col-md-6"}>
{field.type !== "checkbox" ? <label className="form-label">{field.label}</label> : null}
<FieldInput field={field} value={data[field.key]} refs={{ entities: refEntities, t }} />
</div>
</Fragment>
))}
</div>
</div>
</div>
) : null}
<div className="d-flex justify-content-end gap-2">
<Link href={`/books/${bookId}/relations`} className="btn">
{t("common.back")}
</Link>
<button type="submit" className="btn btn-primary">
{t("common.save")}
</button>
</div>
</ConflictForm>
</PageBody>
</>
);
}