Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Fix crash when duplicating a project (#958)

Roman Pominov5a3f767580ad
diff --git a/packages/project/src/db/build.ts b/packages/project/src/db/build.ts
index af952925fc..a2edc730c0 100644
--- a/packages/project/src/db/build.ts
+++ b/packages/project/src/db/build.ts
@@ -222,27 +222,36 @@ const createPages = async (
};
const clonePage = async (
- { projectId, source }: { projectId: Project["id"]; source: Page },
+ from: { projectId: Project["id"]; page: Page },
+ to: { projectId: Project["id"]; buildId: Build["id"] },
context: AppContext,
client: Prisma.TransactionClient = prisma
) => {
- const treeId = source.treeId;
- const tree = await db.tree.clone({ projectId, treeId }, context, client);
- return { ...source, id: uuid(), treeId: tree.id };
+ const tree = await db.tree.clone(
+ { projectId: from.projectId, treeId: from.page.treeId },
+ to,
+ context,
+ client
+ );
+ return { ...from.page, id: uuid(), treeId: tree.id };
};
const clonePages = async (
- { projectId, source }: { projectId: Project["id"]; source: Pages },
+ from: { projectId: Project["id"]; pages: Pages },
+ to: { projectId: Project["id"]; buildId: Build["id"] },
context: AppContext,
client: Prisma.TransactionClient = prisma
) => {
const clones = [];
- for (const page of source.pages) {
- clones.push(await clonePage({ projectId, source: page }, context, client));
+ for (const page of from.pages.pages) {
+ clones.push(
+ await clonePage({ projectId: from.projectId, page }, to, context, client)
+ );
}
return Pages.parse({
homePage: await clonePage(
- { projectId, source: source.homePage },
+ { projectId: from.projectId, page: from.pages.homePage },
+ to,
context,
client
),
@@ -327,7 +336,11 @@ export async function create(
client
)
: await clonePages(
- { projectId: props.projectId, source: props.sourceBuild.pages },
+ {
+ projectId: props.sourceBuild.projectId,
+ pages: props.sourceBuild.pages,
+ },
+ { projectId: props.projectId, buildId: build.id },
context,
client
);
diff --git a/packages/project/src/db/tree.ts b/packages/project/src/db/tree.ts
index e8d07cc677..23fccf082a 100644
--- a/packages/project/src/db/tree.ts
+++ b/packages/project/src/db/tree.ts
@@ -157,15 +157,17 @@ export const loadById = async (
};
export const clone = async (
- { projectId, treeId }: { projectId: Project["id"]; treeId: Tree["id"] },
+ from: { projectId: Project["id"]; treeId: Tree["id"] },
+ to: { projectId: Project["id"]; buildId: Project["id"] },
context: AppContext,
client: Prisma.TransactionClient = prisma
) => {
- const tree = await loadById({ projectId, treeId }, context, client);
+ const tree = await loadById(from, context, client);
if (tree === null) {
- throw new Error(`Tree ${treeId} not found`);
+ throw new Error(`Tree ${from.projectId}/${from.treeId} not found`);
}
- return await create(tree, client);
+
+ return await create({ ...tree, ...to }, client);
};
export const patch = async (