WIP: Linking in document list. Working on copying relationships to new sessions
This commit is contained in:
@@ -5,8 +5,11 @@ import { SessionRow } from "@/components/documents/session/SessionRow";
|
||||
import { Button } from "@headlessui/react";
|
||||
import { useQueryClient, useSuspenseQuery } from "@tanstack/react-query";
|
||||
import { Loader } from "@/components/Loader";
|
||||
import type { Relationship } from "@/lib/types";
|
||||
|
||||
export const Route = createFileRoute("/_app/_authenticated/campaigns/$campaignId")({
|
||||
export const Route = createFileRoute(
|
||||
"/_app/_authenticated/campaigns/$campaignId",
|
||||
)({
|
||||
component: RouteComponent,
|
||||
pendingComponent: Loader,
|
||||
});
|
||||
@@ -26,6 +29,7 @@ function RouteComponent() {
|
||||
// Fetch all documents for this campaign
|
||||
const docs = await pb.collection("documents").getFullList({
|
||||
filter: `campaign = "${params.campaignId}"`,
|
||||
sort: "-created",
|
||||
});
|
||||
// Filter to only those with data.session
|
||||
const sessions = docs.filter((doc: any) => doc.data && doc.data.session);
|
||||
@@ -37,7 +41,22 @@ function RouteComponent() {
|
||||
});
|
||||
|
||||
const createNewSession = useCallback(async () => {
|
||||
await pb.collection("documents").create({
|
||||
// Check for a previous session
|
||||
const prevSession = await pb
|
||||
.collection("documents")
|
||||
.getFirstListItem(
|
||||
`campaign = "${campaign.id}" && json_extract(data, '$.session') IS NOT NULL`,
|
||||
{
|
||||
sort: "-created",
|
||||
},
|
||||
);
|
||||
|
||||
console.log("Previous session: ", {
|
||||
id: prevSession.id,
|
||||
created: prevSession.created,
|
||||
});
|
||||
|
||||
const newSession = await pb.collection("documents").create({
|
||||
campaign: campaign.id,
|
||||
data: {
|
||||
session: {
|
||||
@@ -45,6 +64,31 @@ function RouteComponent() {
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: ["campaign"] });
|
||||
|
||||
// If any, then copy things over
|
||||
if (prevSession) {
|
||||
const prevRelations = await pb
|
||||
.collection<Relationship>("relationships")
|
||||
.getFullList({
|
||||
filter: `primary = "${prevSession.id}"`,
|
||||
});
|
||||
|
||||
console.log(`Found ${prevRelations.length} previous relations`);
|
||||
|
||||
for (const relation of prevRelations) {
|
||||
console.log(
|
||||
`Adding ${relation.secondary.length} items to ${relation.type}`,
|
||||
);
|
||||
await pb.collection("relationships").create({
|
||||
primary: newSession.id,
|
||||
type: relation.type,
|
||||
seciondary: relation.secondary,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: ["campaign"] });
|
||||
}, [campaign]);
|
||||
|
||||
|
||||
@@ -8,7 +8,9 @@ import { useRouter } from "@tanstack/react-router";
|
||||
|
||||
export const Route = createFileRoute("/_app/_authenticated/campaigns/")({
|
||||
loader: async () => {
|
||||
const records = await pb.collection("campaigns").getFullList();
|
||||
const records = await pb.collection("campaigns").getFullList({
|
||||
sort: "-created",
|
||||
});
|
||||
return {
|
||||
campaigns: records.map((rec: any) => ({
|
||||
id: rec.id,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { RelationshipList } from "@/components/RelationshipList";
|
||||
import { DocumentEditForm } from "@/components/documents/DocumentEditForm";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import { displayName } from "@/lib/relationships";
|
||||
import { displayName, relationshipsForDocument } from "@/lib/relationships";
|
||||
import { RelationshipType, type AnyDocument } from "@/lib/types";
|
||||
import { Tab, TabGroup, TabList, TabPanel, TabPanels } from "@headlessui/react";
|
||||
import { createFileRoute, Link } from "@tanstack/react-router";
|
||||
@@ -23,17 +23,10 @@ function RouteComponent() {
|
||||
document: AnyDocument;
|
||||
};
|
||||
|
||||
const relationshipList = [
|
||||
RelationshipType.Scenes,
|
||||
RelationshipType.Secrets,
|
||||
RelationshipType.Locations,
|
||||
RelationshipType.Npcs,
|
||||
RelationshipType.Monsters,
|
||||
RelationshipType.Treasures,
|
||||
];
|
||||
const relationshipList = relationshipsForDocument(document);
|
||||
|
||||
return (
|
||||
<div className="max-w-xl mx-auto py-2 px-4">
|
||||
<div key={document.id} className="max-w-xl mx-auto py-2 px-4">
|
||||
<Link
|
||||
to="/document/$documentId/print"
|
||||
params={{ documentId: document.id }}
|
||||
|
||||
Reference in New Issue
Block a user