import axios from "axios"

export default {
  state: {
    venues: null
  },
  getters: {
    user(state, getters, rootState) {
      return rootState.user
    }
  },
  actions: {
    async fetchVenues({ commit }, location) {
      const items = (await axios.post("/api/nightlife/search", location)).data
        .venues.response.groups[0].items
      commit("showVenues", { location, items })
    },
    setFavorite({ commit }, { newFavs, venueId }) {
      commit("setFavs", { newFavs, venueId })
      axios.put("/api/nightlife/fav", newFavs)
    },
    setAttendance({ commit }, { toAttend, venueId }) {
      commit("setAttendance", { toAttend, venueId })
      axios.put("/api/nightlife/attend", toAttend)
    }
  },
  mutations: {
    showVenues(state, { items, location }) {
      const user = this.getters.user

      //update user with new searh preferences
      if (!user.nightlife) {
        user.nightlife = {
          search: location,
          attending: [],
          favs: [],
          date: new Date().toJSON().slice(0, 10)
        }
      } else {
        user.nightlife.search = location
      }
      state.venues = items.map(item => {
        const photo = item.venue.photos.groups[0].items[0]
        return {
          id: item.venue.id,
          title: item.venue.name,
          avatar: photo.prefix + "48x48" + photo.suffix,
          description: `${item.venue.stats.checkinsCount} Check-ins | ${item
            .venue.location.address}`,
          url: `https://www.google.com/maps/search/${item.venue.name} ${item
            .venue.location.city} ${item.venue.location.country}`,
          going: this.getters.user.nightlife.attending.includes(item.venue.id),
          fav: this.getters.user.nightlife.favs.includes(item.venue.id)
        }
      })
    },
    setFavs(state, { newFavs, venueId }) {
      this.getters.user.nightlife.favs = newFavs
      state.venues.find(
        venue => venue.id === venueId && (venue.fav = !venue.fav)
      )
    },
    setAttendance(state, { toAttend, venueId }) {
      this.getters.user.nightlife.attending = toAttend
      state.venues.find(
        venue => venue.id === venueId && (venue.going = !venue.going)
      )
    }
  }
}

@@ -91,16 +91,25 @@
     user
+Nightlife
 (state, 
@@ -157,16 +157,26 @@
 ate.user
+.nightlife
 %0A    %7D%0A 
@@ -805,18 +805,25 @@
     
-cons
+le
 t user
+Nightlife
  = t
@@ -838,17 +838,58 @@
 ers.user
-%0A
+Nightlife%0A      console.log(userNightlife)
 %0A      /
@@ -939,26 +939,25 @@
    if (!user
-.n
+N
 ightlife) %7B%0A
@@ -960,34 +960,33 @@
 ) %7B%0A        user
-.n
+N
 ightlife = %7B%0A   
@@ -1137,26 +1137,25 @@
         user
-.n
+N
 ightlife.sea
@@ -1691,35 +1691,21 @@
  going: 
-this.getters.
 user
-.n
+N
 ightlife
@@ -1755,35 +1755,21 @@
    fav: 
-this.getters.
 user
-.n
+N
 ightlife
@@ -1882,34 +1882,33 @@
 his.getters.user
-.n
+N
 ightlife.favs = 
@@ -2090,26 +2090,25 @@
 getters.user
-.n
+N
 ightlife.att

Fix nightlife app getter inVuex module