|
@@ -1,46 +1,34 @@
|
|
import * as types from '../../types';
|
|
import * as types from '../../types';
|
|
|
|
+import axios from 'axios';
|
|
|
|
+const distributosResource = '/distributors.json';
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Transforms object of keys into array with id as key
|
|
|
|
+ * @param {object} response data object with keys as ids
|
|
|
|
+ * @returns {array} array of items with ids added to it
|
|
|
|
+ */
|
|
|
|
+const transformData = (data) => {
|
|
|
|
+ let distributors = [];
|
|
|
|
+ for (let key in data) {
|
|
|
|
+ const distributor = data[key];
|
|
|
|
+ distributor.id = key;
|
|
|
|
+ if (!distributor.contacts) {
|
|
|
|
+ distributor.contacts = [];
|
|
|
|
+ }
|
|
|
|
+ distributors.push(distributor);
|
|
|
|
+ }
|
|
|
|
+ return distributors;
|
|
|
|
+};
|
|
export default {
|
|
export default {
|
|
[types.FETCH_DISTRIBUTORS]: ({commit}) => {
|
|
[types.FETCH_DISTRIBUTORS]: ({commit}) => {
|
|
- // TODO: Remove this mocked data
|
|
|
|
- const distributors = [
|
|
|
|
- {
|
|
|
|
- "id": 1,
|
|
|
|
- "title": "Charton Hobbes",
|
|
|
|
- "notes": "",
|
|
|
|
- "contacts": [
|
|
|
|
- {
|
|
|
|
- "uuid": "44490103-92B7-4712-B494-E65878E6B0DC",
|
|
|
|
- "email": "abc@wisksolutions.com",
|
|
|
|
- "date": "2018-02-28T05:01:26Z",
|
|
|
|
- "name": "ABC",
|
|
|
|
- "phone": "",
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- "uuid": "E7AD1046-3293-4F41-910C-63A198C9D3B6",
|
|
|
|
- "email": "efg@wisksolutions.com",
|
|
|
|
- "date": "2018-02-28T05:01:26Z",
|
|
|
|
- "name": "EFG",
|
|
|
|
- "phone": "",
|
|
|
|
- }
|
|
|
|
- ]
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- "id": 2,
|
|
|
|
- "title": "Bob Marley",
|
|
|
|
- "notes": "Something",
|
|
|
|
- "contacts": [
|
|
|
|
- {
|
|
|
|
- "uuid": "44490103-92B7-4712-B494-E65878E6B0DC",
|
|
|
|
- "email": "abc@wisksolutions.com",
|
|
|
|
- "date": "2018-02-28T05:01:26Z",
|
|
|
|
- "name": "ABC",
|
|
|
|
- "phone": "",
|
|
|
|
- }
|
|
|
|
- ]
|
|
|
|
- },
|
|
|
|
- ]
|
|
|
|
- commit(types.UPDATE_DISTRIBUTORS, distributors)
|
|
|
|
|
|
+ // TODO: Add validation
|
|
|
|
+ axios.get(distributosResource)
|
|
|
|
+ .then( responce => {
|
|
|
|
+ const list = transformData(responce.data);
|
|
|
|
+ commit(types.UPDATE_DISTRIBUTORS, list);
|
|
|
|
+ }).catch( error => {
|
|
|
|
+ console.log(error)
|
|
|
|
+ });
|
|
},
|
|
},
|
|
[types.CLEAR_DISTRIBUTORS]: ({commit}) => {
|
|
[types.CLEAR_DISTRIBUTORS]: ({commit}) => {
|
|
commit(types.UPDATE_DISTRIBUTORS, []);
|
|
commit(types.UPDATE_DISTRIBUTORS, []);
|
|
@@ -49,13 +37,24 @@ export default {
|
|
* Expected payload: {id, distributor}
|
|
* Expected payload: {id, distributor}
|
|
*/
|
|
*/
|
|
[types.SET_DISTRIBUTOR]: ({commit, getters}, payload) => {
|
|
[types.SET_DISTRIBUTOR]: ({commit, getters}, payload) => {
|
|
|
|
+ let method = 'post';
|
|
|
|
+ let data = payload.distributor;
|
|
if (payload.id) {
|
|
if (payload.id) {
|
|
- // api call
|
|
|
|
- // Update server with what changed
|
|
|
|
- } else {
|
|
|
|
- // api call
|
|
|
|
- // Add new distributor
|
|
|
|
|
|
+ method = 'put';
|
|
|
|
+ data = {
|
|
|
|
+ [payload.distributor.id]: {
|
|
|
|
+ ...payload.distributor
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- commit(types.UPDATE_DISTRIBUTOR, payload.distributor);
|
|
|
|
|
|
+ axios[method](distributosResource, data)
|
|
|
|
+ .then( res => {
|
|
|
|
+ commit(types.UPDATE_DISTRIBUTOR, {
|
|
|
|
+ id: payload.id,
|
|
|
|
+ distributor: res.data,
|
|
|
|
+ });
|
|
|
|
+ }).catch( error => {
|
|
|
|
+ console.log(error)
|
|
|
|
+ });
|
|
},
|
|
},
|
|
};
|
|
};
|