all files / src/vuex/ actions.js

72.73% Statements 16/22
100% Branches 4/4
45.45% Functions 5/11
64.71% Lines 11/17
1 branch Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37                                                      
import { CHANGE_TITLE, POPULATE_SHOPPING_LISTS, ADD_SHOPPING_LIST, DELETE_SHOPPING_LIST } from './mutation_types'
import api from '../api'
import getters from './getters'
 
export default {
  populateShoppingLists: ({ commit }) => {
    return api.fetchShoppingLists().then(response => {
      commit(POPULATE_SHOPPING_LISTS, response.data)
    })
  },
  changeTitle: (store, data) => {
    store.commit(CHANGE_TITLE, data)
    return store.dispatch('updateList', data.id)
  },
  updateList: (store, id) => {
    let shoppingList = getters.getListById(store.state, id)
 
    return api.updateShoppingList(shoppingList)
  },
  createShoppingList: (store, shoppinglist) => {
    return api.addNewShoppingList(shoppinglist).then(() => {
      store.dispatch('populateShoppingLists')
    }, () => {
      store.commit(ADD_SHOPPING_LIST, shoppinglist)
    })
  },
  deleteShoppingList: (store, id) => {
    return api.deleteShoppingList(id).then(() => {
      store.dispatch('populateShoppingLists')
    }, () => {
      store.commit(DELETE_SHOPPING_LIST, id)
    })
  }
}