text: import * as firebase from 'firebase'
  
  export default {
    state: {
      loadedMeetups: [
        // {
        //   imageUrl: 'https://upload.wikimedia.org/wikipedia/commons/4/47/New_york_times_square-terabass.jpg',
        //   id: 'afajfjadfaadfa323',
        //   title: 'Meetup in New York',
        //   date: new Date(),
        //   location: 'New York',
        //   description: 'New York, New York!'
        // },
        // {
        //   imageUrl: 'https://upload.wikimedia.org/wikipedia/commons/7/7a/Paris_-_Blick_vom_gro%C3%9Fen_Triumphbogen.jpg',
        //   id: 'aadsfhbkhlk1241',
        //   title: 'Meetup in Paris',
        //   date: new Date(),
        //   location: 'Paris',
        //   description: 'It\'s Paris!'
        // }
      ]
    },
    mutations: {
      setLoadedMeetups (state, payload) {
        state.loadedMeetups = payload
      },
      createMeetup (state, payload) {
        state.loadedMeetups.push(payload)
      },
      updateMeetup (state, payload) {
        const meetup = state.loadedMeetups.find(meetup => {
          return meetup.id === payload.id
        })
        if (payload.name) {
          meetup.name = payload.name
        }
        if (payload.regno) {
          meetup.regno = payload.regno
        }
        if (payload.course) {
          meetup.course = payload.course
        }
        if (payload.age) {
          meetup.age = payload.age
        }
        if (payload.date) {
          meetup.date = payload.date
        }
      }
    },
    actions: {
      loadMeetups ({commit}) {
        commit('setLoading', true)
        firebase.database().ref('meetups').once('value')
          .then((data) => {
            const meetups = []
            const obj = data.val()
            for (let key in obj) {
              meetups.push({
                id: key,
                name: obj[key].name,
                regno: obj[key].regno,
                imageUrl: obj[key].imageUrl,
                image: obj[key].image,
                date: obj[key].date,
                age: obj[key].age,
                course:obj[key].course,
                creatorId: obj[key].creatorId
              })
            }
            commit('setLoadedMeetups', meetups)
            commit('setLoading', false)
          })
          .catch(
            (error) => {
              console.log(error)
              commit('setLoading', false)
            }
          )
      },
      createMeetup ({commit, getters}, payload) {
        const meetup = {
          name: payload.name,
          regno: payload.regno,
          date: payload.date.toISOString(),
          course:payload.course,
          age:payload.age,
          creatorId: getters.user.id,
          image: payload.image,
          imageUrl: payload.imageUrl
        }
        let imageUrl
        let key
        firebase.database().ref('meetups').push(meetup)
          .then((data) => {
            key = data.key
            return key
          })
          .then(key => {
            const filename = payload.image.name
            const ext = filename.slice(filename.lastIndexOf('.'))
            return firebase.storage().ref('meetups/' + key + '.' + ext).put(payload.image)
          })
          .then(fileData => {
            imageUrl = fileData.metadata.downloadURLs[0]
            return firebase.database().ref('meetups').child(key).update({imageUrl: imageUrl})
          })
          .then(() => {
            commit('createMeetup', {
              ...meetup,
              imageUrl: imageUrl,
              id: key
            })
          })
          .catch((error) => {
            console.log(error)
          })
        // Reach out to firebase and store it
      },
      updateMeetupData ({commit}, payload) {
        commit('setLoading', true)
        const updateObj = {}
        if (payload.name) {
          updateObj.name = payload.name
        }
        if (payload.regno) {
          updateObj.regno = payload.regno
        }
        if (payload.course) {
          updateObj.course = payload.course
        }
        if (payload.age) {
          updateObj.age = payload.age
        }
        if (payload.date) {
          updateObj.date = payload.date
        }
        firebase.database().ref('meetups').child(payload.id).update(updateObj)
          .then(() => {
            commit('setLoading', false)
            commit('updateMeetup', payload)
          })
          .catch(error => {
            console.log(error)
            commit('setLoading', false)
          })
      }
    },
    getters: {
      loadedMeetups (state) {
        return state.loadedMeetups.sort((meetupA, meetupB) => {
          return meetupA.date > meetupB.date
        })
      },
      featuredMeetups (state, getters) {
        return getters.loadedMeetups.slice(0, 5)
      },
      loadedMeetup (state) {
        return (meetupId) => {
          return state.loadedMeetups.find((meetup) => {
            return meetup.id === meetupId
          })
        }
      }
    }
  }
  