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.

delete props using transaction

Oleg Isonena9bcc164deee
diff --git a/app/canvas/canvas.tsx b/app/canvas/canvas.tsx
index de615a52d5..f58828b181 100644
--- a/app/canvas/canvas.tsx
+++ b/app/canvas/canvas.tsx
@@ -32,7 +32,7 @@ import { WrapperComponentDev } from "./features/wrapper-component";
import { rootInstanceContainer, useRootInstance } from "./shared/nano-values";
import { usePeriodicSync } from "./shared/use-periodic-sync";
import { createTransaction } from "~/lib/sync-engine";
-import { useUpdateProps } from "./shared/props";
+import { useManageProps } from "./shared/props";
const useElementsTree = () => {
const [rootInstance] = useRootInstance();
@@ -82,7 +82,7 @@ type DesignModeProps = {
const DesignMode = ({ treeId }: DesignModeProps) => {
useDragDropHandlers();
useUpdateStyle();
- useUpdateProps();
+ useManageProps();
usePublishSelectedInstance({ treeId });
useInsertInstance();
useReparentInstance();
diff --git a/app/canvas/shared/props.ts b/app/canvas/shared/props.ts
index 9a1d7d2792..58cbd65460 100644
--- a/app/canvas/shared/props.ts
+++ b/app/canvas/shared/props.ts
@@ -2,13 +2,17 @@ import {
type UserPropsUpdates,
useSubscribe,
allUserPropsContainer,
+ DeleteProp,
} from "@webstudio-is/sdk";
import { createTransaction, register } from "~/lib/sync-engine";
-import { updateAllUserPropsMutable } from "~/shared/props-utils";
+import {
+ updateAllUserPropsMutable,
+ deletePropMutable,
+} from "~/shared/props-utils";
register("props", allUserPropsContainer);
-export const useUpdateProps = () => {
+export const useManageProps = () => {
useSubscribe<"updateProps", UserPropsUpdates>(
"updateProps",
(userPropsUpdates) => {
@@ -17,4 +21,10 @@ export const useUpdateProps = () => {
});
}
);
+
+ useSubscribe<"deleteProp", DeleteProp>("deleteProp", (deleteProp) => {
+ createTransaction([allUserPropsContainer], (allUserProps) => {
+ deletePropMutable(allUserProps, deleteProp);
+ });
+ });
};
diff --git a/app/designer/features/settings-panel/use-props-logic.ts b/app/designer/features/settings-panel/use-props-logic.ts
index c37c103863..7321ec1a3a 100644
--- a/app/designer/features/settings-panel/use-props-logic.ts
+++ b/app/designer/features/settings-panel/use-props-logic.ts
@@ -69,7 +69,7 @@ export const usePropsLogic = ({
publish<"deleteProp", DeleteProp>({
type: "deleteProp",
payload: {
- propsId: selectedInstanceData.props.id,
+ instanceId: selectedInstanceData.id,
propId: id,
},
});
diff --git a/app/designer/features/sync/use-sync.ts b/app/designer/features/sync/use-sync.ts
index 9b6868f751..cac3c8a049 100644
--- a/app/designer/features/sync/use-sync.ts
+++ b/app/designer/features/sync/use-sync.ts
@@ -1,4 +1,4 @@
-import { type DeleteProp, type Project } from "@webstudio-is/sdk";
+import { type Project } from "@webstudio-is/sdk";
import type { Config } from "~/config";
import { useSubscribe } from "~/designer/features/canvas-iframe";
import { enqueue } from "./queue";
@@ -9,18 +9,6 @@ import { type SyncItem } from "~/lib/sync-engine";
// and backend fetches and updates big objects, so if we send quickly,
// we end up overwriting things
export const useSync = ({ project }: { config: Config; project: Project }) => {
- useSubscribe<"deleteProp", DeleteProp>(
- "deleteProp",
- ({ propsId, propId }) => {
- enqueue(() =>
- fetch(`/rest/props/delete-prop`, {
- method: "post",
- body: JSON.stringify({ propsId, propId }),
- })
- );
- }
- );
-
useSubscribe<"syncChanges", Array<SyncItem>>(
"syncChanges",
(transactions) => {
diff --git a/app/routes/rest/props.delete-prop.ts b/app/routes/rest/props.delete-prop.ts
deleted file mode 100644
index 3a7995b4b9..0000000000
--- a/app/routes/rest/props.delete-prop.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-import { type ActionFunction } from "remix";
-import * as db from "~/shared/db";
-
-export const action: ActionFunction = async ({ request }) => {
- const data = await request.json();
- await db.props.deleteProp(data);
- return { status: "ok" };
-};
diff --git a/app/shared/db/props.server.ts b/app/shared/db/props.server.ts
index 475d61987d..cbbe311f59 100644
--- a/app/shared/db/props.server.ts
+++ b/app/shared/db/props.server.ts
@@ -70,26 +70,6 @@ export const update = async ({
});
};
-export const deleteProp = async ({
- propsId,
- propId,
-}: {
- propsId: InstanceProps["id"];
- propId: UserProp["id"];
-}) => {
- // @todo update in one command, remove queueing logic on the ui
- // as of prisma client v3.10.0 updating composite types like in this doc didn't work
- // https://www.prisma.io/docs/concepts/components/prisma-client/composite-types
- const props = (await loadById(propsId))?.props ?? [];
- const index = props.findIndex((prop) => prop.id == propId);
- if (index === -1) return;
- props.splice(index, 1);
- await prisma.instanceProps.update({
- where: { id: propsId },
- data: { props },
- });
-};
-
export const deleteProps = async ({
instanceId,
treeId,
diff --git a/app/shared/props-utils/__snapshots__/delete-prop.test.ts.snap b/app/shared/props-utils/__snapshots__/delete-prop.test.ts.snap
new file mode 100644
index 0000000000..1debedd303
--- /dev/null
+++ b/app/shared/props-utils/__snapshots__/delete-prop.test.ts.snap
@@ -0,0 +1,40 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`Delete prop found and deleted 1`] = `
+Object {
+ "instanceId": Object {
+ "id": "id",
+ "instanceId": "instanceId",
+ "props": Array [
+ Object {
+ "id": "propId1",
+ "prop": "a",
+ "value": "1",
+ },
+ ],
+ "treeId": "treeId",
+ },
+}
+`;
+
+exports[`Delete prop not found and not deleted 1`] = `
+Object {
+ "instanceId": Object {
+ "id": "id",
+ "instanceId": "instanceId",
+ "props": Array [
+ Object {
+ "id": "propId",
+ "prop": "b",
+ "value": "1",
+ },
+ Object {
+ "id": "propId1",
+ "prop": "a",
+ "value": "1",
+ },
+ ],
+ "treeId": "treeId",
+ },
+}
+`;
diff --git a/app/shared/props-utils/__snapshots__/update-props.test.ts.snap b/app/shared/props-utils/__snapshots__/update-props.test.ts.snap
index 2c0a5a7ab4..2712a36a36 100644
--- a/app/shared/props-utils/__snapshots__/update-props.test.ts.snap
+++ b/app/shared/props-utils/__snapshots__/update-props.test.ts.snap
@@ -17,23 +17,6 @@ Object {
}
`;
-exports[`Update props with deleted props 1`] = `
-Object {
- "instanceId": Object {
- "id": "id",
- "instanceId": "instanceId",
- "props": Array [
- Object {
- "id": "propId",
- "prop": "a",
- "value": "1",
- },
- ],
- "treeId": "treeId",
- },
-}
-`;
-
exports[`Update props with empty updates 1`] = `
Object {
"instanceId": Object {
diff --git a/app/shared/props-utils/delete-prop.test.ts b/app/shared/props-utils/delete-prop.test.ts
new file mode 100644
index 0000000000..98ad18490a
--- /dev/null
+++ b/app/shared/props-utils/delete-prop.test.ts
@@ -0,0 +1,46 @@
+import { type AllUserProps } from "@webstudio-is/sdk";
+import { deletePropMutable } from "./delete-prop";
+
+describe("Delete prop", () => {
+ test("found and deleted", () => {
+ const propsMap: AllUserProps = {
+ instanceId: {
+ id: "id",
+ instanceId: "instanceId",
+ treeId: "treeId",
+ props: [
+ { id: "propId", prop: "b", value: "1" },
+ { id: "propId1", prop: "a", value: "1" },
+ ],
+ },
+ };
+
+ const isDeleted = deletePropMutable(propsMap, {
+ instanceId: "instanceId",
+ propId: "propId",
+ });
+ expect(propsMap).toMatchSnapshot();
+ expect(isDeleted).toBeTruthy();
+ });
+
+ test("not found and not deleted", () => {
+ const propsMap: AllUserProps = {
+ instanceId: {
+ id: "id",
+ instanceId: "instanceId",
+ treeId: "treeId",
+ props: [
+ { id: "propId", prop: "b", value: "1" },
+ { id: "propId1", prop: "a", value: "1" },
+ ],
+ },
+ };
+
+ const isDeleted = deletePropMutable(propsMap, {
+ instanceId: "instanceId",
+ propId: "notFound",
+ });
+ expect(propsMap).toMatchSnapshot();
+ expect(isDeleted).toBeFalsy();
+ });
+});
diff --git a/app/shared/props-utils/delete-prop.ts b/app/shared/props-utils/delete-prop.ts
new file mode 100644
index 0000000000..947bd79f21
--- /dev/null
+++ b/app/shared/props-utils/delete-prop.ts
@@ -0,0 +1,13 @@
+import { type AllUserProps, type DeleteProp } from "@webstudio-is/sdk";
+
+export const deletePropMutable = (
+ allProps: AllUserProps,
+ deleteProp: DeleteProp
+) => {
+ const prop = allProps[deleteProp.instanceId];
+ if (prop === undefined) return false;
+ const index = prop.props.findIndex(({ id }) => id === deleteProp.propId);
+ if (index === -1) return false;
+ prop.props.splice(index, 1);
+ return true;
+};
diff --git a/app/shared/props-utils/index.ts b/app/shared/props-utils/index.ts
index 234a81bdb5..e76c58df03 100644
--- a/app/shared/props-utils/index.ts
+++ b/app/shared/props-utils/index.ts
@@ -1 +1,2 @@
export * from "./update-props";
+export * from "./delete-prop";
diff --git a/package.json b/package.json
index 15be975160..52552c8b42 100644
--- a/package.json
+++ b/package.json
@@ -43,7 +43,7 @@
"@remix-run/vercel": "^1.2.3",
"@stitches/react": "^1.2.6",
"@stitches/stringify": "^1.2.6",
- "@webstudio-is/sdk": "^0.1.20",
+ "@webstudio-is/sdk": "^0.1.21",
"bson-objectid": "^2.0.2",
"camelcase": "^6.3.0",
"hyphenate-style-name": "^1.0.4",
diff --git a/yarn.lock b/yarn.lock
index 7683e2e17e..623e804fb0 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1820,10 +1820,10 @@
dependencies:
web-streams-polyfill "^3.1.1"
-"@webstudio-is/sdk@^0.1.20":
- version "0.1.20"
- resolved "https://registry.yarnpkg.com/@webstudio-is/sdk/-/sdk-0.1.20.tgz#134fcc2eb166a0c0b648fac4fb008c98666f88c6"
- integrity sha512-WI379pIabj6WU05qaP9MeUFE+9uCQu8uoEAXkxQ9mjkFpQUNY8lIn3ncsvmxZvyoKCaFIZ2UBAs2Pb7Q6fwSHQ==
+"@webstudio-is/sdk@^0.1.21":
+ version "0.1.21"
+ resolved "https://registry.yarnpkg.com/@webstudio-is/sdk/-/sdk-0.1.21.tgz#245f48b4be4a72524338c9017afecba0ff8bfaef"
+ integrity sha512-pafxgtw6YwxOUKh1cyazurFA565xBjqJMpb6h6AaBNErbwJtTzTb7QFVz04skBdEMOnuhCACvPqp+tJU2XEP+w==
dependencies:
"@prisma/client" "^3.10.0"
"@stitches/react" "^1.2.7"