Skip to content

Commit b347cb0

Browse files
committed
Updated CodeSnippet to properly display changes in state and action. Refactored the CodeSnippet generation to use the more proper componentmap[activecomponent] rather than activecomponentobj which has some issues. Added a mount and watch with a DRY method to invoke the CodeSnippet generation on any change to the state that needs to be reflected in the snippet.
1 parent a16e548 commit b347cb0

File tree

2 files changed

+40
-55
lines changed

2 files changed

+40
-55
lines changed

src/components/dashboard_items/CodeSnippet.vue

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import "prismjs/themes/prism-tomorrow.css"; // import syntax highlighting styles
3535
export default {
3636
data() {
3737
return {
38-
// code: `Your component boilerplate will be displayed here.`,
38+
code: `Your component boilerplate will be displayed here.`,
3939
lineNumbers: true,
4040
height: null,
4141
};
@@ -46,18 +46,18 @@ export default {
4646
computed: {
4747
// needs access to current component aka activeComponent
4848
...mapState(["componentMap", "activeComponent", "activeComponentObj", "exportAsTypescript"]),
49-
code: function () {
50-
let computedCode = "Your component boilerplate will be displayed here.";
51-
if (this.activeComponent) {
52-
computedCode = this.createCodeSnippet(
49+
},
50+
methods: {
51+
snippetInvoke(){
52+
if (this.activeComponent !== ''){
53+
this.code = this.createCodeSnippet(
5354
this.componentMap[this.activeComponent].componentName,
5455
this.componentMap[this.activeComponent].children
55-
);
56+
)
57+
} else {
58+
this.code = 'Your component boilerplate will be displayed here.'
5659
}
57-
return computedCode;
5860
},
59-
},
60-
methods: {
6161
highlighter(myCode) {
6262
return highlight(myCode, languages.js);
6363
},
@@ -161,16 +161,16 @@ export default {
161161
// add import mapstate and mapactions if they exist
162162
let imports = "";
163163
if (
164-
this.activeComponentObj.actions.length ||
165-
this.activeComponentObj.state.length
164+
this.componentMap[this.activeComponent].actions.length ||
165+
this.componentMap[this.activeComponent].state.length
166166
) {
167167
imports += "import { ";
168168
if (
169-
this.activeComponentObj.actions.length &&
170-
this.activeComponentObj.state.length
169+
this.componentMap[this.activeComponent].actions.length &&
170+
this.componentMap[this.activeComponent].state.length
171171
) {
172172
imports += "mapState, mapActions";
173-
} else if (this.activeComponentObj.state.length) imports += "mapState";
173+
} else if (this.componentMap[this.activeComponent].state.length) imports += "mapState";
174174
else imports += "mapActions";
175175
imports += ' } from "vuex";\n';
176176
}
@@ -193,9 +193,9 @@ export default {
193193
194194
// if true add data section and populate with props
195195
let data = "";
196-
if (this.activeComponentObj.props.length) {
196+
if (this.componentMap[this.activeComponent].props.length) {
197197
data += " data () {\n return {";
198-
this.activeComponentObj.props.forEach((prop) => {
198+
this.componentMap[this.activeComponent].props.forEach((prop) => {
199199
data += `\n ${prop}: "PLACEHOLDER FOR VALUE",`;
200200
});
201201
data += "\n";
@@ -205,10 +205,10 @@ export default {
205205
206206
// if true add computed section and populate with state
207207
let computed = "";
208-
if (this.activeComponentObj.state.length) {
208+
if (this.componentMap[this.activeComponent].state.length) {
209209
computed += " computed: {";
210210
computed += "\n ...mapState([";
211-
this.activeComponentObj.state.forEach((state) => {
211+
this.componentMap[this.activeComponent].state.forEach((state) => {
212212
computed += `\n "${state}",`;
213213
});
214214
computed += "\n ]),\n";
@@ -217,10 +217,10 @@ export default {
217217
218218
// if true add methods section and populate with actions
219219
let methods = "";
220-
if (this.activeComponentObj.actions.length) {
220+
if (this.componentMap[this.activeComponent].actions.length) {
221221
methods += " methods: {";
222222
methods += "\n ...mapActions([";
223-
this.activeComponentObj.actions.forEach((action) => {
223+
this.componentMap[this.activeComponent].actions.forEach((action) => {
224224
methods += `\n "${action}",`;
225225
});
226226
methods += "\n ]),\n";
@@ -260,29 +260,25 @@ export default {
260260
},
261261
},
262262
watch: {
263-
// // watches activeComponentObj for changes to make it reactive upon mutation
264-
// activeComponentObj: {
265-
// handler () {
266-
// // console.log(this.activeComponentObj.children)
267-
// this.code = this.createCodeSnippet(
268-
// this.activeComponentObj.componentName,
269-
// this.activeComponentObj.children
270-
// )
271-
// }
272-
// },
273-
// // // // watches componentMap for changes to make it reactive upon mutation
274-
// componentMap: {
275-
// handler () {
276-
// this.code = this.createCodeSnippet(
277-
// this.activeComponentObj.componentName,
278-
// this.activeComponentObj.children
279-
// )
280-
// }
281-
// }
263+
// watches activeComponentObj for changes to make it reactive upon mutation
264+
// // // watches componentMap for changes to make it reactive upon mutation
265+
activeComponent: {
266+
handler () {
267+
this.snippetInvoke();
268+
},
269+
deep: true
270+
},
271+
componentMap: {
272+
handler () {
273+
this.snippetInvoke();
274+
},
275+
deep: true
276+
}
282277
},
283278
mounted() {
284279
// https://vuejs.org/v2/api/#Vue-nextTick
285280
// kinda like a promise, used for the window resize
281+
this.snippetInvoke()
286282
this.$nextTick(() => {
287283
window.addEventListener("resize", this.getWindowHeight);
288284

src/store/mutations.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ const mutations = {
238238
temp.state = newArray;
239239
state.activeComponentObj = null;
240240
state.activeComponentObj = temp;
241+
console.log(state.componentMap)
241242
},
242243

243244
[types.DELETE_USER_STATE]: (state, payload) => {
@@ -246,14 +247,8 @@ const mutations = {
246247
// first don't go through if component is App or Homeview
247248
if (component === "App" || component === "HomeView") continue;
248249
// splice out if there is a match
249-
const newState = state.componentMap[component].state;
250-
const index = newState.indexOf(payload);
251-
if (index > -1) {
252-
newState.splice(index, 1);
253-
state.componentMap[component].state = newState;
254-
} else {
255-
continue;
256-
}
250+
state.componentMap[component].state = state.componentMap[component].state.filter(
251+
(action) => action !== payload)
257252
}
258253
// remove from userState
259254
let index = state.userState.indexOf(payload);
@@ -265,14 +260,8 @@ const mutations = {
265260
// first don't go through if component is App or Homeview
266261
if (component === "App" || component === "HomeView") continue;
267262
// splice out if there is a match
268-
const newActions = state.componentMap[component].actions;
269-
const index = newActions.indexOf(payload);
270-
if (index > -1) {
271-
newActions.splice(index, 1);
272-
state.componentMap[component].actions = newActions;
273-
} else {
274-
continue;
275-
}
263+
state.componentMap[component].actions = state.componentMap[component].actions.filter(
264+
(action) => action !== payload)
276265
}
277266
let index = state.userActions.indexOf(payload);
278267
state.userActions.splice(index, 1);

0 commit comments

Comments
 (0)