first commit
This commit is contained in:
commit
b7b0f253ce
85 files changed
+7560
No files matched your search
@@ -0,0 +1,161 @@
|
||||
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 { EntitySelect } from "@/components/entity-select";
|
||||
import { FieldInput } from "@/components/entity-form";
|
||||
import { PageBody, PageHeader } from "@/components/page";
|
||||
import { getT } from "@/i18n/server";
|
||||
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;
|
||||
await requireBook(bookId);
|
||||
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 candidates = await prisma.entity.findMany({
|
||||
where: { bookId, id: { notIn: [relation.fromId, relation.toId] } },
|
||||
select: { id: true, name: true, kind: 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")}
|
||||
</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}
|
||||
<form action={saveRelation}>
|
||||
<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" required 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>
|
||||
</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]} />
|
||||
</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>
|
||||
</form>
|
||||
</PageBody>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user