diff --git a/src/store/actions.js b/src/store/actions.js new file mode 100644 index 00000000..6322f8b8 --- /dev/null +++ b/src/store/actions.js @@ -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 + } +} diff --git a/src/store/getters.js b/src/store/getters.js new file mode 100644 index 00000000..589b5142 --- /dev/null +++ b/src/store/getters.js @@ -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 +} diff --git a/src/store/index.js b/src/store/index.js index 789944dc..00f1f6ef 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -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 } }) diff --git a/src/store/mutations.js b/src/store/mutations.js new file mode 100644 index 00000000..2f9eed8b --- /dev/null +++ b/src/store/mutations.js @@ -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 + } +}