Skip to content

Instantly share code, notes, and snippets.

@acrolink
Created November 28, 2021 12:05
Show Gist options
  • Save acrolink/d1d2b220e4bd5bc3674a59cbba5a7bf9 to your computer and use it in GitHub Desktop.
Save acrolink/d1d2b220e4bd5bc3674a59cbba5a7bf9 to your computer and use it in GitHub Desktop.
/libsys/frontend/src/store.js [vue]
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import * as Cookies from 'js-cookie'
Vue.use(Vuex)
export default new Vuex.Store({
plugins: [
createPersistedState({
getState: (key) => Cookies.getJSON(key),
setState: (key, state) => Cookies.set(key, state, {
expires: 3,
secure: true
})
})
],
state: {
books_shelf: [],
locale: 'ar'
},
getters: {
books_shelf: state => state.books_shelf,
locale: state => state.locale
},
mutations: {
add_to_book_shelf(state, payload) {
if (state.books_shelf.filter(function (obj) {
return obj.id == payload.book.id;
}).length == 0) {
let to_be_added = {
id: payload.book.id,
code: payload.book.code,
name: payload.book.title,
author: payload.book.author,
return_after: payload.return_after,
fb_status: null,
fb_text: null
}
state.books_shelf.push(to_be_added)
}
},
remove_from_book_shelf(state, book) {
state.books_shelf.splice(state.books_shelf.findIndex(item => item.id === book.id), 1)
},
reset_book_shelf(state, book) {
state.books_shelf = []
},
set_locale(state, locale) {
console.log('change locale..')
state.locale = locale
},
},
actions: {}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment