Revert "Start supporting edit forms for any document type."
This reverts commit 6845bd06bf.
This commit is contained in:
@@ -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 (
|
||||
<LocationForm
|
||||
target={{
|
||||
type: "new",
|
||||
campaignId,
|
||||
}}
|
||||
onSubmit={onCreate}
|
||||
/>
|
||||
);
|
||||
return <LocationForm campaign={campaignId} onCreate={onCreate} />;
|
||||
case RelationshipType.Monsters:
|
||||
return <MonsterForm campaign={campaignId} onCreate={onCreate} />;
|
||||
case RelationshipType.Npcs:
|
||||
return <NpcForm campaign={campaignId} onCreate={onCreate} />;
|
||||
case RelationshipType.Secrets:
|
||||
return <SecretForm campaign={campaignId} onCreate={onCreate} />;
|
||||
case RelationshipType.DiscoveredIn:
|
||||
return "Form not supported here";
|
||||
case RelationshipType.Treasures:
|
||||
return <TreasureForm campaign={campaignId} onCreate={onCreate} />;
|
||||
case RelationshipType.Scenes:
|
||||
return <SceneForm campaign={campaignId} onCreate={onCreate} />;
|
||||
case RelationshipType.DiscoveredIn:
|
||||
return "Form not supported here";
|
||||
}
|
||||
|
||||
return assertUnreachable(relationshipType);
|
||||
@@ -1,12 +0,0 @@
|
||||
import type { CampaignId, Document } from "@/lib/types";
|
||||
|
||||
export type FormTarget<T extends Document> =
|
||||
| {
|
||||
type: "new";
|
||||
campaignId: CampaignId;
|
||||
document?: undefined;
|
||||
}
|
||||
| {
|
||||
type: "existing";
|
||||
document: T;
|
||||
};
|
||||
@@ -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<Location>;
|
||||
onSubmit: (location: Location) => Promise<void>;
|
||||
campaign: CampaignId;
|
||||
onCreate: (location: Location) => Promise<void>;
|
||||
}) => {
|
||||
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<string | null>(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}
|
||||
>
|
||||
<h3>Create ne w location</h3>
|
||||
<h3>Create new location</h3>
|
||||
<div className="flex gap-5 w-full items-center">
|
||||
<label>Name</label>
|
||||
<input
|
||||
|
||||
Reference in New Issue
Block a user