Skip to content

Instantly share code, notes, and snippets.

@YuraKolesnikov
Created January 14, 2020 08:50
Show Gist options
  • Save YuraKolesnikov/b762ad40733e414be0f4990fea0526cf to your computer and use it in GitHub Desktop.
Save YuraKolesnikov/b762ad40733e414be0f4990fea0526cf to your computer and use it in GitHub Desktop.
import axios from './axios';
export const CustomerApi = {
async getCustomers (query = null) {
return await axios.get('/customers?expand=orders', {
params: query
});
},
async createCustomer (payload) {
return await axios.post('/customers', payload);
},
async updateCustomer ({ id, payload }) {
return await axios.put(`/customers/${id}`, payload);
},
async deleteCustomer (id) {
return await axios.delete(`/customers/${id}`);
}
};
import { CustomerApi } from '@/services/CustomerApi';
import { composeQueryString } from '@/utils/queryStringComposer';
export default {
GET_CUSTOMERS: async ({ state, commit }) => {
state.isLoading = true;
const query = composeQueryString(state.queryParams.filters);
query.append('page', state.queryParams.page);
if (state.queryParams.sort.value) {query.append('sort', state.queryParams.sort.value);}
const response = await CustomerApi.getCustomers(query);
if (response.status === 200) {
commit('SET_CUSTOMERS', response.data);
commit('SET_PAGE_COUNT', +response.headers['x-pagination-page-count']);
}
state.isLoading = false;
},
CREATE_CUSTOMER: async ({ state, commit, dispatch }, payload) => {
state.isLoading = true;
const response = await CustomerApi.createCustomer(payload);
if (response.status !== 201) {
commit('TOGGLE_SHOULD_CLOSE_MODAL', false);
} else {
commit('TOGGLE_SHOULD_CLOSE_MODAL', true);
}
dispatch(
'messages/SHOW_ALERT',
{ response, customSuccessMessage: 'Customer created successfully' },
{ root: true }
);
dispatch('GET_CUSTOMERS');
},
UPDATE_CUSTOMER: async ({ state, dispatch }, { id, payload }) => {
state.isLoading = true;
const response = await CustomerApi.updateCustomer({ id, payload });
dispatch(
'messages/SHOW_ALERT',
{ response, customSuccessMessage: 'Customer updated successfully' },
{ root: true }
);
await dispatch('GET_CUSTOMERS');
},
DELETE_CUSTOMER: async ({ state, dispatch }, id) => {
state.isLoading = true;
const response = await CustomerApi.deleteCustomer(id);
dispatch(
'messages/SHOW_ALERT',
{ response, customSuccessMessage: 'Customer deleted successfully' },
{ root: true }
);
await dispatch('GET_CUSTOMERS');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment