audiobookshelf/client/store/index.js

134 lines
3.5 KiB
JavaScript
Raw Normal View History

2021-09-16 00:59:38 +02:00
import { checkForUpdate } from '@/plugins/version'
2021-09-28 13:44:40 +02:00
import Vue from 'vue'
2021-08-18 00:01:11 +02:00
export const state = () => ({
2021-09-16 00:59:38 +02:00
versionData: null,
serverSettings: null,
2021-08-18 00:01:11 +02:00
streamAudiobook: null,
editModalTab: 'details',
2021-08-18 00:01:11 +02:00
showEditModal: false,
selectedAudiobook: null,
playOnLoad: false,
developerMode: false,
selectedAudiobooks: [],
processingBatch: false,
previousPath: '/',
routeHistory: [],
showExperimentalFeatures: false
2021-08-18 00:01:11 +02:00
})
export const getters = {
getIsAudiobookSelected: state => audiobookId => {
return !!state.selectedAudiobooks.includes(audiobookId)
},
getNumAudiobooksSelected: state => state.selectedAudiobooks.length
}
2021-08-18 00:01:11 +02:00
export const actions = {
updateServerSettings({ commit }, payload) {
var updatePayload = {
...payload
}
return this.$axios.$patch('/api/serverSettings', updatePayload).then((result) => {
if (result.success) {
2021-10-06 04:10:49 +02:00
commit('setServerSettings', result.serverSettings)
return true
} else {
return false
}
}).catch((error) => {
console.error('Failed to update server settings', error)
return false
})
2021-09-16 00:59:38 +02:00
},
checkForUpdate({ commit }) {
return checkForUpdate()
.then((res) => {
commit('setVersionData', res)
return res
})
.catch((error) => {
console.error('Update check failed', error)
return false
})
},
popRoute({ commit, state }) {
if (!state.routeHistory.length) {
return null
}
var _history = [...state.routeHistory]
var last = _history.pop()
commit('setRouteHistory', _history)
return last
}
}
2021-08-18 00:01:11 +02:00
export const mutations = {
setRouteHistory(state, val) {
state.routeHistory = val
},
setPreviousPath(state, val) {
state.previousPath = val
},
2021-09-16 00:59:38 +02:00
setVersionData(state, versionData) {
state.versionData = versionData
},
setServerSettings(state, settings) {
2021-10-06 04:10:49 +02:00
if (!settings) return
state.serverSettings = settings
},
2021-08-18 00:01:11 +02:00
setStreamAudiobook(state, audiobook) {
state.playOnLoad = true
state.streamAudiobook = audiobook
},
setStream(state, stream) {
state.playOnLoad = false
state.streamAudiobook = stream ? stream.audiobook : null
},
clearStreamAudiobook(state, audiobookId) {
if (state.streamAudiobook && state.streamAudiobook.id === audiobookId) {
state.playOnLoad = false
state.streamAudiobook = null
}
},
setPlayOnLoad(state, val) {
state.playOnLoad = val
},
showEditModal(state, audiobook) {
state.editModalTab = 'details'
2021-08-18 00:01:11 +02:00
state.selectedAudiobook = audiobook
state.showEditModal = true
},
showEditModalOnTab(state, { audiobook, tab }) {
state.editModalTab = tab
state.selectedAudiobook = audiobook
state.showEditModal = true
},
setEditModalTab(state, tab) {
state.editModalTab = tab
},
2021-08-18 00:01:11 +02:00
setShowEditModal(state, val) {
state.showEditModal = val
},
setDeveloperMode(state, val) {
state.developerMode = val
},
setSelectedAudiobooks(state, audiobooks) {
2021-09-28 13:44:40 +02:00
Vue.set(state, 'selectedAudiobooks', audiobooks)
},
toggleAudiobookSelected(state, audiobookId) {
if (state.selectedAudiobooks.includes(audiobookId)) {
state.selectedAudiobooks = state.selectedAudiobooks.filter(a => a !== audiobookId)
} else {
2021-09-28 13:44:40 +02:00
var newSel = state.selectedAudiobooks.concat([audiobookId])
Vue.set(state, 'selectedAudiobooks', newSel)
}
},
setProcessingBatch(state, val) {
state.processingBatch = val
},
setExperimentalFeatures(state, val) {
state.showExperimentalFeatures = val
localStorage.setItem('experimental', val ? 1 : 0)
2021-08-18 00:01:11 +02:00
}
}