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]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user