forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponentStore.ts
More file actions
201 lines (199 loc) · 6.2 KB
/
componentStore.ts
File metadata and controls
201 lines (199 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import type Block from "@/block";
import webComponent from "@/data/webComponent";
import useCanvasStore from "@/stores/canvasStore";
import usePageStore from "@/stores/pageStore";
import { BuilderComponent } from "@/types/Builder/BuilderComponent";
import getBlockTemplate from "@/utils/blockTemplate";
import { alert, confirm, getBlockInstance, getBlockObject } from "@/utils/helpers";
import { createDocumentResource, createResource } from "frappe-ui";
import { defineStore } from "pinia";
import { markRaw } from "vue";
import { toast } from "vue-sonner";
const useComponentStore = defineStore("componentStore", {
state: () => ({
components: <BlockComponent[]>[],
componentMap: <Map<string, Block>>new Map(),
componentDocMap: <Map<string, BuilderComponent>>new Map(),
fetchingComponent: new Set(),
selectedComponent: null as string | null,
}),
actions: {
async editComponent(block?: Block | null, componentName?: string) {
if (!block?.isExtendedFromComponent() && !componentName) {
return;
}
componentName =
componentName || (block?.extendedFromComponent as string) || (block?.isChildOfComponent as string);
await this.loadComponent(componentName);
const component = this.getComponent(componentName);
const componentBlock = this.getComponentBlock(componentName);
const canvasStore = useCanvasStore();
canvasStore.editOnCanvas(
componentBlock,
(block: Block) => this.saveComponent(block, componentName),
"Save Component",
component.component_name,
component.name,
);
},
saveComponent(block: Block, componentName: string) {
const pageStore = usePageStore();
return webComponent.setValue
.submit({
name: componentName,
block: getBlockObject(block),
})
.then(async (data: BuilderComponent) => {
this.setComponentMap(data);
toast.success("Component saved!", {
duration: 5000,
action: {
label: "Sync in all pages",
onClick: async () => {
const componentResource = createResource({
url: "builder.api.sync_component",
method: "POST",
params: {
component_id: data.name,
},
auto: true,
});
await toast.promise(componentResource.promise, {
loading: "Syncing component in all the pages...",
success: () => {
pageStore.fetchActivePage().then(() => {
pageStore.setPage(pageStore.activePage?.name as string);
});
return "Component synced in all the pages!";
},
error: () => "Error syncing component in all the pages!",
});
},
},
});
});
},
isComponentUsed(componentName: string) {
// TODO: Refactor or reduce complexity
const checkComponent = (block: Block) => {
if (block.extendedFromComponent === componentName) {
return true;
}
if (block.children) {
for (const child of block.children) {
if (checkComponent(child)) {
return true;
}
}
}
return false;
};
const canvasStore = useCanvasStore();
for (const block of canvasStore.activeCanvas?.getRootBlock()?.children || []) {
if (checkComponent(block)) {
return true;
}
}
return false;
},
getComponentBlock(componentName: string) {
return (
(this.componentMap.get(componentName) as Block) ||
getBlockInstance(getBlockTemplate("empty-component"))
);
},
async loadComponent(componentName: string) {
if (!this.componentMap.has(componentName) && !this.fetchingComponent.has(componentName)) {
this.fetchingComponent.add(componentName);
return this.fetchComponent(componentName)
.then((componentDoc) => {
this.setComponentMap(componentDoc);
})
.catch(() => {
const missingComponentDoc = {
name: componentName,
block: JSON.stringify(getBlockTemplate("missing-component")),
creation: "",
modified: "",
owner: "Administrator",
modified_by: "Administrator",
docstatus: 1 as 0 | 1 | 2,
};
this.setComponentMap(missingComponentDoc);
})
.finally(() => {
this.fetchingComponent.delete(componentName);
});
}
},
setComponentMap(componentDoc: BuilderComponent) {
this.componentDocMap.set(componentDoc.name, componentDoc);
this.componentMap.set(componentDoc.name, markRaw(getBlockInstance(componentDoc.block)));
},
async fetchComponent(componentName: string) {
const webComponentDoc = await createDocumentResource({
doctype: "Builder Component",
name: componentName,
auto: true,
});
await webComponentDoc.get.promise;
return webComponentDoc.doc as BuilderComponent;
},
getComponent(componentName: string) {
return this.componentDocMap.get(componentName) as BuilderComponent;
},
createComponent(obj: BuilderComponent, updateExisting = false) {
const component = this.getComponent(obj.name);
if (component) {
const existingComponent = component.block;
const newComponent = obj.block;
if (updateExisting && existingComponent !== newComponent) {
return webComponent.setValue.submit({
name: obj.name,
block: obj.block,
});
} else {
console.log("Skipping component update", obj.name);
return;
}
}
return webComponent.insert
.submit(obj)
.then(() => {
this.setComponentMap(obj);
})
.catch((e: { response: { status: number } }) => {
if (e?.response?.status === 409) {
if (updateExisting) {
return webComponent.setValue.submit({
name: obj.name,
block: obj.block,
});
}
}
});
},
getComponentName(componentId: string) {
let componentObj = this.componentDocMap.get(componentId);
if (!componentObj) {
return componentId;
}
return componentObj.component_name;
},
async deleteComponent(component: BlockComponent) {
if (this.isComponentUsed(component.name)) {
alert("Component is used in current page. You cannot delete it.");
} else {
const confirmed = await confirm(
`Are you sure you want to delete component: ${component.component_name}?`,
);
if (confirmed) {
webComponent.delete.submit(component.name).then(() => {
this.componentMap.delete(component.name);
});
}
}
},
},
});
export default useComponentStore;