Skip to content

Instantly share code, notes, and snippets.

@waelio
Created March 24, 2021 22:44
Show Gist options
  • Save waelio/f312192936cab4a0ebcc93fa1879ea1e to your computer and use it in GitHub Desktop.
Save waelio/f312192936cab4a0ebcc93fa1879ea1e to your computer and use it in GitHub Desktop.
Generate REST Vue - Vuex model
const { snakeToCamel, camelToSnake } = require ("waelio-utils");
function regModule (mod ) {
if(!mod) return null
const modelName = mod.name
const UPPER = modelName.toUpperCase()
const SNAKE = camelToSnake(modelName)
const UPPER_SNAKE = SNAKE.toUpperCase()
const CAMEL = modelName.charAt(0).toUpperCase() + modelName.slice(1)
const isNameSpaced = mod.namespaced || false
try {
return {
name: CAMEL,
namespaced: isNameSpaced,
state: {
[CAMEL]: [],
[`${CAMEL}.active`]: {},
[`${CAMEL}.raw`]: null,
[`${CAMEL}.errors`]: null,
[`${CAMEL}.started`]: false,
[`${CAMEL}.pending`]: false,
[`${CAMEL}.completed`]: false,
},
mutations: {
[`SET_${UPPER_SNAKE}`](state, payload) {
state[`${CAMEL}`] = payload;
state[`${CAMEL}.raw`] = payload;
},
[`SET_${UPPER_SNAKE}_ACTIVE`](state, payload) {
Object.assign(state.[`${CAMEL}.active`], payload);
state[`${CAMEL}.raw`] = payload
},
[`SET_${UPPER_SNAKE}_RAW`](state, payload) {
state[`${CAMEL}.raw`] = payload
},
[`SET_${UPPER_SNAKE}_ERROR`](state, payload) {
state[`${CAMEL}.errors`] = payload
},
[`SET_${UPPER_SNAKE}_STARTED`](state, payload=true) {
state[`${CAMEL}.started`] = payload
state[`${CAMEL}.pending`] = !payload
state[`${CAMEL}.completed`] = !payload
},
[`SET_${UPPER_SNAKE}_PENDING`](state, payload = true) {
state[`${CAMEL}.started`] = !payload
state[`${CAMEL}.pending`] = payload
state[`${CAMEL}.completed`] = !payload
},
[`SET_${UPPER_SNAKE}_COMPLETED`](state, payload = true) {
state[`${CAMEL}.started`] = !payload
state[`${CAMEL}.pending`] = !payload
state[`${CAMEL}.completed`] = payload
}
},
actions:{
async [`find${CAMEL}`]({ state, commit }, query) {
commit([`SET_${UPPER_SNAKE}_STARTED`],true)
const result = await api[mod.endpoint].find({ query })
commit([`SET_${UPPER_SNAKE}_PENDING`],true)
result
.then(success => {
commit([`SET_${UPPER_SNAKE}_COMPLETED`],true)
commit( [`SET_${UPPER_SNAKE}`], success);
})
.catch(error => {
console.log(error);
commit([`SET_${UPPER_SNAKE}_ERROR`], error);
});
},
async [`findAll${CAMEL}`]({ state, commit }) {
commit([`SET_${UPPER_SNAKE}_STARTED`],true)
const result = await api[mod.endpoint].find({ query })
commit([`SET_${UPPER_SNAKE}_PENDING`],true)
result
.then(success => {
commit([`SET_${UPPER_SNAKE}_COMPLETED`])
console.log('%cMyProject%cline:77%cresult', 'color:#fff;background:#ee6f57;padding:3px;border-radius:2px', 'color:#fff;background:#1f3c88;padding:3px;border-radius:2px', 'color:#fff;background:rgb(222, 125, 44);padding:3px;border-radius:2px', success)
commit( [`SET_${UPPER_SNAKE}`], success);
})
.catch(error => {
console.log(error);
commit([`SET_${UPPER_SNAKE}_ERROR`], error);
});
},
async [`get${CAMEL}`]({ state, commit }, id){
commit([`SET_${UPPER_SNAKE}_STARTED`])
const result = await api[mod.endpoint].get(id)
commit([`SET_${UPPER_SNAKE}_PENDING`])
result
.then(success => {
commit([`SET_${UPPER_SNAKE}_COMPLETED`])
commit( [`SET_${UPPER_SNAKE}_ACTIVE`], success);
})
.catch(error => {
console.log(error);
commit([`SET_${UPPER_SNAKE}_ERROR`], error);
});
},
async [`create${CAMEL}`]({ state, commit }, payload){
commit([`SET_${UPPER_SNAKE}_STARTED`])
const result = await api[mod.endpoint].create(payload)
commit([`SET_${UPPER_SNAKE}_PENDING`])
result
.then(success => {
commit([`SET_${UPPER_SNAKE}_COMPLETED`])
commit( [`SET_${UPPER_SNAKE}_ACTIVE`], success);
})
.catch(error => {
console.log(error);
commit([`SET_${UPPER_SNAKE}_ERROR`], error);
});
},
async [`update${CAMEL}`]({ state, commit }, id, payload){
payload = {...payload, _id:id}
commit([`SET_${UPPER_SNAKE}_STARTED`])
const result = await api[mod.endpoint].update(payload)
commit([`SET_${UPPER_SNAKE}_PENDING`])
result
.then(success => {
commit([`SET_${UPPER_SNAKE}_COMPLETED`])
commit( [`SET_${UPPER_SNAKE}_ACTIVE`], success);
})
.catch(error => {
console.log(error);
commit([`SET_${UPPER_SNAKE}_ERROR`], error);
});
},
async [`patch${CAMEL}`]({ state, commit }, id, payload){
payload = {...payload, _id:id}
commit([`SET_${UPPER_SNAKE}_STARTED`])
const result = await api[mod.endpoint].create(payload)
commit([`SET_${UPPER_SNAKE}_PENDING`])
result
.then(success => {
commit([`SET_${UPPER_SNAKE}_COMPLETED`])
commit( [`SET_${UPPER_SNAKE}_ACTIVE`], success);
})
.catch(error => {
console.log(error);
commit([`SET_${UPPER_SNAKE}_ERROR`], error);
});
},
async [`delete${CAMEL}`]({ state, commit }, id){
commit([`SET_${UPPER_SNAKE}_STARTED`])
const result = await api[mod.endpoint].delete({_id: id})
commit([`SET_${UPPER_SNAKE}_PENDING`])
result
.then(success => {
commit([`SET_${UPPER_SNAKE}_COMPLETED`])
commit( [`SET_${UPPER_SNAKE}_ACTIVE`], success);
})
.catch(error => {
console.log(error);
commit([`SET_${UPPER_SNAKE}_ERROR`], error);
});
}
},
getters:{
[CAMEL]: state=>state[CAMEL],
[`${CAMEL}.active`]: state=>state[`${CAMEL}.active`],
[`${CAMEL}.raw`]: state=>state[`${CAMEL}.raw`],
[`${CAMEL}.status`] (state) {
return {
started: state[`${CAMEL}.started`],
pending: state[`${CAMEL}.pending`],
completed: state[`${CAMEL}.completed`]
}
}
}
}
} catch (error) {
store.$note.error(error)
}
}
export default regModule
export {regModule}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment