-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathnew.tsx
More file actions
58 lines (52 loc) · 1.74 KB
/
new.tsx
File metadata and controls
58 lines (52 loc) · 1.74 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
import React, { useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { useFireproof } from "use-fireproof";
import { Button } from "../../../../../components/Button.jsx";
import { EditableCodeHighlight } from "../../../../../components/CodeHighlight.jsx";
export function NewLedgerDocument() {
const { tenantId, ledgerId } = useParams();
const navigate = useNavigate();
const { database } = useFireproof(ledgerId || "");
const [docToSave, setDocToSave] = useState<string>(JSON.stringify({}, null, 2));
const [needsSave, setNeedsSave] = useState(false);
async function saveDocument() {
try {
const data = JSON.parse(docToSave);
await database.put(data);
navigate(`/fp/cloud/tenants/${tenantId}/ledgers/${ledgerId}/documents`);
} catch (error) {
console.error("Failed to save document:", error);
}
}
function editorChanged({ code, valid }: { code: string; valid: boolean }) {
setNeedsSave(valid);
setDocToSave(code);
}
return (
<div className="space-y-4">
<div>
<h3 className="text-lg font-medium mb-2">Document Data</h3>
<EditableCodeHighlight onChange={editorChanged} code={docToSave} />
</div>
<div className="flex gap-[14px] justify-end mt-[14px] mb-[32px]">
<Button
variation="secondary"
onClick={() => navigate(`/fp/cloud/tenants/${tenantId}/ledgers/${ledgerId}/documents`)}
style="min-w-[105px]"
>
Back
</Button>
<Button
variation="primary"
disabled={!needsSave}
style="min-w-[105px]"
onClick={() => {
saveDocument();
}}
>
Save
</Button>
</div>
</div>
);
}