add vuex stores

This commit is contained in:
Jonathan Shook 2020-08-25 15:31:47 -05:00
parent de3c750b5d
commit 630cf74888
3 changed files with 321 additions and 31 deletions

View File

@ -1,11 +1,83 @@
// https://www.mikestreety.co.uk/blog/vue-js-using-localstorage-with-the-vuex-store
/**
categories: [
{
name,
title,
topics,
weight,
path
summary_topic: {
name,
path,
basename,
categories,
weight,
title,
content: mdMeta.body
},
topics: [
{
name,
path,
basename,
categories,
weight,
title,
content: mdMeta.body
}
]
}
]
* @returns {{isDrawerOpen: boolean, isMenuLocked: boolean, active_category: null, active_topic: null, categories: []}}
*/
export const state = () => ({
categories: [],
active_category: null,
active_topic: null,
isDrawerOpen: true,
isDrawerPinned: false
isMenuLocked: false
});
export const getters = {
getCategories: (state, getters) => {
return state.categories;
},
getActiveCategory: (state, getters) => {
return state.active_category;
},
getActiveTopic: (state, getters) => {
return state.active_topic;
},
getIsMenuLocked: (state, getters) => {
return state.isMenuLocked;
},
getIsDrawerOpen: (state, getters) => {
return state.isDrawerOpen;
},
getActiveMarkdownContent: (state, getters) => {
if (state.active_category===null) {
throw "unable to load active markdown for undefined category";
}
if (state.active_topic===null) {
throw "uanble to load active markdown for undefined topic";
}
return state.active_topic.content;
}
}
export const mutations = {
setActiveCategory(state, active_category) {
state.active_category = active_category;
},
setActiveTopic(state, active_topic) {
state.active_topic = active_topic;
},
setCategories(state, categories) {
state.categories = categories;
},
// initializeStore(state) {
// if(localStorage.getItem('store')) {
// this.replaceState(
@ -14,18 +86,153 @@ export const mutations = {
// }
// },
toggleDrawerState(state, newDrawerState) {
if (state.isDrawerPinned) {
if (state.isMenuLocked) {
return;
}
state.isDrawerOpen=!state.isDrawerOpen;
state.isDrawerOpen = !state.isDrawerOpen;
},
setDrawer(state, newDrawerState) {
if (state.isDrawerPinned) {
setIsDrawerOpen(state, newDrawerState) {
if (state.isMenuLocked) {
return;
}
state.isDrawerOpen=newDrawerState;
state.isDrawerOpen = newDrawerState;
},
setMenuLock(state, newLockState) {
state.isDrawerPinned=newLockState;
setIsMenuLocked(state, newLockState) {
state.isMenuLocked = newLockState;
}
};
};
export const actions = {
async setActiveCategory({commit, state, dispatch}, active_category) {
await commit("setActiveCategory", active_category)
},
async setActiveTopic({commit, state, dispatch}, active_topic) {
await commit("setActiveTopic", active_topic);
},
async setIsMenuLocked({commit, state, dispatch}, isMenuLocked) {
await commit("setIsMenuLocked", isMenuLocked);
},
async setIsDrawerOpen({commit, state, dispatch}, isDrawerOpen) {
await commit("setIsDrawerOpen", isDrawerOpen)
},
async setCategories({commit, state, dispatch}, categories) {
await commit("setCategories", categories)
},
async loadCategories({commit, state, dispatch}) {
if (state.categories === null || state.categories.length === 0) {
let fm = require('front-matter');
const category_data = await this.$axios.get("/docs/markdown.csv")
.then(manifest => {
// console.log("typeof(manifest):" + typeof (manifest))
// console.log("manifest:" + JSON.stringify(manifest, null, 2))
return manifest.data.split("\n").filter(x => {
return x!==null && x.length>0
})
})
.then(async lines => {
let val = await Promise.all(lines.map(line => {
let url = "/docs" + "/" + line;
// console.log("url:"+url)
return this.$axios.get("/docs/" + line)
.then(res => {
// console.log("typeof(res):" + typeof(res))
return {
path: line,
content: res.data
};
})
})).then(r => {
// console.log("r:" + JSON.stringify(r,null,2))
return r
});
return val;
// let mapof =Object.fromEntries(val)
// console.log("mapof:" + JSON.stringify(mapof, null, 2))
// return mapof;
})
.then(fetched => {
return fetched.map(entry => {
let [, name] = entry.path.match(/(.+)\.md$/);
let basename = entry.path.split("/").find(x => x.includes(".md"))
let categories = entry.path.split("/").filter(x => !x.includes("."))
let mdMeta = fm(entry.content);
let weight = ((mdMeta.attributes.weight) ? mdMeta.attributes.weight : 0)
let title = ((mdMeta.attributes.title) ? mdMeta.attributes.title : basename)
let path = "/docs/" + entry.path
// console.log("path:" + entry.path)
return {
name,
path,
basename,
categories,
weight,
title,
content: mdMeta.body
}
})
}
)
.then(alltopics => {
// console.log("input:" + JSON.stringify(input, null, 2))
let categorySet = new Set();
alltopics.forEach(x => {
x.categories.forEach(y => {
categorySet.add(y);
})
})
return Array.from(categorySet).map(name => {
// console.log("category:" + JSON.stringify(category, null, 2))
let topics = alltopics.filter(x => x.categories.toString() === name);
// console.log("docs_in_category = " + JSON.stringify(docs_in_category, null, 2))
let summary_topic = topics.find(x => x.path.endsWith('index.md'));
let weight = summary_topic ? summary_topic.weight : 0;
let title = summary_topic ? (summary_topic.title ? summary_topic.title : name) : name;
let content = summary_topic ? (summary_topic.content ? summary_topic.content: "") : "";
topics = topics.filter(x => !x.path.endsWith('index.md'));
topics.sort((a, b) => a.weight - b.weight);
let path = "/docs/" + name;
let entry = {
path,
name,
title,
weight,
content,
topics,
summary_topic
}
// console.log("entry=> " + entry);
return entry;
}).sort((c1, c2) => c1.weight - c2.weight);
})
.catch((e) => {
console.error("error in loadCategories:" + e);
})
await dispatch("setCategories", category_data)
}
if (state.active_category===null) {
commit("setActiveCategory", state.categories[0]);
}
if (state.active_topic===null) {
commit("setActiveTopic", state.active_category.topics[0]);
}
// console.log("typeof(result):" + typeof (docinfo))
// console.log("result:" + JSON.stringify(docinfo, null, 2))
}
}

View File

@ -0,0 +1,83 @@
// https://www.mikestreety.co.uk/blog/vue-js-using-localstorage-with-the-vuex-store
import {mapGetters} from "vuex";
export const state = () => ({
workloads: [],
templates: {},
searchin: ''
});
export const getters = {
getWorkloads: (state, getters) => {
return state.workloads;
},
getSearchin: (state, getters) => {
return state.searchin;
},
getTemplates: (state, getters) => {
return state.templates;
}
}
export const mutations = {
setWorkloads(state, workloads) {
state.workloads = workloads;
},
setSearchin(state, searchin) {
state.searchin = searchin;
},
setTemplates(state, templates) {
state.templates = templates;
}
};
export const actions = {
async setWorkloads({commit, state, dispatch}, val) {
console.log("committing setWorkloads:" + JSON.stringify(val));
commit('setWorkloads', val);
},
async setTemplates({commit, state, dispatch}, val) {
console.log("commiting setTemplates:" + JSON.stringify(val));
commit("setTemplates", val);
},
async setSearchin({commit, state, dispatch}, val) {
console.log("committing setsearchin:" + JSON.stringify(val));
commit('setSearchin', val);
},
fetchWorkloads({commit, state, dispatch}, params) {
let reason = params.reason;
let searchin = params.searchin;
if (reason === undefined || searchin === undefined) {
throw "Unable to fetch workloads without a reason or searchin: " + JSON.stringify(params);
}
console.log("fetching workloads because '" + reason + "'")
commit("setTemplates", undefined);
this.$axios.$get("/workloads/?searchin=" + searchin)
.then(res => {
console.log("axios/vuex workloads async get:" + JSON.stringify(res));
commit("setWorkloads", res);
})
.catch((e) => {
console.error("axios/nuxt workloads async error:", e);
})
},
async fetchTemplates({commit, state, dispatch}, params) {
let reason = params.reason;
let workload = params.workload;
let searchin = params.searchin;
if (reason === undefined || workload === undefined || searchin === undefined) {
throw "Unable to fetch templates for workload without a {reason,workload,searchin}: " + JSON.stringify(params);
}
console.log("fetching templates for '" + workload + "' because '" + reason + "'")
this.$axios.$get("/workloads/parameters?workloadName=" + workload + "&" + searchin)
.then(res => {
console.log("axios/vuex templates async get:" + JSON.stringify(res));
dispatch("setTemplates", res);
})
.catch((e) => {
console.error("axios/nuxt templates async error:", e);
})
}
};

View File

@ -24,20 +24,8 @@ export const mutations = {
setWorkspaces(state, workspaces) {
state.workspaces = workspaces;
}
// initializeStore(state) {
// if(localStorage.getItem('store')) {
// this.replaceState(
// Object.assign(state,JSON.parse(localStorage.getItem('store')))
// );
// }
// },
};
/**
* The base url is already set for the API by this point
* @type {{getWorkspaces({commit: *}): void, return: ([]|[{name: string}]|[{name: string}]|default.computed.workspaces)}}
*/
export const actions = {
async setWorkspace({commit, state, dispatch}, val) {
console.log("committing setWorkspace:" + JSON.stringify(val));
@ -59,6 +47,22 @@ export const actions = {
console.error("axios/nuxt workspaces async error:", e);
})
},
async putFile({commit, state, dispatch}, params) {
let to_workspace = params.workspace;
let to_filename = params.filename;
let to_content = params.content;
if (!to_workspace || !to_filename || !to_content) {
throw("Unable to save file to workspace without params having workspace, filename, content");
}
const result = await this.$axios.$post("/workspaces/" + to_workspace + "/" + to_filename, to_content)
.then(res => {
console.log("axios/vuex workspace put:" + JSON.stringify(res));
return res;
})
.catch((e) => {
console.error("axios/vuex workspace put:", e)
});
},
async activateWorkspace({commit, state, dispatch}, workspace) {
const fresh_workspace = await this.$axios.$get("/workspaces/" + workspace)
.then(res => {
@ -68,11 +72,11 @@ export const actions = {
.catch((e) => {
console.error("axios/nuxt getWorkspace async error:", e)
})
await dispatch('initWorkspaces',"workspace '" + workspace + "' added");
await dispatch('initWorkspaces', "workspace '" + workspace + "' added");
// await dispatch.initWorkspaces({commit, state, dispatch}, "workspace '" + workspace + "' added")
// await this.$store.dispatch("workspaces/initWorkspaces", "workspace '" + workspace + "' added")
// await this.initWorkspaces({commit}, "workspace added");
commit('setWorkspace', fresh_workspace)
commit('setWorkspace', fresh_workspace.name)
return workspace;
},
async purgeWorkspace({commit, state, dispatch}, workspace) {
@ -84,15 +88,11 @@ export const actions = {
.catch((e) => {
console.error("axios/nuxt purgeWorkspace error:", e)
})
await dispatch('initWorkspaces',"workspace '" + workspace + "' purged");
// dispatch.dispatch('initWorkspaces',"workspace '" + workspace + "' purged");
// dispatch.initWorkspaces({commit, state, dispatch}, "workspace '" + workspace + "' purged")
const found = this.state.workspaces.workspaces.find(w => {w.name === workspace});
const found = this.state.workspaces.workspaces.find(w => w.name === workspace);
if (!found) {
console.log("setting active workspace to 'default' since the previous workspace '" + workspace + "' is not found")
await dispatch.activateWorkspace({commit, state, dispatch}, "default");
await dispatch('activateWorkspace', "default");
}
await dispatch('initWorkspaces', "workspace '" + workspace + "' purged");
}
};