|  | @@ -8,53 +8,69 @@ const distributosResource = '/distributors.json';
 | 
	
		
			
				|  |  |   * @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);
 | 
	
		
			
				|  |  | +  let distributors = [];
 | 
	
		
			
				|  |  | +  for (let key in data) {
 | 
	
		
			
				|  |  | +    const distributor = data[key];
 | 
	
		
			
				|  |  | +    distributor.id = key;
 | 
	
		
			
				|  |  | +    if (!distributor.contacts) {
 | 
	
		
			
				|  |  | +      distributor.contacts = [];
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | -    return distributors;
 | 
	
		
			
				|  |  | +    distributors.push(distributor);
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +  return distributors;
 | 
	
		
			
				|  |  |  };
 | 
	
		
			
				|  |  |  export default {
 | 
	
		
			
				|  |  | -    [types.FETCH_DISTRIBUTORS]: ({commit}) => {
 | 
	
		
			
				|  |  | -        // 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}) => {
 | 
	
		
			
				|  |  | -        commit(types.UPDATE_DISTRIBUTORS, []);
 | 
	
		
			
				|  |  | -    },
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * Expected payload: {id, distributor}
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    [types.SET_DISTRIBUTOR]: ({commit, getters}, payload) => {
 | 
	
		
			
				|  |  | -        let method = 'post';
 | 
	
		
			
				|  |  | -        let data = payload.distributor;
 | 
	
		
			
				|  |  | -        if (payload.id) {
 | 
	
		
			
				|  |  | -            method = 'put';
 | 
	
		
			
				|  |  | -            data = {
 | 
	
		
			
				|  |  | -                [payload.distributor.id]: {
 | 
	
		
			
				|  |  | -                    ...payload.distributor
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | +  [types.FETCH_DISTRIBUTORS]: ({commit}) => {
 | 
	
		
			
				|  |  | +    // 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}) => {
 | 
	
		
			
				|  |  | +    commit(types.UPDATE_DISTRIBUTORS, []);
 | 
	
		
			
				|  |  | +  },
 | 
	
		
			
				|  |  | +  /**
 | 
	
		
			
				|  |  | +   * Expected payload: {id, distributor}
 | 
	
		
			
				|  |  | +   */
 | 
	
		
			
				|  |  | +  [types.SET_DISTRIBUTOR]: ({commit, getters}, payload) => {
 | 
	
		
			
				|  |  | +    let method = 'post';
 | 
	
		
			
				|  |  | +    let data = payload.distributor;
 | 
	
		
			
				|  |  | +    if (payload.id) {
 | 
	
		
			
				|  |  | +      method = 'put';
 | 
	
		
			
				|  |  | +      data = {
 | 
	
		
			
				|  |  | +        [payload.distributor.id]: {
 | 
	
		
			
				|  |  | +          ...payload.distributor
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    // API call
 | 
	
		
			
				|  |  | +    axios[method](distributosResource, data)
 | 
	
		
			
				|  |  | +      .then( res => {
 | 
	
		
			
				|  |  | +        const id = res.data.name || payload.id
 | 
	
		
			
				|  |  | +        // When posting, the responce is an object with attribute name with the new key
 | 
	
		
			
				|  |  | +        if (res.data.name) {
 | 
	
		
			
				|  |  | +          // So we add the key to the id property in order to add to the store
 | 
	
		
			
				|  |  | +          payload.distributor.id = res.data.name;
 | 
	
		
			
				|  |  | +          commit(types.ADD_DISTRIBUTOR, {
 | 
	
		
			
				|  |  | +            id,
 | 
	
		
			
				|  |  | +            distributor: payload.distributor,
 | 
	
		
			
				|  |  | +          });
 | 
	
		
			
				|  |  | +        // When updating, the responce contains the response of what was changed
 | 
	
		
			
				|  |  | +        } else if (payload.id) {
 | 
	
		
			
				|  |  | +          commit(types.UPDATE_DISTRIBUTOR, {
 | 
	
		
			
				|  |  | +            id,
 | 
	
		
			
				|  |  | +            distributor: payload.distributor,
 | 
	
		
			
				|  |  | +          });
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +          throw new Error('Something went wrong');
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        axios[method](distributosResource, data)
 | 
	
		
			
				|  |  | -            .then( res => {
 | 
	
		
			
				|  |  | -                commit(types.UPDATE_DISTRIBUTOR, {
 | 
	
		
			
				|  |  | -                        id: payload.id,
 | 
	
		
			
				|  |  | -                        distributor: res.data,
 | 
	
		
			
				|  |  | -                });
 | 
	
		
			
				|  |  | -            }).catch( error => {
 | 
	
		
			
				|  |  | -                console.log(error)
 | 
	
		
			
				|  |  | -            });
 | 
	
		
			
				|  |  | -    },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +      }).catch( error => {
 | 
	
		
			
				|  |  | +        console.log(error)
 | 
	
		
			
				|  |  | +      });
 | 
	
		
			
				|  |  | +  },
 | 
	
		
			
				|  |  |  };
 |