Adds scenes
This commit is contained in:
66
src/components/documents/scene/SceneForm.tsx
Normal file
66
src/components/documents/scene/SceneForm.tsx
Normal file
@@ -0,0 +1,66 @@
|
||||
// SceneForm.tsx
|
||||
// Form for adding a new scene to a session.
|
||||
import { useState } from "react";
|
||||
import type { CampaignId, Scene } from "@/lib/types";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
|
||||
/**
|
||||
* Renders a form to add a new scene. Calls onCreate with the new scene document.
|
||||
*/
|
||||
export const SceneForm = ({
|
||||
campaign,
|
||||
onCreate,
|
||||
}: {
|
||||
campaign: CampaignId;
|
||||
onCreate: (scene: Scene) => Promise<void>;
|
||||
}) => {
|
||||
const [text, setText] = useState("");
|
||||
const [adding, setAdding] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
async function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
if (!text.trim()) return;
|
||||
setAdding(true);
|
||||
setError(null);
|
||||
try {
|
||||
const sceneDoc: Scene = await pb.collection("documents").create({
|
||||
campaign,
|
||||
data: {
|
||||
scene: {
|
||||
text,
|
||||
},
|
||||
},
|
||||
});
|
||||
setText("");
|
||||
await onCreate(sceneDoc);
|
||||
} catch (e: any) {
|
||||
setError(e?.message || "Failed to add scene.");
|
||||
} finally {
|
||||
setAdding(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<form className="flex items-center gap-2 mt-4" onSubmit={handleSubmit}>
|
||||
<h3>Create new scene</h3>
|
||||
<input
|
||||
type="text"
|
||||
className="flex-1 px-3 py-2 rounded bg-slate-800 text-slate-100 border border-slate-700 focus:outline-none focus:ring-2 focus:ring-violet-500"
|
||||
placeholder="Add a new scene..."
|
||||
value={text}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
disabled={adding}
|
||||
aria-label="Add new scene"
|
||||
/>
|
||||
{error && <div className="text-red-400 mt-2 text-sm">{error}</div>}
|
||||
<button
|
||||
type="submit"
|
||||
className="px-4 py-2 rounded bg-emerald-600 hover:bg-emerald-700 text-white font-semibold transition-colors disabled:opacity-60"
|
||||
disabled={adding || !text.trim()}
|
||||
>
|
||||
{adding ? "Adding..." : "Create"}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
};
|
||||
25
src/components/documents/scene/SceneRow.tsx
Normal file
25
src/components/documents/scene/SceneRow.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import type { Scene } from "@/lib/types";
|
||||
|
||||
/**
|
||||
* Renders an editable scene row
|
||||
*/
|
||||
export const SceneRow = ({ scene }: { scene: Scene }) => {
|
||||
async function saveScene(text: string) {
|
||||
await pb.collection("documents").update(scene.id, {
|
||||
data: {
|
||||
...scene.data,
|
||||
scene: {
|
||||
text,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="">
|
||||
<AutoSaveTextarea value={scene.data.scene.text} onSave={saveScene} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user