split store

This commit is contained in:
Daan Wijns 2020-05-30 13:10:13 +02:00
parent 38117ddf43
commit c16c98384d
4 changed files with 106 additions and 94 deletions

23
src/store/actions.js Normal file
View file

@ -0,0 +1,23 @@
import Vue from 'vue'
import qbit from '../services/qbit'
export default {
INIT_INTERVALS: async context => {
context.state.intervals[0] = setInterval(() => {
context.commit('updateMainData')
}, 2000)
},
LOGIN: async (context, payload) => {
const res = await qbit.login(payload)
console.log(res)
if (res === 'Ok.') {
Vue.$toast.success('Successfully logged in!')
context.commit('LOGIN', true)
context.commit('updateMainData')
context.commit('SET_SETTINGS')
return true
}
Vue.$toast.error('Log in failed 😕')
return false
}
}

10
src/store/getters.js Normal file
View file

@ -0,0 +1,10 @@
export default {
containsTorrent: state => hash => state.selected_torrents.includes(hash),
getTheme: state => () => state.webuiSettings.darkTheme,
getModalState: state => name => state.modals[name.toLowerCase()],
getSettings: state => () => state.settings,
getStatus: state => () => state.status,
getTorrent: state => hash =>
state.torrents.filter(el => el.hash === hash)[0],
getWebuiSettings: state => () => state.webuiSettings
}

View file

@ -2,10 +2,6 @@ import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersist from 'vuex-persist'
import Torrent from '../models/torrent'
import Status from '../models/Status'
import qbit from '../services/qbit'
const vuexPersist = new VuexPersist({
key: 'vuetorrent',
storage: window.localStorage
@ -13,6 +9,10 @@ const vuexPersist = new VuexPersist({
Vue.use(Vuex)
import getters from './getters'
import mutations from './mutations'
import actions from './actions'
export default new Vuex.Store({
plugins: [vuexPersist.plugin],
state: {
@ -46,100 +46,13 @@ export default new Vuex.Store({
selectedDetailTorrent: null
},
getters: {
containsTorrent: state => hash =>
state.selected_torrents.includes(hash),
getTheme: state => () => state.webuiSettings.darkTheme,
getModalState: state => name => state.modals[name.toLowerCase()],
getSettings: state => () => state.settings,
getStatus: state => () => state.status,
getTorrent: state => hash =>
state.torrents.filter(el => el.hash === hash)[0],
getWebuiSettings: state => () => state.webuiSettings
...getters
},
mutations: {
REMOVE_INTERVALS: state => {
state.intervals.forEach(el => clearInterval(el))
},
TOGGLE_MODAL(state, modal) {
state.modals[modal.toLowerCase()] = !state.modals[
modal.toLowerCase()
]
},
SET_SELECTED: (state, payload) => {
if (payload.type === 'add')
state.selected_torrents.push(payload.hash)
if (payload.type === 'remove')
state.selected_torrents.splice(
state.selected_torrents.indexOf(payload.hash),
1
)
},
RESET_SELECTED: state => {
state.selected_torrents = []
},
TOGGLE_THEME(state) {
state.webuiSettings.darkTheme = !state.webuiSettings.darkTheme
},
LOGOUT: state => {
qbit.logout()
state.authenticated = false
},
LOGIN: async (state, payload) => {
state.authenticated = payload
},
updateMainData: async state => {
const rid = state.rid ? state.rid : undefined
const res = await qbit.getMainData(rid)
// status
state.status = new Status(res.data.server_state)
// graph
state.download_data.splice(0, 1)
state.download_data.push(state.status.dlspeedRaw)
state.upload_data.splice(0, 1)
state.upload_data.push(state.status.upspeedRaw)
const { data } = await qbit.getTorrents(state.sort_options)
// torrents
state.torrents = []
for (const [key, value] of Object.entries(data)) {
state.torrents.push(new Torrent({ hash: key, ...value }))
}
},
SET_SETTINGS: async state => {
const { data } = await qbit.getAppPreferences()
state.settings.savePath = data.save_path
},
SET_SELECTED_TORRENT_DETAIL: (state, hash) => {
state.selectedDetailTorrent = hash
},
UPDATE_SORT_OPTIONS: (state, payload) => {
state.sort_options.sort = payload.name
state.sort_options.reverse = payload.reverse
state.sort_options.hashes = payload.hashes ? payload.hashes : null
state.sort_options.filter = payload.filter ? payload.filter : null
}
...mutations
},
actions: {
INIT_INTERVALS: async context => {
context.state.intervals[0] = setInterval(() => {
context.commit('updateMainData')
}, 2000)
},
LOGIN: async (context, payload) => {
const res = await qbit.login(payload)
console.log(res)
if (res === 'Ok.') {
Vue.$toast.success('Successfully logged in!')
context.commit('LOGIN', true)
context.commit('updateMainData')
context.commit('SET_SETTINGS')
return true
}
Vue.$toast.error('Log in failed 😕')
return false
}
...actions
}
})

66
src/store/mutations.js Normal file
View file

@ -0,0 +1,66 @@
import Torrent from '../models/torrent'
import Status from '../models/Status'
import qbit from '../services/qbit'
export default {
REMOVE_INTERVALS: state => {
state.intervals.forEach(el => clearInterval(el))
},
TOGGLE_MODAL(state, modal) {
state.modals[modal.toLowerCase()] = !state.modals[modal.toLowerCase()]
},
SET_SELECTED: (state, payload) => {
if (payload.type === 'add') state.selected_torrents.push(payload.hash)
if (payload.type === 'remove')
state.selected_torrents.splice(
state.selected_torrents.indexOf(payload.hash),
1
)
},
RESET_SELECTED: state => {
state.selected_torrents = []
},
TOGGLE_THEME(state) {
state.webuiSettings.darkTheme = !state.webuiSettings.darkTheme
},
LOGOUT: state => {
qbit.logout()
state.authenticated = false
},
LOGIN: async (state, payload) => {
state.authenticated = payload
},
updateMainData: async state => {
const rid = state.rid ? state.rid : undefined
const res = await qbit.getMainData(rid)
// status
state.status = new Status(res.data.server_state)
// graph
state.download_data.splice(0, 1)
state.download_data.push(state.status.dlspeedRaw)
state.upload_data.splice(0, 1)
state.upload_data.push(state.status.upspeedRaw)
const { data } = await qbit.getTorrents(state.sort_options)
// torrents
state.torrents = []
for (const [key, value] of Object.entries(data)) {
state.torrents.push(new Torrent({ hash: key, ...value }))
}
},
SET_SETTINGS: async state => {
const { data } = await qbit.getAppPreferences()
state.settings.savePath = data.save_path
},
SET_SELECTED_TORRENT_DETAIL: (state, hash) => {
state.selectedDetailTorrent = hash
},
UPDATE_SORT_OPTIONS: (state, payload) => {
state.sort_options.sort = payload.name
state.sort_options.reverse = payload.reverse
state.sort_options.hashes = payload.hashes ? payload.hashes : null
state.sort_options.filter = payload.filter ? payload.filter : null
}
}