Fälttyp för koppling till post i mallar
- Ny fälttyp reference med valfri posttyp (refKind): rullista i formuläret, länk med ID på postsidan och rutan "Används av" på den kopplade posten - Server kontrollerar att kopplad post finns i boken och har rätt typ - Gemensam hantering av fältvärden (lib/field-values.ts) för poster och relationer - Standardmallar använder kopplingar för Lokal, Skiss, Företag, Ägare och Referens - Mallredigerare, översättningar och README Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6cea76899f
commit
e840adf526
13 files changed
+235
-57
No files matched your search
@@ -3,6 +3,7 @@ import { saveEntity } from "@/actions/entities";
|
||||
import { EntityForm } from "@/components/entity-form";
|
||||
import { PageBody, PageHeader } from "@/components/page";
|
||||
import { getT } from "@/i18n/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { KIND_BY_SLUG } from "@/lib/kinds";
|
||||
import { requireBook } from "@/lib/session";
|
||||
import { getTemplate } from "@/lib/templates";
|
||||
@@ -21,6 +22,11 @@ export default async function NewEntityPage({
|
||||
if (!info) notFound();
|
||||
const { t, locale } = await getT();
|
||||
const { fields } = await getTemplate(bookId, info.kind, locale);
|
||||
const refEntities = await prisma.entity.findMany({
|
||||
where: { bookId },
|
||||
select: { id: true, name: true, kind: true, code: true },
|
||||
orderBy: { name: "asc" },
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -31,6 +37,7 @@ export default async function NewEntityPage({
|
||||
bookId={bookId}
|
||||
kind={info.kind}
|
||||
fields={fields}
|
||||
refEntities={refEntities}
|
||||
defaultName={name?.slice(0, 200)}
|
||||
cancelHref={`/books/${bookId}/e/${slug}`}
|
||||
t={t}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { saveEntity } from "@/actions/entities";
|
||||
import { EntityForm } from "@/components/entity-form";
|
||||
import { PageBody, PageHeader } from "@/components/page";
|
||||
import { getT } from "@/i18n/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { requireEntity } from "@/lib/session";
|
||||
import { getTemplate } from "@/lib/templates";
|
||||
|
||||
@@ -10,6 +11,11 @@ export default async function EditEntityPage({ params }: { params: Promise<{ boo
|
||||
const entity = await requireEntity(bookId, entityId);
|
||||
const { t, locale } = await getT();
|
||||
const { fields } = await getTemplate(bookId, entity.kind, locale);
|
||||
const refEntities = await prisma.entity.findMany({
|
||||
where: { bookId },
|
||||
select: { id: true, name: true, kind: true, code: true },
|
||||
orderBy: { name: "asc" },
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -21,6 +27,7 @@ export default async function EditEntityPage({ params }: { params: Promise<{ boo
|
||||
kind={entity.kind}
|
||||
entity={entity}
|
||||
fields={fields}
|
||||
refEntities={refEntities}
|
||||
cancelHref={`/books/${bookId}/entity/${entity.id}`}
|
||||
t={t}
|
||||
/>
|
||||
|
||||
@@ -13,9 +13,10 @@ import { KIND_INFO, WORKPLACE_KINDS } from "@/lib/kinds";
|
||||
import { requireEntity } from "@/lib/session";
|
||||
import { formatFieldValue, getTemplate, type FieldDef } from "@/lib/templates";
|
||||
|
||||
type Group = { section?: string; items: { field: FieldDef; value: string }[] };
|
||||
type RefTarget = { id: string; name: string; code: string | null };
|
||||
type Group = { section?: string; items: { field: FieldDef; value: string; target?: RefTarget }[] };
|
||||
|
||||
function groupFields(fields: FieldDef[], data: Record<string, unknown>): Group[] {
|
||||
function groupFields(fields: FieldDef[], data: Record<string, unknown>, byId: Map<string, RefTarget>): Group[] {
|
||||
const groups: Group[] = [];
|
||||
let current: Group = { items: [] };
|
||||
for (const field of fields) {
|
||||
@@ -23,6 +24,11 @@ function groupFields(fields: FieldDef[], data: Record<string, unknown>): Group[]
|
||||
if (current.items.length > 0) groups.push(current);
|
||||
current = { section: field.section, items: [] };
|
||||
}
|
||||
if (field.type === "reference") {
|
||||
const target = typeof data[field.key] === "string" ? byId.get(data[field.key] as string) : undefined;
|
||||
if (target) current.items.push({ field, value: target.name, target });
|
||||
continue;
|
||||
}
|
||||
const value = formatFieldValue(field, data[field.key]);
|
||||
if (value) current.items.push({ field, value });
|
||||
}
|
||||
@@ -35,7 +41,7 @@ export default async function EntityPage({ params }: { params: Promise<{ bookId:
|
||||
const base = await requireEntity(bookId, entityId);
|
||||
const { t, locale } = await getT();
|
||||
|
||||
const [entity, template, allEntities, maps] = await Promise.all([
|
||||
const [entity, template, bookEntities, maps] = await Promise.all([
|
||||
prisma.entity.findUniqueOrThrow({
|
||||
where: { id: base.id },
|
||||
include: {
|
||||
@@ -52,7 +58,7 @@ export default async function EntityPage({ params }: { params: Promise<{ bookId:
|
||||
getTemplate(bookId, base.kind, locale),
|
||||
prisma.entity.findMany({
|
||||
where: { bookId },
|
||||
select: { id: true, name: true, kind: true },
|
||||
select: { id: true, name: true, kind: true, code: true, data: true },
|
||||
orderBy: { name: "asc" },
|
||||
}),
|
||||
prisma.bookMap.findMany({ where: { bookId }, orderBy: { name: "asc" } }),
|
||||
@@ -61,7 +67,27 @@ export default async function EntityPage({ params }: { params: Promise<{ bookId:
|
||||
const info = KIND_INFO[entity.kind];
|
||||
const Icon = kindIcons[entity.kind];
|
||||
const image = fileUrl(entity.imageFile);
|
||||
const groups = groupFields(template.fields, (entity.data ?? {}) as Record<string, unknown>);
|
||||
const allEntities = bookEntities.map(({ data: _data, ...rest }) => rest);
|
||||
const groups = groupFields(
|
||||
template.fields,
|
||||
(entity.data ?? {}) as Record<string, unknown>,
|
||||
new Map(allEntities.map((e) => [e.id, e])),
|
||||
);
|
||||
|
||||
// Poster som pekar hit via ett kopplingsfält i sin mall, t.ex. företag som använder den här lokalen
|
||||
const referencing = bookEntities.filter(
|
||||
(e) => e.id !== entity.id && Object.values((e.data ?? {}) as Record<string, unknown>).some((v) => v === entity.id),
|
||||
);
|
||||
const referencingTemplates = new Map(
|
||||
await Promise.all(
|
||||
[...new Set(referencing.map((e) => e.kind))].map(async (kind) => [kind, (await getTemplate(bookId, kind, locale)).fields] as const),
|
||||
),
|
||||
);
|
||||
const backlinks = referencing.map((e) => {
|
||||
const data = (e.data ?? {}) as Record<string, unknown>;
|
||||
const field = (referencingTemplates.get(e.kind) ?? []).find((f) => f.type === "reference" && data[f.key] === entity.id);
|
||||
return { entity: e, label: field?.label ?? null };
|
||||
});
|
||||
const isWorkplace = WORKPLACE_KINDS.includes(entity.kind);
|
||||
type Person = { id: string; name: string };
|
||||
type RelationItem = {
|
||||
@@ -176,11 +202,16 @@ export default async function EntityPage({ params }: { params: Promise<{ bookId:
|
||||
<div key={i} className={i > 0 ? "mt-4" : ""}>
|
||||
{group.section ? <h4 className="subheader border-bottom pb-2 mb-3">{group.section}</h4> : null}
|
||||
<dl className="mb-0">
|
||||
{group.items.map(({ field, value }) => (
|
||||
{group.items.map(({ field, value, target }) => (
|
||||
<div className="row mb-2" key={field.key}>
|
||||
<dt className="col-sm-4 text-secondary fw-normal">{field.label}</dt>
|
||||
<dd className={`col-sm-8 mb-0 ${field.type === "textarea" ? "bm-prewrap" : ""}`}>
|
||||
{field.type === "url" ? (
|
||||
{target ? (
|
||||
<Link href={entityHref(target.id)} className="fw-bold">
|
||||
{target.code ? <span className="badge me-2">{target.code}</span> : null}
|
||||
{target.name}
|
||||
</Link>
|
||||
) : field.type === "url" ? (
|
||||
<a href={value} target="_blank" rel="noopener noreferrer nofollow">
|
||||
{value}
|
||||
</a>
|
||||
@@ -196,6 +227,23 @@ export default async function EntityPage({ params }: { params: Promise<{ bookId:
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{backlinks.length > 0 ? (
|
||||
<div className="card mt-3">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">{t("entity.referencedBy")}</h3>
|
||||
</div>
|
||||
<div className="list-group list-group-flush">
|
||||
{backlinks.map(({ entity: e, label }) => (
|
||||
<Link key={e.id} href={entityHref(e.id)} className="list-group-item list-group-item-action d-flex align-items-center gap-2">
|
||||
{e.code ? <span className="badge">{e.code}</span> : null}
|
||||
<span className="fw-bold">{e.name}</span>
|
||||
<span className="text-secondary">{t(`kind.${e.kind}.one`)}</span>
|
||||
{label ? <span className="badge bg-azure-lt ms-auto">{label}</span> : null}
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<div className="col-lg-4">
|
||||
|
||||
@@ -34,6 +34,11 @@ export default async function RelationPage({
|
||||
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);
|
||||
@@ -139,7 +144,7 @@ export default async function RelationPage({
|
||||
) : 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]} />
|
||||
<FieldInput field={field} value={data[field.key]} refs={{ entities: refEntities, t }} />
|
||||
</div>
|
||||
</Fragment>
|
||||
))}
|
||||
|
||||
Reference in new issue
Block a user