Forked from
onny / nextcloud-app-podcast
52 commits behind the upstream repository.
episode.js 2.63 KiB
/*
* @copyright Copyright (c) 2021 Jonas Heinrich <onny@project-insanity.org>
*
* @author Jonas Heinrich <onny@project-insanity.org>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { EpisodeApi } from './../services/EpisodeApi'
const apiClient = new EpisodeApi()
export default {
state: {
episodes: [],
},
getters: {
getEpisodes: state => {
return state.episodes
},
episodeById: state => (id) => {
return state.episodes.find((episode) => episode.id === id)
},
episodeExists: state => (id) => {
return state.episodes.some((episode) => episode.id === id)
},
},
mutations: {
addEpisode(state, episode) {
state.episodes.unshift(episode)
},
removeEpisode(state, episode) {
const existingIndex = state.episodes.findIndex(_episode => _episode.id === episode.id)
if (existingIndex !== -1) {
state.episodes.splice(existingIndex, 1)
}
},
setEpisodes(state, episodes) {
state.episodes = episodes
},
updateEpisode(state, { episode, playtime } = {}) {
const existingIndex = state.episodes.findIndex(_episode => _episode.id === episode.id)
state.episodes[existingIndex].playtime = playtime
state.episodes[existingIndex].lastplayed = Date.now()
},
},
actions: {
async loadEpisodes(context) {
const episodes = await apiClient.loadEpisodes()
if (episodes) {
context.dispatch('loadEpisode', episodes[0])
}
context.commit('setEpisodes', episodes)
},
addEpisode({ commit, getters }, episode) {
if (getters.episodeExists(episode.id)) {
return true
}
apiClient.addEpisode(episode)
.then((episode) => {
commit('addEpisode', episode)
})
},
removeEpisode({ commit }, episode) {
apiClient.removeEpisode(episode)
.then((episode) => {
commit('removeEpisode', episode)
})
},
updateEpisode({ commit, getters }, { episode, playtime } = {}) {
apiClient.updateEpisode({ episode, playtime })
.then((episode) => {
commit('updateEpisode', { episode, playtime })
})
},
},
}