Store user module for users settings and username.
This commit is contained in:
104
src/modules/userModule.js
Normal file
104
src/modules/userModule.js
Normal file
@@ -0,0 +1,104 @@
|
||||
import { getSettings } from '@/api'
|
||||
|
||||
function setLocalStorageByKey(key, value) {
|
||||
if (value instanceof Object || value instanceof Array) {
|
||||
value = JSON.stringify(value)
|
||||
}
|
||||
const buff = Buffer.from(value)
|
||||
const encodedValue = buff.toString('base64')
|
||||
localStorage.setItem(key, encodedValue)
|
||||
}
|
||||
|
||||
function getLocalStorageByKey(key) {
|
||||
const encodedValue = localStorage.getItem(key)
|
||||
if (encodedValue == null) {
|
||||
return undefined
|
||||
}
|
||||
const buff = new Buffer(encodedValue, 'base64')
|
||||
const value = buff.toString('utf-8')
|
||||
|
||||
try {
|
||||
return JSON.parse(value)
|
||||
} catch {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
const ifMissingSettingsAndTokenExistsFetchSettings =
|
||||
() => getLocalStorageByKey('token') ? getSettings() : null
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state: {
|
||||
admin: false,
|
||||
settings: undefined,
|
||||
username: undefined,
|
||||
plex_userid: undefined
|
||||
},
|
||||
getters: {
|
||||
admin: (state) => {
|
||||
return state.admin
|
||||
},
|
||||
settings: (state, foo, bar) => {
|
||||
console.log('is this called?')
|
||||
const settings = state.settings || getLocalStorageByKey('settings')
|
||||
if (settings instanceof Object) {
|
||||
return settings
|
||||
}
|
||||
|
||||
ifMissingSettingsAndTokenExistsFetchSettings()
|
||||
return undefined
|
||||
},
|
||||
username: (state) => {
|
||||
const settings = state.settings || getLocalStorageByKey('settings')
|
||||
|
||||
if (settings instanceof Object && settings.hasOwnProperty('user_name')) {
|
||||
return settings.user_name
|
||||
}
|
||||
|
||||
ifMissingSettingsAndTokenExistsFetchSettings()
|
||||
return undefined
|
||||
},
|
||||
plex_userid: (state) => {
|
||||
const settings = state.settings || getLocalStorageByKey('settings')
|
||||
console.log('plex_userid from store', settings)
|
||||
|
||||
if (settings instanceof Object && settings.hasOwnProperty('plex_userid')) {
|
||||
return settings.plex_userid
|
||||
}
|
||||
|
||||
ifMissingSettingsAndTokenExistsFetchSettings()
|
||||
return undefined
|
||||
}
|
||||
},
|
||||
mutations: {
|
||||
SET_ADMIN: (state, isAdmin) => {
|
||||
state.admin = isAdmin
|
||||
},
|
||||
SET_USERNAME: (state, username) => {
|
||||
state.username = username
|
||||
console.log('username')
|
||||
setLocalStorageByKey('username', username)
|
||||
},
|
||||
SET_SETTINGS: (state, settings) => {
|
||||
state.settings = settings
|
||||
console.log('settings')
|
||||
setLocalStorageByKey('settings', settings)
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
setAdmin: ({commit}, isAdmin) => {
|
||||
if (!(isAdmin instanceof Object)) {
|
||||
throw "Parameter is not a boolean value."
|
||||
}
|
||||
commit('SET_ADMIN', isAdmin)
|
||||
},
|
||||
setSettings: ({commit}, settings) => {
|
||||
console.log('settings input', settings)
|
||||
if (!(settings instanceof Object)) {
|
||||
throw "Parameter is not a object."
|
||||
}
|
||||
commit('SET_SETTINGS', settings)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,19 @@
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
|
||||
import torrentModule from './modules/torrentModule'
|
||||
import darkmodeModule from './modules/darkmodeModule'
|
||||
import documentTitle from './modules/documentTitle'
|
||||
import torrentModule from './modules/torrentModule'
|
||||
import userModule from './modules/userModule'
|
||||
|
||||
Vue.use(Vuex)
|
||||
|
||||
const store = new Vuex.Store({
|
||||
modules: {
|
||||
torrentModule,
|
||||
darkmodeModule,
|
||||
documentTitle
|
||||
documentTitle,
|
||||
torrentModule,
|
||||
userModule
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user