Forms now update documents directly.
This commit is contained in:
@@ -1,20 +1,16 @@
|
||||
// Displays a single secret with discovered checkbox and text.
|
||||
import type { Secret, Session } from "@/lib/types";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import { useState } from "react";
|
||||
import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import type { Secret } from "@/lib/types";
|
||||
import { useState } from "react";
|
||||
|
||||
/**
|
||||
* Renders an editable secret form.
|
||||
* Handles updating the discovered state and discoveredIn relationship.
|
||||
*/
|
||||
export const SecretEditForm = ({
|
||||
secret,
|
||||
session,
|
||||
}: {
|
||||
secret: Secret;
|
||||
session?: Session;
|
||||
}) => {
|
||||
export const SecretEditForm = ({ secret }: { secret: Secret }) => {
|
||||
const { dispatch } = useDocument();
|
||||
const [checked, setChecked] = useState(
|
||||
!!(secret.data as any)?.secret?.discovered,
|
||||
);
|
||||
@@ -25,43 +21,28 @@ export const SecretEditForm = ({
|
||||
setLoading(true);
|
||||
setChecked(newChecked);
|
||||
try {
|
||||
await pb.collection("documents").update(secret.id, {
|
||||
data: {
|
||||
...secret.data,
|
||||
discovered: newChecked,
|
||||
},
|
||||
});
|
||||
if (session || !newChecked) {
|
||||
// If the session exists or the element is being unchecked, remove any
|
||||
// existing discoveredIn relationship
|
||||
const rels = await pb.collection("relationships").getList(1, 1, {
|
||||
filter: `primary = "${secret.id}" && type = "discoveredIn"`,
|
||||
const updated: Secret = await pb
|
||||
.collection("documents")
|
||||
.update(secret.id, {
|
||||
data: {
|
||||
...secret.data,
|
||||
discovered: newChecked,
|
||||
},
|
||||
});
|
||||
if (rels.items.length > 0) {
|
||||
await pb.collection("relationships").delete(rels.items[0].id);
|
||||
}
|
||||
}
|
||||
if (session) {
|
||||
if (newChecked) {
|
||||
await pb.collection("relationships").create({
|
||||
primary: secret.id,
|
||||
secondary: [session.id],
|
||||
type: "discoveredIn",
|
||||
});
|
||||
}
|
||||
}
|
||||
dispatch({ type: "setDocument", doc: updated });
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function saveText(text: string) {
|
||||
await pb.collection("documents").update(secret.id, {
|
||||
const updated: Secret = await pb.collection("documents").update(secret.id, {
|
||||
data: {
|
||||
...secret.data,
|
||||
text,
|
||||
},
|
||||
});
|
||||
dispatch({ type: "setDocument", doc: updated });
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user