From c28073d5165e1198f1abc9787353bd4693583242 Mon Sep 17 00:00:00 2001 From: Drew Haven Date: Fri, 13 Jun 2025 11:16:37 -0700 Subject: [PATCH] Revert "Start supporting edit forms for any document type." This reverts commit 6845bd06bf624e455f9871157445832bcbcc9f94. --- src/components/RelationshipList.tsx | 4 +- ...latedDocumentForm.tsx => DocumentForm.tsx} | 18 ++---- src/components/documents/Forms.ts | 12 ---- .../documents/location/LocationForm.tsx | 55 +++++++------------ 4 files changed, 26 insertions(+), 63 deletions(-) rename src/components/documents/{NewRelatedDocumentForm.tsx => DocumentForm.tsx} (83%) delete mode 100644 src/components/documents/Forms.ts diff --git a/src/components/RelationshipList.tsx b/src/components/RelationshipList.tsx index d168953..e86c3e2 100644 --- a/src/components/RelationshipList.tsx +++ b/src/components/RelationshipList.tsx @@ -3,7 +3,7 @@ import { pb } from "@/lib/pocketbase"; import type { Document, RelationshipType } from "@/lib/types"; import { useState } from "react"; import { Loader } from "./Loader"; -import { NewRelatedDocumentForm } from "./documents/NewRelatedDocumentForm"; +import { DocumentForm } from "./documents/DocumentForm"; import { DocumentRow } from "./documents/DocumentRow"; interface RelationshipListProps { @@ -68,7 +68,7 @@ export function RelationshipList({ error={error} renderRow={(document) => } newItemForm={(onSubmit) => ( - { diff --git a/src/components/documents/NewRelatedDocumentForm.tsx b/src/components/documents/DocumentForm.tsx similarity index 83% rename from src/components/documents/NewRelatedDocumentForm.tsx rename to src/components/documents/DocumentForm.tsx index 3d7d8d0..d700acb 100644 --- a/src/components/documents/NewRelatedDocumentForm.tsx +++ b/src/components/documents/DocumentForm.tsx @@ -7,13 +7,13 @@ import { SecretForm } from "./secret/SecretForm"; import { TreasureForm } from "./treasure/TreasureForm"; function assertUnreachable(_x: never): never { - throw new Error("NewRelatedDocumentForm switch is not exhaustive"); + throw new Error("DocumentForm switch is not exhaustive"); } /** * Renders a form for any document type depending on the relationship. */ -export const NewRelatedDocumentForm = ({ +export const DocumentForm = ({ campaignId, relationshipType, onCreate, @@ -24,27 +24,19 @@ export const NewRelatedDocumentForm = ({ }) => { switch (relationshipType) { case RelationshipType.Locations: - return ( - - ); + return ; case RelationshipType.Monsters: return ; case RelationshipType.Npcs: return ; case RelationshipType.Secrets: return ; + case RelationshipType.DiscoveredIn: + return "Form not supported here"; case RelationshipType.Treasures: return ; case RelationshipType.Scenes: return ; - case RelationshipType.DiscoveredIn: - return "Form not supported here"; } return assertUnreachable(relationshipType); diff --git a/src/components/documents/Forms.ts b/src/components/documents/Forms.ts deleted file mode 100644 index f451fd5..0000000 --- a/src/components/documents/Forms.ts +++ /dev/null @@ -1,12 +0,0 @@ -import type { CampaignId, Document } from "@/lib/types"; - -export type FormTarget = - | { - type: "new"; - campaignId: CampaignId; - document?: undefined; - } - | { - type: "existing"; - document: T; - }; diff --git a/src/components/documents/location/LocationForm.tsx b/src/components/documents/location/LocationForm.tsx index c6a1dc4..7eed94b 100644 --- a/src/components/documents/location/LocationForm.tsx +++ b/src/components/documents/location/LocationForm.tsx @@ -1,22 +1,19 @@ import { useState } from "react"; -import type { Location } from "@/lib/types"; +import type { CampaignId, Location } from "@/lib/types"; import { pb } from "@/lib/pocketbase"; -import type { FormTarget } from "../Forms"; /** * Renders a form to add a new location. Calls onCreate with the new location document. */ export const LocationForm = ({ - target, - onSubmit, + campaign, + onCreate, }: { - target: FormTarget; - onSubmit: (location: Location) => Promise; + campaign: CampaignId; + onCreate: (location: Location) => Promise; }) => { - const [name, setName] = useState(target.document?.data.location.name ?? ""); - const [description, setDescription] = useState( - target.document?.data.location.description ?? "", - ); + const [name, setName] = useState(""); + const [description, setDescription] = useState(""); const [adding, setAdding] = useState(false); const [error, setError] = useState(null); @@ -26,32 +23,18 @@ export const LocationForm = ({ setAdding(true); setError(null); try { - if (target.type === "new") { - const locationDoc: Location = await pb.collection("documents").create({ - campaign: target.campaignId, - data: { - location: { - name, - description, - }, + const locationDoc: Location = await pb.collection("documents").create({ + campaign, + data: { + location: { + name, + description, }, - }); - setName(""); - setDescription(""); - await onSubmit(locationDoc); - } else { - const locationDoc: Location = await pb - .collection("documents") - .update(target.document.id, { - data: { - location: { - name, - description, - }, - }, - }); - await onSubmit(locationDoc); - } + }, + }); + setName(""); + setDescription(""); + await onCreate(locationDoc); } catch (e: any) { setError(e?.message || "Failed to add location."); } finally { @@ -64,7 +47,7 @@ export const LocationForm = ({ className="flex items-left flex-col gap-2 mt-4" onSubmit={handleSubmit} > -

Create ne w location

+

Create new location