Skip to content

Instantly share code, notes, and snippets.

@syntaxlexx
Created October 27, 2018 16:34
Show Gist options
  • Save syntaxlexx/42e124c8c76783fc1d7d7afdf6969537 to your computer and use it in GitHub Desktop.
Save syntaxlexx/42e124c8c76783fc1d7d7afdf6969537 to your computer and use it in GitHub Desktop.
StoreGenerator [Vuex]
import { paths } from './paths';
import { visa } from '../libs/Customs';
import {StoreActionUrl} from "./StoreActionUrl";
import { apiEndpoint } from '@app/libs/Util';
export default class StoreGenerator
{
constructor()
{
this.state = {};
this.actions = {};
this.getters = {};
this.mutations = {};
this.setState();
this.setActions();
this.setGetters();
this.setMutations();
}
/*
* Set the state variables
*/
setState()
{
paths.forEach( path => {
if(path.hasOwnProperty('params'))
{
path.params.forEach(param => {
let stateProperty = this.lowerFirst(this.getName(path, param));
this.state[stateProperty] = [];
});
}
else
{
let stateProperty = this.lowerFirst(this.getName(path));
this.state[stateProperty] = [];
}
})
}
/*
* Return the state variables
*/
getState()
{
return this.state;
}
/*
* Set the getters
*/
setGetters()
{
paths.forEach( path => {
let functionName = 'get' + this.getName(path);
this.getters[functionName] = function(state){
let name = this.getName(path);
let stateProperty = this.lowerFirst(name);
return state[stateProperty];
}.bind(this);
})
}
/*
* Get the getters
*/
getGetters()
{
return this.getters;
}
/*
* Set the actions
*/
setActions()
{
var _this = this;
paths.forEach((path) => {
let baseUrl = apiEndpoint + path.url;
let name = 'set' + this.getName(path);
this.actions[name] = function(context, options = {}){
let actionUrl = new StoreActionUrl(baseUrl, options).getUrl(); // will be in camel-case
// convert to kebab-case
actionUrl = _this.camelToKebab(actionUrl);
axios.get(actionUrl, visa()).then(({data}) => {
context.commit(name, data);
});
};
})
}
/*
* Get actions
*/
getActions()
{
return this.actions;
}
/*
* Set Mutations
*/
setMutations()
{
paths.forEach((path) => {
let name = 'set' + this.getName(path);
this.mutations[name] = (state, value) => {
let name = this.getName(path);
let stateProperty = this.lowerFirst(name);
state[stateProperty] = value;
};
})
}
/*
* Get mutations
*/
getMutations()
{
return this.mutations;
}
/*
* Get the last segment of the url
*/
getUrlSegment(url)
{
let contents = url.split("/");
return contents[contents.length - 1];
}
/*
* Make the first letter uppercase
*/
ucwords(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
/*
* Make the first letter lowercase
*/
lowerFirst(string)
{
return string.charAt(0).toLowerCase() + string.slice(1);
}
/*
* Get the name of the action and mutation given a path
*/
getName(path, prefix='')
{
return this.ucwords(this.getUrlSegment(path.url)) + this.ucwords(prefix);
}
/**
* Convert Strings from camelCase to kebab-case
* @returns {string}
* @param input
*/
camelToKebab(str) {
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}
/**
* Convert Strings from kebab-case to camelCase
* @returns {string}
* @param input
*/
kebabToCamel(str) {
return str.replace(/-([a-z])/g, function (g) {
return g[1].toUpperCase();
});
}
}
@syntaxlexx
Copy link
Author

Finally found a shortcut to convert the StoreGenerator's action urls to be kebab, whilst maintaining the store's states in camelCase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment