Skip to content

Commit c459220

Browse files
Shanon LeeShanon Lee
authored andcommitted
Fix bug: do not mutate vuex store outside of mutation handler. Modified CreateComponent activeComponentData computed property to include deepclone of activeComponentObj
1 parent 3ae5c48 commit c459220

File tree

3 files changed

+11
-46
lines changed

3 files changed

+11
-46
lines changed

src/components/ComponentDisplay.vue

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,12 @@ Description:
9292
</template>
9393

9494
<script>
95-
9695
import { mapState, mapActions } from "vuex";
9796
import VueDraggableResizable from "vue-draggable-resizable";
97+
const cloneDeep = require('lodash.clonedeep')
9898
// import "vue-draggable-resizable/dist/VueDraggableResizable.css";
9999
// import Vue3DraggableResizable from "vue3-draggable-resizable";
100100
// import "vue3-draggable-resizable/dist/Vue3DraggableResizable.css";
101-
102101
export default {
103102
name: "ComponentDisplay",
104103
components: {
@@ -113,7 +112,7 @@ export default {
113112
mockImg: false,
114113
initialPosition: { x: 0, y: 0 },
115114
initialSize: { w: 0, h: 0 },
116-
htmlElements: []
115+
htmlElements: [],
117116
};
118117
},
119118
mounted() {
@@ -125,7 +124,6 @@ export default {
125124
}
126125
}
127126
});
128-
129127
window.addEventListener("keyup", event => {
130128
if (event.key === "Delete") {
131129
if (this.activeComponent) {
@@ -141,13 +139,11 @@ export default {
141139
this.$store.dispatch("copyActiveComponent");
142140
}
143141
});
144-
145142
window.addEventListener('paste', () => {
146143
this.$store.dispatch("pasteActiveComponent");
147144
// console.log('pasted');
148145
})
149146
},
150-
151147
computed: {
152148
...mapState([
153149
"routes",
@@ -158,26 +154,25 @@ export default {
158154
"imagePath",
159155
"activeComponentObj"
160156
]),
161-
162157
// used in VueDraggableResizeable component
163158
activeRouteArray() {
159+
// console.log(this.routes[this.activeRoute]);
164160
return this.routes[this.activeRoute];
165161
},
166-
167162
// used to delete active component
168163
activeComponentData() {
169-
return this.activeComponentObj;
164+
// Must deep clone this so we are not directly mutating state
165+
return cloneDeep(this.activeComponentObj);
170166
},
171-
172167
// childList () {
173168
// return this.componentMap[componentData.componentName].children
174169
// },
175-
176170
options() {
177171
// checks if component has any parents and pushes them into lineage
178172
const checkParents = (component, lineage = [component.componentName]) => {
179173
if (!Object.keys(component.parent).length) return lineage;
180174
for (var parents in component.parent) {
175+
// Mutating?
181176
lineage.push(parents);
182177
checkParents(component.parent[parents], lineage);
183178
}
@@ -204,13 +199,11 @@ export default {
204199
if (!exceptions.has(component)) return component;
205200
});
206201
},
207-
208202
userImage() {
209203
const imgSrc = `file://` + this.imagePath[this.activeRoute];
210204
// console.log('imgSrc is: ', imgSrc)
211205
return imgSrc;
212206
},
213-
214207
// updates display with mockup image
215208
mockBg() {
216209
return this.imagePath[this.activeRoute]
@@ -244,7 +237,6 @@ export default {
244237
});
245238
}
246239
},
247-
248240
methods: {
249241
...mapActions([
250242
"setActiveComponent",
@@ -256,7 +248,6 @@ export default {
256248
"updateStartingSize",
257249
"updateComponentSize"
258250
]),
259-
260251
// records component's initial position in case of drag
261252
recordInitialPosition: function(e) {
262253
// console.log('recording initial position: ', this.initialPosition)
@@ -266,7 +257,6 @@ export default {
266257
this.initialPosition.x = this.activeComponentData.x;
267258
this.initialPosition.y = this.activeComponentData.y;
268259
},
269-
270260
// records component's initial size/position in case of resize
271261
recordInitialSize: function(e) {
272262
// console.log('recording initial size')
@@ -275,7 +265,6 @@ export default {
275265
this.initialPosition.x = this.activeComponentData.x;
276266
this.initialPosition.y = this.activeComponentData.y;
277267
},
278-
279268
// sets component's ending size/position
280269
finishedResize: function(x, y, w, h) {
281270
// console.log('FINISHED RESIZING')
@@ -297,7 +286,6 @@ export default {
297286
this.updateComponentSize(payload);
298287
}
299288
},
300-
301289
finishedDrag: function(x, y) {
302290
// console.log('FINISHED DRAGGING')
303291
let payload = {
@@ -316,36 +304,29 @@ export default {
316304
this.updateComponentPosition(payload);
317305
}
318306
},
319-
320307
/* Records size/position
321308
Add @resizing="onResize" to VueDraggableResizable #component-box to use
322-
323309
onResize: function (x, y, width, height) {
324310
this.activeComponentData.x = x
325311
this.activeComponentData.y = y
326312
this.activeComponentData.w = width
327313
this.activeComponentData.h = height
328-
329314
this.componentMap[this.activeComponent].x = x
330315
this.componentMap[this.activeComponent].y = y
331316
this.componentMap[this.activeComponent].w = width
332317
this.componentMap[this.activeComponent].h = height
333318
},
334319
*/
335-
336320
/* Records component's position
337321
Add @dragging="onDrag" to VueDraggableResizable #component-box to use
338-
339322
onDrag: function (x, y) {
340323
console.log('ondrag')
341324
this.activeComponentData.x = x
342325
this.activeComponentData.y = y
343-
344326
this.componentMap[this.activeComponent].x = x
345327
this.componentMap[this.activeComponent].y = y
346328
},
347329
*/
348-
349330
// unhighlights all inactive components
350331
onActivated(componentData) {
351332
// console.log('onActivated - comp display, componentData', componentData)
@@ -364,25 +345,21 @@ export default {
364345
}
365346
this.activeComponentData.isActive = true;
366347
},
367-
368348
// deactivated is emitted before activated
369349
onDeactivated() {
370350
if (this.activeComponent !== "") {
371351
this.activeComponentData.isActive = false;
372352
}
373353
},
374-
375354
// renders modal with Update Children and Layer in it
376355
handleAddChild() {
377356
this.modalOpen = true;
378357
},
379-
380358
// used when user selects to add child from dropdown
381359
handleSelect(value) {
382360
// console.log('selected child component: ', value)
383361
this.updateActiveComponentChildrenValue(value);
384362
},
385-
386363
// user can change component's layer order
387364
handleLayer(e) {
388365
// console.log('handeLayer\'s e: ', e)
@@ -397,7 +374,6 @@ export default {
397374
if (e.target.innerText === "-" && payload.z > 0) payload.z--;
398375
this.updateComponentLayer(payload);
399376
},
400-
401377
// if user clicks on display grid, resets active component to ''
402378
handleClick(event) {
403379
if (event.target.className === "component-display grid-bg") {
@@ -409,7 +385,6 @@ export default {
409385
copyActiveComponent() {
410386
// console.log('copied');
411387
}
412-
413388
},
414389
watch: {
415390
activeComponent: function() {
@@ -435,7 +410,6 @@ export default {
435410
line-height: 1.2;
436411
z-index: -1;
437412
}
438-
439413
.component-html-info {
440414
display: flex;
441415
font-size: 14px;
@@ -485,7 +459,6 @@ export default {
485459
#269;
486460
behavior: url(/pie/PIE.htc);
487461
}
488-
489462
.menu {
490463
margin-bottom: 0px !important;
491464
}
@@ -517,4 +490,4 @@ export default {
517490
#counter {
518491
margin-top: 20px;
519492
}
520-
</style>
493+
</style>

src/components/home_sidebar_items/ComponentTab/CreateComponent.vue

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ Description:
5353
import Icons from '../Icons'
5454
import ParentMultiselect from '../ParentMultiselect.vue'
5555
import { mapState, mapActions } from 'vuex'
56-
57-
5856
export default {
5957
name: 'HomeSidebar',
6058
components: {
@@ -88,7 +86,6 @@ export default {
8886
'addNestedNoActive',
8987
'editComponentName',
9088
]),
91-
9289
createComponent () {
9390
if (!this.componentNameInputValue.replace(/[^a-z0-9-_.]/gi, '')) {
9491
event.preventDefault()
@@ -113,18 +110,14 @@ export default {
113110
parent: {},
114111
isActive: false
115112
}
116-
117-
console.log(component)
118113
if (!this.componentMap[component.componentName]) {
119-
console.log('line119')
120-
this.registerComponent(component)
121-
console.log(this.setActiveComponent)
122-
// this.setActiveComponent(component.componentName)
114+
this.registerComponent(component);
115+
console.log(this.$store.state)
116+
this.setActiveComponent(component.componentName)
123117
}
124118
},
125119
},
126120
}
127-
128121
</script>
129122

130123
<style lang="scss" scoped>
@@ -136,7 +129,6 @@ export default {
136129
margin: 0.75rem;
137130
width: 90%;
138131
}
139-
140132
.inner-div {
141133
display: flex;
142134
flex-direction: column;

src/store/mutations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ const mutations = {
479479
const {
480480
componentName, htmlList, children, parent, isActive, actions, props
481481
} = payload
482-
const s = payload.state
482+
const s = payload.state;
483483
state.componentMap = {
484484
...state.componentMap,
485485
[componentName]: {

0 commit comments

Comments
 (0)