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.

feat: delete resources along with variables (#2749)

Bogdan Chadkinf85d7fa4fdda
diff --git a/apps/builder/app/builder/features/settings-panel/variables-section.tsx b/apps/builder/app/builder/features/settings-panel/variables-section.tsx
index 874b831106..1fc119aa63 100644
--- a/apps/builder/app/builder/features/settings-panel/variables-section.tsx
+++ b/apps/builder/app/builder/features/settings-panel/variables-section.tsx
@@ -118,9 +118,19 @@ const $usedVariables = computed([$props, $resources], (props, resources) => {
});
const deleteVariable = (variableId: DataSource["id"]) => {
- serverSyncStore.createTransaction([$dataSources], (dataSources) => {
- dataSources.delete(variableId);
- });
+ serverSyncStore.createTransaction(
+ [$dataSources, $resources],
+ (dataSources, resources) => {
+ const dataSource = dataSources.get(variableId);
+ if (dataSource === undefined) {
+ return;
+ }
+ dataSources.delete(variableId);
+ if (dataSource.type === "resource") {
+ resources.delete(dataSource.resourceId);
+ }
+ }
+ );
};
const EmptyVariables = () => {
diff --git a/apps/builder/app/shared/instance-utils.test.ts b/apps/builder/app/shared/instance-utils.test.ts
index 9ce17590fc..dde7aef8e2 100644
--- a/apps/builder/app/shared/instance-utils.test.ts
+++ b/apps/builder/app/shared/instance-utils.test.ts
@@ -37,6 +37,7 @@ import {
$pages,
$project,
$props,
+ $resources,
$styleSourceSelections,
$styleSources,
$styles,
@@ -606,6 +607,46 @@ describe("delete instance", () => {
])
);
});
+
+ test("delete resource along with variable", () => {
+ $instances.set(
+ toMap([
+ createInstance("body", "Body", [{ type: "id", value: "box" }]),
+ createInstance("box", "Box", []),
+ ])
+ );
+ $resources.set(
+ toMap([
+ {
+ id: "resourceId",
+ name: "My Resource",
+ url: `""`,
+ method: "get",
+ headers: [],
+ },
+ ])
+ );
+ $dataSources.set(
+ toMap([
+ {
+ id: "resourceVariableId",
+ scopeInstanceId: "box",
+ name: "My Resource Variable",
+ type: "resource",
+ resourceId: "resourceId",
+ },
+ ])
+ );
+ $registeredComponentMetas.set(createFakeComponentMetas({}));
+
+ deleteInstance(["box", "body"]);
+
+ expect($instances.get()).toEqual(
+ toMap([createInstance("body", "Body", [])])
+ );
+ expect($dataSources.get()).toEqual(new Map());
+ expect($resources.get()).toEqual(new Map());
+ });
});
describe("get instances slice", () => {
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index cd85cdfcd4..f7ad38ddfd 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -44,6 +44,7 @@ import {
$project,
$breakpoints,
$pages,
+ $resources,
} from "./nano-states";
import {
type DroppableTarget,
@@ -430,6 +431,7 @@ export const deleteInstance = (instanceSelector: InstanceSelector) => {
$styleSources,
$styles,
$dataSources,
+ $resources,
],
(
instances,
@@ -437,7 +439,8 @@ export const deleteInstance = (instanceSelector: InstanceSelector) => {
styleSourceSelections,
styleSources,
styles,
- dataSources
+ dataSources,
+ resources
) => {
let targetInstanceId = instanceSelector[0];
const parentInstanceId = instanceSelector[1];
@@ -498,6 +501,9 @@ export const deleteInstance = (instanceSelector: InstanceSelector) => {
instanceIds.has(dataSource.scopeInstanceId)
) {
dataSources.delete(dataSource.id);
+ if (dataSource.type === "resource") {
+ resources.delete(dataSource.resourceId);
+ }
}
}
for (const instanceId of instanceIds) {