273 lines
10 KiB
React
273 lines
10 KiB
React
import { useEffect, useState } from "react";
|
|
import { Link, useLocation, useNavigate, useParams, useSearchParams } from "react-router-dom";
|
|
import { api, documentToText, fetchFile, fetchPaperless, markDirty, textToDocument } from "../api.js";
|
|
import { ErrorNote } from "./widgets.jsx";
|
|
import { formatDate, kindLabel, Trail } from "./content.jsx";
|
|
|
|
function pageContext(params) {
|
|
const kind = params.get("context_kind");
|
|
const id = params.get("context_id");
|
|
if (!kind || !id || kind === "direct") return null;
|
|
return { kind, id };
|
|
}
|
|
|
|
export function Reader() {
|
|
const location = useLocation();
|
|
const kind = location.pathname.startsWith("/cases") ? "cases" : "matters";
|
|
const { id, bindingId } = useParams();
|
|
const [params] = useSearchParams();
|
|
const inherited = pageContext(params);
|
|
const [record, setRecord] = useState(null);
|
|
const [error, setError] = useState("");
|
|
const [previewUrl, setPreviewUrl] = useState("");
|
|
const [text, setText] = useState("");
|
|
const [note, setNote] = useState("");
|
|
const [documentOn, setDocumentOn] = useState("");
|
|
const [summary, setSummary] = useState("");
|
|
const [previewKind, setPreviewKind] = useState("");
|
|
const [notice, setNotice] = useState("");
|
|
const navigate = useNavigate();
|
|
|
|
const context = kind === "cases" ? { kind: "case", id } : (inherited || { kind: "matter", id });
|
|
const query = kind === "matters" && inherited ? `?context_kind=${inherited.kind}&context_id=${inherited.id}` : "";
|
|
|
|
async function load() {
|
|
const detail = await api(`/api/v1/${kind}/${id}${query}`);
|
|
const pools = [...(detail.bindings || [])];
|
|
for (const group of detail.contained || []) pools.push(...group.bindings);
|
|
const binding = pools.find((item) => item.id === bindingId);
|
|
if (!binding) {
|
|
setRecord(detail);
|
|
setError("Diese Unterlage ist in dieser Ansicht nicht sichtbar.");
|
|
return;
|
|
}
|
|
setRecord({ ...detail, binding });
|
|
setText(binding.element.kind === "note" ? documentToText(binding.element.document) : "");
|
|
setDocumentOn(binding.element.dates?.document_on || "");
|
|
setSummary(binding.element.summary || "");
|
|
setError("");
|
|
}
|
|
|
|
useEffect(() => {
|
|
setRecord(null);
|
|
setPreviewUrl((current) => {
|
|
if (current) URL.revokeObjectURL(current);
|
|
return "";
|
|
});
|
|
load().catch((err) => setError(err.message));
|
|
}, [kind, id, bindingId, query]);
|
|
|
|
useEffect(() => () => {
|
|
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
|
}, [previewUrl]);
|
|
|
|
useEffect(() => {
|
|
const element = record?.binding?.element;
|
|
if (!element || element.kind === "note") return undefined;
|
|
if (!element.preview?.available) return undefined;
|
|
let alive = true;
|
|
const load = element.kind === "external_ref" ? fetchPaperless(element.id, context) : fetchFile(element.id, context);
|
|
load.then((blob) => {
|
|
if (!alive) return;
|
|
const next = URL.createObjectURL(blob);
|
|
setPreviewUrl((current) => {
|
|
if (current) URL.revokeObjectURL(current);
|
|
return next;
|
|
});
|
|
if (blob.type.startsWith("image/")) setPreviewKind("image");
|
|
else if (blob.type.includes("pdf")) setPreviewKind("pdf");
|
|
else setPreviewKind("other");
|
|
}).catch((err) => {
|
|
if (alive) setError(err.message);
|
|
});
|
|
return () => {
|
|
alive = false;
|
|
};
|
|
}, [record?.binding?.id]);
|
|
|
|
if (error && !record?.binding) {
|
|
return <main className="page"><ErrorNote error={error} /><p><Link to={`/${kind}/${id}${query}`}>Zurück</Link></p></main>;
|
|
}
|
|
if (!record?.binding) return <main className="page"><p className="muted">Unterlage wird geladen …</p></main>;
|
|
|
|
const binding = record.binding;
|
|
const element = binding.element;
|
|
const back = `/${kind}/${id}${query}`;
|
|
|
|
async function openFile() {
|
|
setError("");
|
|
try {
|
|
const blob = element.kind === "external_ref"
|
|
? await fetchPaperless(element.id, context)
|
|
: await fetchFile(element.id, context);
|
|
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
|
setPreviewUrl(URL.createObjectURL(blob));
|
|
} catch (err) {
|
|
setError(err.message);
|
|
}
|
|
}
|
|
|
|
async function saveNote(event) {
|
|
event.preventDefault();
|
|
setError("");
|
|
try {
|
|
const updated = await api(`/api/v1/notes/${element.id}`, {
|
|
method: "PATCH",
|
|
body: { expected_version: element.version, document: textToDocument(text), context },
|
|
});
|
|
markDirty(false);
|
|
setNotice(updated.notice || "Der gemeinsame Text ist gespeichert.");
|
|
load();
|
|
} catch (err) {
|
|
setError(err.message);
|
|
}
|
|
}
|
|
|
|
async function saveSummary(event) {
|
|
event.preventDefault();
|
|
setError("");
|
|
try {
|
|
await api(`/api/v1/elements/${element.id}/summary`, {
|
|
method: "PATCH",
|
|
body: { expected_version: element.version, summary: summary.trim() || null, context },
|
|
});
|
|
markDirty(false);
|
|
load();
|
|
} catch (err) {
|
|
setError(err.message);
|
|
}
|
|
}
|
|
|
|
async function saveDate(event) {
|
|
event.preventDefault();
|
|
setError("");
|
|
try {
|
|
await api(`/api/v1/elements/${element.id}/dates`, {
|
|
method: "PATCH",
|
|
body: {
|
|
expected_version: element.version,
|
|
document_on: documentOn || null,
|
|
sent_on: element.dates?.sent_on || null,
|
|
received_on: element.dates?.received_on || null,
|
|
context,
|
|
},
|
|
});
|
|
markDirty(false);
|
|
load();
|
|
} catch (err) {
|
|
setError(err.message);
|
|
}
|
|
}
|
|
|
|
async function annotate(event) {
|
|
event.preventDefault();
|
|
const search = new URLSearchParams();
|
|
search.set("context_kind", context.kind);
|
|
search.set("context_id", context.id);
|
|
try {
|
|
await api(`/api/v1/bindings/${binding.id}/annotations?${search}`, { method: "POST", body: { body: note } });
|
|
setNote("");
|
|
markDirty(false);
|
|
load();
|
|
} catch (err) {
|
|
setError(err.message);
|
|
}
|
|
}
|
|
|
|
async function detach() {
|
|
if (!window.confirm("Aus diesem Zusammenhang entfernen? Die Unterlage bleibt an anderen Stellen erhalten.")) return;
|
|
const search = new URLSearchParams();
|
|
search.set("context_kind", context.kind);
|
|
search.set("context_id", context.id);
|
|
await api(`/api/v1/bindings/${binding.id}?${search}`, { method: "DELETE" });
|
|
navigate(back);
|
|
}
|
|
|
|
async function destroy() {
|
|
if (!window.confirm("Überall löschen? Alle Einbindungen verschwinden. Eine Paperless-Quelle bleibt unangetastet.")) return;
|
|
await api(`/api/v1/elements/${element.id}/delete`, { method: "POST", body: { expected_version: element.version } });
|
|
navigate(back);
|
|
}
|
|
|
|
return (
|
|
<main className="page reader">
|
|
<Trail items={[...(record.trail || []).map((item, index, all) => (index === all.length - 1 ? { ...item, href: back } : item)), { label: element.title, href: null }]} />
|
|
<p><Link to={back}>{kind === "cases" ? "Zurück zur Akte" : "Zurück zum Vorgang"}</Link></p>
|
|
<header className="page-head">
|
|
<div>
|
|
<p className="eyebrow">{kindLabel(element.kind)}</p>
|
|
<h1>{element.title}</h1>
|
|
<p className="muted">Fachliches Datum: {formatDate(element.dates?.document_on)}</p>
|
|
</div>
|
|
</header>
|
|
<ErrorNote error={error} />
|
|
{element.kind === "note" ? (
|
|
<form className="stack" onSubmit={saveNote}>
|
|
<label>
|
|
Gemeinsamer Text
|
|
<textarea rows={8} value={text} disabled={!binding.rights.edit} onChange={(event) => { setText(event.target.value); markDirty(true); }} />
|
|
</label>
|
|
<p className="muted">Diese Änderung betrifft den gemeinsamen Inhalt der Notiz.</p>
|
|
{notice ? <p className="notice">{notice}</p> : null}
|
|
{binding.rights.edit ? <button type="submit">Text speichern</button> : <p className="muted">In diesem Kontext nur lesbar.</p>}
|
|
</form>
|
|
) : previewUrl && previewKind === "image" ? (
|
|
<img className="reader-preview" alt={element.title} src={previewUrl} />
|
|
) : previewUrl ? (
|
|
<iframe className="preview" title={element.title} src={previewUrl} />
|
|
) : (
|
|
<div className="stack">
|
|
<p className="muted">{element.preview?.available ? "Vorschau wird geladen …" : "Für diesen Typ gibt es keine eingebaute Vorschau."}</p>
|
|
<button type="button" onClick={openFile}>{element.kind === "external_ref" ? "Paperless öffnen" : "Datei öffnen"}</button>
|
|
</div>
|
|
)}
|
|
{binding.rights.edit ? (
|
|
<form className="stack" onSubmit={saveSummary}>
|
|
<label>
|
|
Kurzbeschreibung, optional
|
|
<textarea rows={2} maxLength={400} value={summary} onChange={(event) => { setSummary(event.target.value); markDirty(true); }} />
|
|
</label>
|
|
<button type="submit" className="quiet">Kurzbeschreibung speichern</button>
|
|
</form>
|
|
) : element.summary ? <p>{element.summary}</p> : null}
|
|
{binding.rights.edit ? (
|
|
<form className="stack" onSubmit={saveDate}>
|
|
<label>
|
|
{element.kind === "note" ? "Bezugsdatum, optional" : "Dokumentdatum, optional"}
|
|
<input type="date" value={documentOn} onChange={(event) => { setDocumentOn(event.target.value); markDirty(true); }} />
|
|
</label>
|
|
<button type="submit" className="quiet">Datum speichern</button>
|
|
</form>
|
|
) : null}
|
|
<section className="section">
|
|
<h2>Anmerkung in diesem Zusammenhang</h2>
|
|
<p className="muted">Die Anmerkung gilt nur hier, nicht für den gemeinsamen Inhalt.</p>
|
|
{binding.annotations?.length ? (
|
|
<ul className="notes">
|
|
{binding.annotations.map((item) => (
|
|
<li key={item.id}><strong>{item.author_display_name}</strong> {item.body}</li>
|
|
))}
|
|
</ul>
|
|
) : <p className="muted">Noch keine Anmerkung.</p>}
|
|
{binding.rights.edit ? (
|
|
<form className="stack" onSubmit={annotate}>
|
|
<label>
|
|
Neue Anmerkung
|
|
<textarea rows={3} value={note} onChange={(event) => { setNote(event.target.value); markDirty(true); }} required />
|
|
</label>
|
|
<button type="submit" className="quiet">Anmerkung speichern</button>
|
|
</form>
|
|
) : null}
|
|
</section>
|
|
<details className="fold">
|
|
<summary>Weitere Aktionen</summary>
|
|
<div className="row">
|
|
{record.rights?.edit ? <button type="button" className="quiet" onClick={detach}>Aus diesem Zusammenhang entfernen</button> : null}
|
|
{record.is_owner ? <button type="button" className="danger" onClick={destroy}>Überall löschen</button> : null}
|
|
</div>
|
|
</details>
|
|
</main>
|
|
);
|
|
}
|
|
|