mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-08 21:15:18 +09:00
delete tree when deleting a page
This commit is contained in:
1 parent
d02d71d039
commit
bdf7534201
3 files changed
+19
-1
No files matched your search
@@ -136,11 +136,18 @@ export const editPage = async (
|
||||
};
|
||||
|
||||
export const deletePage = async (buildId: Build["id"], pageId: Page["id"]) => {
|
||||
// @todo: delete tree etc.
|
||||
return updatePages(buildId, async (currentPages) => {
|
||||
if (pageId === currentPages.homePage.id) {
|
||||
throw new Error("Cannot delete home page");
|
||||
}
|
||||
|
||||
const page = pagesUtils.findById(currentPages, pageId);
|
||||
if (page === undefined) {
|
||||
throw new Error(`Page with id "${pageId}" not found`);
|
||||
}
|
||||
|
||||
await db.tree.deleteById(page.treeId);
|
||||
|
||||
return {
|
||||
homePage: currentPages.homePage,
|
||||
pages: currentPages.pages.filter((page) => page.id !== pageId),
|
||||
|
||||
@@ -21,6 +21,10 @@ export const loadByTreeId = async (treeId: Tree["id"]) => {
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteByTreeId = async (treeId: Tree["id"]) => {
|
||||
await prisma.instanceProps.deleteMany({ where: { treeId } });
|
||||
};
|
||||
|
||||
export const clone = async ({
|
||||
previousTreeId,
|
||||
nextTreeId,
|
||||
|
||||
@@ -3,6 +3,7 @@ import { applyPatches, type Patch } from "immer";
|
||||
import { prisma } from "@webstudio-is/prisma-client";
|
||||
import { Tree as DbTree } from "@prisma/client";
|
||||
import { utils } from "../index";
|
||||
import { props } from ".";
|
||||
|
||||
export const createRootInstance = (breakpoints: Array<Breakpoint>) => {
|
||||
// Take the smallest breakpoint as default
|
||||
@@ -22,6 +23,11 @@ export const create = async (root: Instance): Promise<DbTree> => {
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteById = async (treeId: string): Promise<void> => {
|
||||
await props.deleteByTreeId(treeId);
|
||||
await prisma.tree.delete({ where: { id: treeId } });
|
||||
};
|
||||
|
||||
export const loadById = async (treeId: string): Promise<Tree | null> => {
|
||||
const tree = await prisma.tree.findUnique({
|
||||
where: { id: treeId },
|
||||
@@ -42,6 +48,7 @@ export const clone = async (treeId: string) => {
|
||||
if (tree === null) {
|
||||
throw new Error(`Tree ${treeId} not found`);
|
||||
}
|
||||
// @todo: clone props?
|
||||
return await create(tree.root);
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user