mirror of
https://github.com/VueTorrent/VueTorrent.git
synced 2025-02-26 20:31:13 +03:00
split store
This commit is contained in:
parent
38117ddf43
commit
c16c98384d
4 changed files with 106 additions and 94 deletions
23
src/store/actions.js
Normal file
23
src/store/actions.js
Normal 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
10
src/store/getters.js
Normal 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
|
||||||
|
}
|
|
@ -2,10 +2,6 @@ import Vue from 'vue'
|
||||||
import Vuex from 'vuex'
|
import Vuex from 'vuex'
|
||||||
import VuexPersist from 'vuex-persist'
|
import VuexPersist from 'vuex-persist'
|
||||||
|
|
||||||
import Torrent from '../models/torrent'
|
|
||||||
import Status from '../models/Status'
|
|
||||||
import qbit from '../services/qbit'
|
|
||||||
|
|
||||||
const vuexPersist = new VuexPersist({
|
const vuexPersist = new VuexPersist({
|
||||||
key: 'vuetorrent',
|
key: 'vuetorrent',
|
||||||
storage: window.localStorage
|
storage: window.localStorage
|
||||||
|
@ -13,6 +9,10 @@ const vuexPersist = new VuexPersist({
|
||||||
|
|
||||||
Vue.use(Vuex)
|
Vue.use(Vuex)
|
||||||
|
|
||||||
|
import getters from './getters'
|
||||||
|
import mutations from './mutations'
|
||||||
|
import actions from './actions'
|
||||||
|
|
||||||
export default new Vuex.Store({
|
export default new Vuex.Store({
|
||||||
plugins: [vuexPersist.plugin],
|
plugins: [vuexPersist.plugin],
|
||||||
state: {
|
state: {
|
||||||
|
@ -46,100 +46,13 @@ export default new Vuex.Store({
|
||||||
selectedDetailTorrent: null
|
selectedDetailTorrent: null
|
||||||
},
|
},
|
||||||
getters: {
|
getters: {
|
||||||
containsTorrent: state => hash =>
|
...getters
|
||||||
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
|
|
||||||
},
|
},
|
||||||
|
|
||||||
mutations: {
|
mutations: {
|
||||||
REMOVE_INTERVALS: state => {
|
...mutations
|
||||||
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
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
INIT_INTERVALS: async context => {
|
...actions
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
66
src/store/mutations.js
Normal file
66
src/store/mutations.js
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue