Makes a generic document row
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
// SecretForm.tsx
|
||||
// Form for adding a new secret to a session.
|
||||
import { useState } from "react";
|
||||
import type { Session, Secret } from "@/lib/types";
|
||||
import type { Secret } from "@/lib/types";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
|
||||
/**
|
||||
* Renders a form to add a new secret. Calls onCreate with the new secret document.
|
||||
*/
|
||||
export const SecretForm = ({ session, onCreate }: { session: Session; onCreate: (secret: Secret) => Promise<void>; }) => {
|
||||
export const SecretForm = ({
|
||||
campaign,
|
||||
onCreate,
|
||||
}: {
|
||||
campaign: string;
|
||||
onCreate: (secret: Secret) => Promise<void>;
|
||||
}) => {
|
||||
const [newSecret, setNewSecret] = useState("");
|
||||
const [adding, setAdding] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
@@ -19,7 +25,7 @@ export const SecretForm = ({ session, onCreate }: { session: Session; onCreate:
|
||||
setError(null);
|
||||
try {
|
||||
const secretDoc: Secret = await pb.collection("documents").create({
|
||||
campaign: session.campaign,
|
||||
campaign,
|
||||
data: {
|
||||
secret: {
|
||||
text: newSecret,
|
||||
|
||||
@@ -8,8 +8,16 @@ import { useState } from "react";
|
||||
* Renders a secret row with a discovered checkbox and secret text.
|
||||
* Handles updating the discovered state and discoveredIn relationship.
|
||||
*/
|
||||
export const SecretRow = ({ secret, session }: { secret: Secret; session: Session }) => {
|
||||
const [checked, setChecked] = useState(!!(secret.data as any)?.secret?.discovered);
|
||||
export const SecretRow = ({
|
||||
secret,
|
||||
session,
|
||||
}: {
|
||||
secret: Secret;
|
||||
session?: Session;
|
||||
}) => {
|
||||
const [checked, setChecked] = useState(
|
||||
!!(secret.data as any)?.secret?.discovered,
|
||||
);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
async function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
|
||||
@@ -26,19 +34,24 @@ export const SecretRow = ({ secret, session }: { secret: Secret; session: Sessio
|
||||
},
|
||||
},
|
||||
});
|
||||
// Remove any existing discoveredIn relationship
|
||||
const rels = await pb.collection("relationships").getList(1, 1, {
|
||||
filter: `primary = "${secret.id}" && type = "discoveredIn"`,
|
||||
});
|
||||
if (rels.items.length > 0) {
|
||||
await pb.collection("relationships").delete(rels.items[0].id);
|
||||
}
|
||||
if (newChecked) {
|
||||
await pb.collection("relationships").create({
|
||||
primary: secret.id,
|
||||
secondary: [session.id],
|
||||
type: "discoveredIn",
|
||||
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"`,
|
||||
});
|
||||
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",
|
||||
});
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
setLoading(false);
|
||||
|
||||
Reference in New Issue
Block a user