Created
February 22, 2019 12:38
-
-
Save peerhenry/069981fc2f71faa0bec2ed4f62b67210 to your computer and use it in GitHub Desktop.
infi vuex boilerplate 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
| const initialState = { | |
| customer: undefined, | |
| fetchCustomerPending: false, | |
| fetchCustomerError: undefined, | |
| updateCustomerPending: false, | |
| updateCustomerError: undefined, | |
| } | |
| const getters = { | |
| customer(state) { | |
| return state.customer | |
| }, | |
| fetchCustomerPending(state) { | |
| return state.fetchCustomerPending | |
| }, | |
| fetchCustomerError(state) { | |
| return state.fetchCustomerError | |
| }, | |
| updateCustomerPending(state) { | |
| return state.updateCustomerPending | |
| }, | |
| updateCustomerError(state) { | |
| return state.updateCustomerError | |
| } | |
| } | |
| const mutations = { | |
| setCustomer(state, customer) { | |
| state.customer = customer | |
| }, | |
| setFetchCustomerPending(state, pending) { | |
| state.fetchCustomerPending = pending | |
| }, | |
| setFetchCustomerError(state, error) { | |
| state.fetchCustomerError = error | |
| }, | |
| setUpdateCustomerPending(state, pending) { | |
| state.updateCustomerPending = pending | |
| }, | |
| setUpdateCustomerError(state, error) { | |
| state.updateCustomerError = error | |
| }, | |
| } | |
| const actions = { | |
| 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) | |
| } | |
| ) | |
| }, | |
| updateCustomer(context, payload) { | |
| context.commit('setUpdateCustomerPending', true) | |
| context.commit('setUpdateCustomerError', undefined) | |
| axios.post(updateCustomerUrl, payload).then( | |
| (response) => { | |
| context.commit('setUpdateCustomerPending', true) | |
| }, | |
| (error) => { | |
| context.commit('setUpdateCustomerPending', false) | |
| context.commit('setUpdateCustomerError', error) | |
| } | |
| ) | |
| } | |
| } | |
| const customerModule = { | |
| state: initialState, | |
| getters, | |
| mutations, | |
| actions | |
| } | |
| export customerModule |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment