|
| 1 | +// We need Vue to ensure proper updates in mutations |
| 2 | +import Vue from 'vue' |
| 3 | + |
| 4 | +const camelize = function (snakeText, capitalizeFirstLetter = true) { |
| 5 | + let regexp = /(_\w)/g |
| 6 | + if (capitalizeFirstLetter) { |
| 7 | + regexp = /(^\w|_\w)/g |
| 8 | + } |
| 9 | + const out = snakeText.replace(regexp, (match) => { |
| 10 | + if (match.length > 1) { |
| 11 | + return match[1].toUpperCase() |
| 12 | + } else { |
| 13 | + return match.toUpperCase() |
| 14 | + } |
| 15 | + }) |
| 16 | + return out |
| 17 | +} |
| 18 | + |
| 19 | +export default function (endpoint, singular, plural, editable = true, destroyable = true) { |
| 20 | + const lowCamelSingular = camelize(singular, false) |
| 21 | + const lowCamelPlural = camelize(plural, false) |
| 22 | + const camelSingular = camelize(singular) |
| 23 | + const camelPlural = camelize(plural) |
| 24 | + |
| 25 | + // Empty module |
| 26 | + const module = {state: {}, mutations: {}, actions: {}, getters: {}} |
| 27 | + |
| 28 | + // Mutators: |
| 29 | + module.mutations[`set_${singular}`] = (state, entity) => { Vue.set(state, entity.id, entity) } |
| 30 | + module.mutations[`remove_${singular}`] = (state, id) => {Vue.delete(state, id)} |
| 31 | + |
| 32 | + // Actions: |
| 33 | + module.actions[`load${camelPlural}`] = ({commit}) => { |
| 34 | + api.get(endpoint) |
| 35 | + .then((data) => { |
| 36 | + for (let i = 0; i < data.length; i++) { |
| 37 | + commit(`set_${singular}`, data[i]) |
| 38 | + } |
| 39 | + }) |
| 40 | + .catch((error) => {console.log(`ERROR in load${camelPlural}:`, error)}) |
| 41 | + } |
| 42 | + module.actions[`load${camelSingular}`] = ({commit}, id) => { |
| 43 | + api.get(`${endpoint}/${id}`) |
| 44 | + .then((data) => {commit(`set_${singular}`, data)}) |
| 45 | + .catch((error) => {console.log(`ERROR in load${camelSingular}:`, error)}) |
| 46 | + } |
| 47 | + if (editable) { |
| 48 | + module.actions[`create${camelSingular}`] = ({commit}, payload) => { |
| 49 | + api.post(endpoint, payload) |
| 50 | + .then((data) => { |
| 51 | + // Let's assume "data" is the new article, sent back by the API |
| 52 | + // We don't want to store the user input in our store :) |
| 53 | + commit(`set_${singular}`, data) |
| 54 | + }) |
| 55 | + .catch((error) => {console.log(`ERROR in create${camelSingular}:`, error)}) |
| 56 | + } |
| 57 | + module.actions[`update${camelSingular}`] = ({commit}, payload) => { |
| 58 | + api.patch(`${endpoint}/${payload.id}`, payload) |
| 59 | + .then((data) => { |
| 60 | + // Let's assume "data" is the updated article |
| 61 | + commit(`set_${singular}`, data) |
| 62 | + }) |
| 63 | + .catch((error) => {console.log(`ERROR in update${camelSingular}:`, error)}) |
| 64 | + } |
| 65 | + } |
| 66 | + if (destroyable) { |
| 67 | + module.actions[`destroy${camelSingular}`] = ({commit}, id) => { |
| 68 | + api.delete(`${endpoint}/${id}`) |
| 69 | + .then(() => { commit(`remove_${singular}`, id) }) |
| 70 | + .catch((error) => {console.log('ERROR in DESTROY_ARTICLE:', error)}) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Getters |
| 75 | + module.getters[lowCamelPlural] = state => state |
| 76 | + module.getters[lowCamelSingular] = state => id => state[id] || undefined |
| 77 | + |
| 78 | + return module |
| 79 | +} |
0 commit comments