-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathshow.tsx
More file actions
91 lines (81 loc) · 3.24 KB
/
show.tsx
File metadata and controls
91 lines (81 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React, { useState } from "react";
import { Link, useNavigate, useParams } from "react-router-dom";
import { CodeHighlight, EditableCodeHighlight } from "../../components/CodeHighlight.jsx";
import { useFireproof } from "use-fireproof";
export function DocsShow() {
const { name } = useParams();
const navigate = useNavigate();
let { id: _id } = useParams();
_id = _id === "new" ? undefined : _id;
const { useDocument, database } = useFireproof(name);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const [doc] = useDocument(() => ({ _id: _id! }));
const [docToSave, setDocToSave] = useState<string>(JSON.stringify(doc, null, 2));
const [needsSave, setNeedsSave] = useState(false);
async function saveDocument(_id?: string) {
const data = JSON.parse(docToSave);
const resp = await database.put({ _id, ...data });
if (!_id) {
navigate(`/fp/databases/${name}/docs/${resp.id}`);
return;
}
setNeedsSave(false);
}
async function deleteDocument(_id: string) {
alert("Are you sure?");
await database.del(_id);
navigate(-1);
}
function editorChanged({ code, valid }: { code: string; valid: boolean }) {
setNeedsSave(valid);
setDocToSave(() => code);
}
// const { _id: id, ...data } = doc;
// const title = id ? `Edit document: ${_id}` : "Create new document";
const idFirstMeta = { _id };
return (
<div className="p-6 bg-[--muted]">
<div className="flex justify-between items-center mb-4">
<nav className="text-lg text-[--muted-foreground]">
<Link to={`/fp/databases/${name}`} className="font-medium text-[--foreground] hover:underline">
{name}
</Link>
<span className="mx-2">></span>
<span>{_id ? `Document: ${_id}` : "New Document"}</span>
</nav>
</div>
<h3>Editable data fields</h3>
<EditableCodeHighlight onChange={editorChanged} code={JSON.stringify(doc, null, 2)} />
<div className="flex space-x-4 mt-4">
<button
onClick={() => {
saveDocument(_id);
}}
className={`${
needsSave
? "bg-[--accent] hover:bg-[--accent]/80 text-accent-foreground"
: "bg-[--accent] hover:bg-[--accent]/80 text-accent-foreground opacity-50"
} inline-flex items-center justify-center rounded px-3 py-2 text-sm font-semibold shadow-sm transition-colors`}
>
Save
</button>
{_id && (
<button
onClick={() => deleteDocument(_id)}
className="inline-flex items-center justify-center rounded bg-[--destructive] px-3 py-2 text-sm font-semibold text-destructive-foreground shadow-sm hover:bg-[--destructive]/80 transition-colors"
>
Delete
</button>
)}
<Link
to={`/fp/databases/${name}`}
className="inline-flex items-center justify-center rounded bg-[--background] border border-[--border] px-3 py-2 text-sm font-semibold text-[--foreground] shadow-sm hover:bg-[--background]/80 transition-colors"
>
Cancel
</Link>
</div>
<h3 className="mt-4">Fireproof metadata</h3>
<CodeHighlight code={JSON.stringify(idFirstMeta, null, 2)} />
</div>
);
}