2021-08-24 01:31:04 +02:00
|
|
|
|
2021-10-24 22:53:51 +02:00
|
|
|
import Vue from 'vue'
|
|
|
|
|
2021-08-24 01:31:04 +02:00
|
|
|
export const state = () => ({
|
|
|
|
user: null,
|
|
|
|
settings: {
|
|
|
|
orderBy: 'book.title',
|
|
|
|
orderDesc: false,
|
|
|
|
filterBy: 'all',
|
2021-08-26 16:47:51 +02:00
|
|
|
playbackRate: 1,
|
|
|
|
bookshelfCoverSize: 120
|
2021-08-24 01:31:04 +02:00
|
|
|
},
|
|
|
|
settingsListeners: []
|
|
|
|
})
|
|
|
|
|
|
|
|
export const getters = {
|
2021-08-27 14:01:47 +02:00
|
|
|
getIsRoot: (state) => state.user && state.user.type === 'root',
|
2021-08-24 01:31:04 +02:00
|
|
|
getToken: (state) => {
|
|
|
|
return state.user ? state.user.token : null
|
|
|
|
},
|
|
|
|
getUserAudiobook: (state) => (audiobookId) => {
|
|
|
|
return state.user && state.user.audiobooks ? state.user.audiobooks[audiobookId] || null : null
|
|
|
|
},
|
|
|
|
getUserSetting: (state) => (key) => {
|
|
|
|
return state.settings ? state.settings[key] || null : null
|
|
|
|
},
|
2021-09-07 00:42:15 +02:00
|
|
|
getUserCanUpdate: (state) => {
|
|
|
|
return state.user && state.user.permissions ? !!state.user.permissions.update : false
|
|
|
|
},
|
|
|
|
getUserCanDelete: (state) => {
|
|
|
|
return state.user && state.user.permissions ? !!state.user.permissions.delete : false
|
|
|
|
},
|
|
|
|
getUserCanDownload: (state) => {
|
|
|
|
return state.user && state.user.permissions ? !!state.user.permissions.download : false
|
2021-09-18 19:45:34 +02:00
|
|
|
},
|
|
|
|
getUserCanUpload: (state) => {
|
|
|
|
return state.user && state.user.permissions ? !!state.user.permissions.upload : false
|
2021-10-23 03:08:02 +02:00
|
|
|
},
|
|
|
|
getUserCanAccessAllLibraries: (state) => {
|
|
|
|
return state.user && state.user.permissions ? !!state.user.permissions.accessAllLibraries : false
|
|
|
|
},
|
|
|
|
getLibrariesAccessible: (state, getters) => {
|
|
|
|
if (!state.user) return []
|
|
|
|
if (getters.getUserCanAccessAllLibraries) return []
|
|
|
|
return state.user.librariesAccessible || []
|
|
|
|
},
|
|
|
|
getCanAccessLibrary: (state, getters) => (libraryId) => {
|
|
|
|
if (!state.user) return false
|
|
|
|
if (getters.getUserCanAccessAllLibraries) return true
|
|
|
|
return getters.getLibrariesAccessible.includes(libraryId)
|
2021-08-24 01:31:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
updateUserSettings({ commit }, payload) {
|
|
|
|
var updatePayload = {
|
|
|
|
...payload
|
|
|
|
}
|
2021-09-21 23:42:01 +02:00
|
|
|
// Immediately update
|
|
|
|
commit('setSettings', updatePayload)
|
2021-08-24 01:31:04 +02:00
|
|
|
return this.$axios.$patch('/api/user/settings', updatePayload).then((result) => {
|
|
|
|
if (result.success) {
|
|
|
|
commit('setSettings', result.settings)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}).catch((error) => {
|
|
|
|
console.error('Failed to update settings', error)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
setUser(state, user) {
|
|
|
|
state.user = user
|
2021-10-23 03:08:02 +02:00
|
|
|
|
2021-08-24 02:37:40 +02:00
|
|
|
if (user) {
|
|
|
|
if (user.token) localStorage.setItem('token', user.token)
|
|
|
|
} else {
|
2021-08-24 01:31:04 +02:00
|
|
|
localStorage.removeItem('token')
|
|
|
|
}
|
|
|
|
},
|
2021-10-24 22:53:51 +02:00
|
|
|
updateUserAudiobook(state, { id, data }) {
|
|
|
|
if (!state.user) return
|
|
|
|
if (!state.user.audiobooks) {
|
|
|
|
Vue.set(state.user, 'audiobooks', {})
|
|
|
|
}
|
|
|
|
Vue.set(state.user.audiobooks, id, data)
|
|
|
|
},
|
2021-08-24 01:31:04 +02:00
|
|
|
setSettings(state, settings) {
|
|
|
|
if (!settings) return
|
|
|
|
|
|
|
|
var hasChanges = false
|
|
|
|
for (const key in settings) {
|
|
|
|
if (state.settings[key] !== settings[key]) {
|
|
|
|
hasChanges = true
|
|
|
|
state.settings[key] = settings[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasChanges) {
|
|
|
|
state.settingsListeners.forEach((listener) => {
|
|
|
|
listener.meth(state.settings)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addSettingsListener(state, listener) {
|
|
|
|
var index = state.settingsListeners.findIndex(l => l.id === listener.id)
|
|
|
|
if (index >= 0) state.settingsListeners.splice(index, 1, listener)
|
|
|
|
else state.settingsListeners.push(listener)
|
|
|
|
},
|
|
|
|
removeSettingsListener(state, listenerId) {
|
|
|
|
state.settingsListeners = state.settingsListeners.filter(l => l.id !== listenerId)
|
|
|
|
}
|
|
|
|
}
|