Created
February 22, 2019 12:40
-
-
Save peerhenry/efdbef99eea5797e980984d3f948dd18 to your computer and use it in GitHub Desktop.
infi vuex blogpost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function fetchCustomer(context, payload) { | |
| context.commit('setFetchCustomerPending', true) | |
| context.commit('setFetchCustomerError', undefined) | |
| const url = fetchCustomerUrl + '?ID=' + payload.customerId | |
| axios.get(url).then( | |
| (response) => { | |
| context.commit('setFetchCustomerPending', false) | |
| context.commit('setCustomer', response.data) | |
| }, | |
| (error) => { | |
| context.commit('setFetchCustomerPending', false) | |
| context.commit('setFetchCustomerError', error) | |
| } | |
| ) | |
| } | |
| function updateCustomer(context, payload) { | |
| context.commit('setUpdateCustomerPending', true) | |
| context.commit('setUpdateCustomerError', undefined) | |
| axios.post(updateCustomerUrl, payload).then( | |
| (response) => { | |
| context.commit('setUpdateCustomerPending', false) | |
| }, | |
| (error) => { | |
| context.commit('setUpdateCustomerPending', false) | |
| context.commit('setUpdateCustomerError', error) | |
| } | |
| ) | |
| } | |
| const customerModule = { | |
| state: { customer: undefined }, | |
| getters: { | |
| customer(state) { return state.customer } | |
| }, | |
| mutations: { | |
| setCustomer(state, customer) { state.customer = customer } | |
| }, | |
| actions: {} | |
| } | |
| setupApiCall(customerModule, 'fetchCustomer', fetchCustomer) | |
| setupApiCall(customerModule, 'updateCustomer', updateCustomer) | |
| export customerModule |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function setupApiCall(vuexModule, name, action) { | |
| const pendingKey = name + 'Pending' | |
| const errorKey = name + 'Error' | |
| const capitalizedName = name.charAt(0).toUpperCase() + name.slice(1) | |
| const pendingMutationKey = 'set' + capitalizedName + 'Pending' | |
| const errorMutationKey = 'set' + capitalizedName + 'Error' | |
| vuexModule.state[pendingKey] = false | |
| vuexModule.state[errorKey] = undefined | |
| vuexModule.getters[pendingKey] = (state) => state[pendingKey] | |
| vuexModule.getters[errorKey] = (state) => state[errorKey] | |
| vuexModule.mutations[pendingMutationKey] = (state, pending) => state[pendingKey] = pending | |
| vuexModule.mutations[errorMutationKey] = (state, error) => state[errorKey] = error | |
| vuexModule.actions[name] = action | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment