diff --git a/frontend/main.js b/frontend/main.js index da93ebe..003850c 100644 --- a/frontend/main.js +++ b/frontend/main.js @@ -1,7 +1,7 @@ import Vue from 'vue' import VueRouter from 'vue-router' import router from './routes' -// import store from './store' +import store from './store' import App from './App.vue' @@ -10,7 +10,7 @@ Vue.use(VueRouter) new Vue({ el: '#app', router, - // store, + store, components: { App }, render: h => h(App) -}) \ No newline at end of file +}) diff --git a/frontend/modules/cartModule.js b/frontend/modules/cartModule.js new file mode 100644 index 0000000..7e53d7f --- /dev/null +++ b/frontend/modules/cartModule.js @@ -0,0 +1,44 @@ + +const updateLocalStorage = (key, value) => { + localStorage.setItem(key, JSON.stringify(value)); +} +const getParsedFromLocalStorage = key => { + const value = localStorage.getItem(key); + try { + return JSON.parse(value) + } catch { + return null + } +} + +export default { + namespaced: true, + state: { + inventory: getParsedFromLocalStorage('inventory') || [], + }, + getters: { + inventory: state => { + return state.inventory; + } + }, + mutations: { + ADD_ITEM_TO_CART: (state, item) => { + state.inventory.push(item); + + updateLocalStorage('inventory', state.inventory) + }, + REMOVE_ITEM_FROM_CART: (state, item) => { + state.inventory = state.inventory.filter(i => i !== item) + + updateLocalStorage('inventory', state.inventory) + } + }, + actions: { + addItemToCart({ commit }, item) { + commit("ADD_ITEM_TO_CART", item); + }, + removeItemFromCart({ commit }, item) { + commit('REMOVE_ITEM_FROM_CART', item) + } + } +}; diff --git a/frontend/modules/playerModule.js b/frontend/modules/playerModule.js deleted file mode 100644 index bec4599..0000000 --- a/frontend/modules/playerModule.js +++ /dev/null @@ -1,117 +0,0 @@ - -export default { - namespaced: true, - state: { - player: undefined, - playlist: [], - authenticatedAdmin: false, - channelSettings: null, - clientSettings: null, - nowPlaying: null, - userSuggested: [], - externalSuggested: [], - PLAYER_STATES: { - BUFFERING: 3, - CUED: 5, - ENDED: 0, - PAUSED: 2, - PLAYING: 1, - UNSTARTED: -1 - }, - socket: null, - channel: "summér" - }, - getters: { - channel: state => { - return state.channel; - }, - socket: state => { - return state.socket - }, - PLAYER_STATES: state => { - return state.PLAYER_STATES; - }, - authenticatedAdmin: state => { - return state.authenticatedAdmin; - }, - playlist: state => { - return state.playlist; - }, - channelSettings: state => { - return state.channelSettings; - }, - clientSettings: state => { - return state.clientSettings; - }, - nowPlaying: state => { - return state.nowPlaying; - }, - userSuggested: state => { - return state.userSuggested; - }, - externalSuggested: state => { - return state.externalSuggested; - }, - player: state => { - return state.player; - } - }, - mutations: { - SET_CHANNEL: (state, channel) => { - state.channel = channel; - }, - SET_AUTHENTICATED_ADMIN: (state, authenticatedAdmin) => { - state.authenticatedAdmin = authenticatedAdmin; - }, - SET_PLAYLIST: (state, playlist) => { - state.playlist = playlist; - }, - SET_CHANNEL_SETTINGS: (state, channelSettings) => { - state.channelSettings = channelSettings; - }, - SET_CLIENT_SETTINGS: (state, clientSettings) => { - state.clientSettings = clientSettings; - }, - SET_NOW_PLAYING: (state, nowPlaying) => { - state.nowPlaying = nowPlaying; - }, - SET_USER_SUGGESTED: (state, userSuggested) => { - state.userSuggested = userSuggested; - }, - SET_EXTERNAL_SUGGESTED: (state, externalSuggested) => { - state.externalSuggested = externalSuggested; - }, - SET_PLAYER: (state, player) => { - state.player = player; - } - }, - actions: { - setChannel({ commit }, channel) { - commit("SET_CHANNEL", channel.toLowerCase()); - }, - setAuthenticatedAdmin({ commit }, authenticatedAdmin) { - commit("SET_AUTHENTICATED_ADMIN", authenticatedAdmin); - }, - setPlaylist({ commit }, playlist) { - commit("SET_PLAYLIST", playlist); - }, - setChannelSettings({ commit }, channelSettings) { - commit("SET_CHANNEL_SETTINGS", channelSettings); - }, - setClientSettings({ commit }, clientSettings) { - commit("SET_CLIENT_SETTINGS", clientSettings); - }, - setNowPlaying({ commit }, nowPlaying) { - commit("SET_NOW_PLAYING", nowPlaying); - }, - setUserSuggested({ commit }, userSuggested) { - commit("SET_USER_SUGGESTED", userSuggested); - }, - setExternalSuggested({ commit }, externalSuggested) { - commit("SET_EXTERNAL_SUGGESTED", externalSuggested); - }, - setPlayer({ commit }, player) { - commit("SET_PLAYER", player); - } - } -}; diff --git a/frontend/store.js b/frontend/store.js index 739fa3a..498d864 100644 --- a/frontend/store.js +++ b/frontend/store.js @@ -1,14 +1,13 @@ import Vue from 'vue'; import Vuex from 'vuex'; - -import playerModule from '@/modules/playerModule'; - Vue.use(Vuex); +import cartModule from '@/modules/cartModule'; + const store = new Vuex.Store({ modules: { - playerModule + cartModule } }) -export default store; \ No newline at end of file +export default store;