2020-10-12 15:18:57 +02:00
|
|
|
import Vue from 'vue'
|
2020-10-12 17:36:47 +02:00
|
|
|
import api from '@/api'
|
2020-09-27 15:18:07 +02:00
|
|
|
import router from '@/router'
|
2020-10-12 17:36:47 +02:00
|
|
|
import { timeout } from '@/helpers/commons'
|
2020-08-27 18:30:45 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
state: {
|
2020-10-12 15:18:57 +02:00
|
|
|
host: window.location.host,
|
2020-08-27 18:30:45 +02:00
|
|
|
connected: localStorage.getItem('connected') === 'true',
|
2020-10-09 18:02:22 +02:00
|
|
|
yunohost: null, // yunohost app infos: Object {version, repo}
|
2020-10-13 20:40:32 +02:00
|
|
|
error: null,
|
2020-10-09 22:05:23 +02:00
|
|
|
waiting: false,
|
2020-10-12 15:18:57 +02:00
|
|
|
history: []
|
2020-08-27 18:30:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
mutations: {
|
|
|
|
'SET_CONNECTED' (state, connected) {
|
|
|
|
localStorage.setItem('connected', connected)
|
|
|
|
state.connected = connected
|
|
|
|
},
|
|
|
|
|
|
|
|
'SET_YUNOHOST_INFOS' (state, yunohost) {
|
|
|
|
state.yunohost = yunohost
|
2020-10-09 18:02:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'UPDATE_WAITING' (state, boolean) {
|
|
|
|
state.waiting = boolean
|
2020-10-12 15:18:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'ADD_HISTORY_ENTRY' (state, [uri, method, date]) {
|
2021-02-15 15:21:26 +01:00
|
|
|
state.history.push({ uri, method, date, status: 'pending', messages: [] })
|
|
|
|
},
|
|
|
|
|
|
|
|
'UPDATE_LAST_HISTORY_ENTRY' (state, [key, value]) {
|
|
|
|
Vue.set(state.history[state.history.length - 1], key, value)
|
2020-10-12 15:18:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'ADD_MESSAGE' (state, message) {
|
|
|
|
state.history[state.history.length - 1].messages.push(message)
|
|
|
|
},
|
|
|
|
|
|
|
|
'UPDATE_PROGRESS' (state, progress) {
|
|
|
|
Vue.set(state.history[state.history.length - 1], 'progress', progress)
|
2020-10-13 20:40:32 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'SET_ERROR' (state, error) {
|
|
|
|
state.error = error
|
2020-08-27 18:30:45 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2020-09-27 15:18:07 +02:00
|
|
|
'LOGIN' ({ dispatch }, password) {
|
|
|
|
// Entering a wrong password will trigger a 401 api response.
|
|
|
|
// action `DISCONNECT` will then be triggered by the response handler but will not
|
|
|
|
// redirect to `/login` so the view can display the catched error.
|
2020-08-27 18:30:45 +02:00
|
|
|
return api.post('login', { password }).then(() => {
|
2020-09-27 15:18:07 +02:00
|
|
|
dispatch('CONNECT')
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
'LOGOUT' ({ dispatch }) {
|
|
|
|
return api.get('logout').then(() => {
|
|
|
|
dispatch('DISCONNECT')
|
2020-08-27 18:30:45 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-10-13 20:40:32 +02:00
|
|
|
'RESET_CONNECTED' ({ commit }) {
|
2020-08-27 18:30:45 +02:00
|
|
|
commit('SET_CONNECTED', false)
|
|
|
|
commit('SET_YUNOHOST_INFOS', null)
|
2020-10-13 20:40:32 +02:00
|
|
|
},
|
|
|
|
|
2020-11-25 18:23:52 +01:00
|
|
|
'DISCONNECT' ({ dispatch, commit }, route) {
|
2020-10-13 20:40:32 +02:00
|
|
|
dispatch('RESET_CONNECTED')
|
2020-11-25 18:23:52 +01:00
|
|
|
commit('UPDATE_WAITING', false)
|
2020-10-13 20:40:32 +02:00
|
|
|
if (router.currentRoute.name === 'login') return
|
2020-09-27 15:18:07 +02:00
|
|
|
router.push({
|
|
|
|
name: 'login',
|
|
|
|
// Add a redirect query if next route is not unknown (like `logout`) or `login`
|
|
|
|
query: route && !['login', null].includes(route.name)
|
|
|
|
? { redirect: route.path }
|
|
|
|
: {}
|
|
|
|
})
|
2020-08-27 18:30:45 +02:00
|
|
|
},
|
|
|
|
|
2020-09-27 15:18:07 +02:00
|
|
|
'CONNECT' ({ commit, dispatch }) {
|
|
|
|
commit('SET_CONNECTED', true)
|
|
|
|
dispatch('GET_YUNOHOST_INFOS')
|
|
|
|
router.push(router.currentRoute.query.redirect || { name: 'home' })
|
2020-08-27 18:30:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'GET_YUNOHOST_INFOS' ({ commit }) {
|
|
|
|
return api.get('versions').then(versions => {
|
|
|
|
commit('SET_YUNOHOST_INFOS', versions.yunohost)
|
|
|
|
})
|
2020-09-27 15:18:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'CHECK_INSTALL' ({ dispatch }, retry = 2) {
|
|
|
|
// this action will try to query the `/installed` route 3 times every 5 s with
|
|
|
|
// a timeout of the same delay.
|
2020-09-27 22:36:32 +02:00
|
|
|
// FIXME need testing with api not responding
|
2020-09-27 15:18:07 +02:00
|
|
|
return timeout(api.get('installed'), 5000).then(({ installed }) => {
|
|
|
|
return installed
|
|
|
|
}).catch(err => {
|
|
|
|
if (retry > 0) {
|
|
|
|
return dispatch('CHECK_INSTALL', --retry)
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
})
|
2020-10-09 22:05:23 +02:00
|
|
|
},
|
|
|
|
|
2020-10-12 15:18:57 +02:00
|
|
|
'WAITING_FOR_RESPONSE' ({ commit }, [uri, method]) {
|
|
|
|
commit('UPDATE_WAITING', true)
|
|
|
|
commit('ADD_HISTORY_ENTRY', [uri, method, Date.now()])
|
|
|
|
},
|
|
|
|
|
2021-02-15 15:21:26 +01:00
|
|
|
'SERVER_RESPONDED' ({ state, commit }, success) {
|
|
|
|
const action = state.history.length ? state.history[state.history.length - 1] : null
|
|
|
|
if (action) {
|
|
|
|
let status = success ? 'success' : 'error'
|
|
|
|
if (status === 'success' && action.messages.some(msg => msg.type === 'danger' || msg.type === 'warning')) {
|
|
|
|
status = 'warning'
|
|
|
|
}
|
|
|
|
commit('UPDATE_LAST_HISTORY_ENTRY', ['status', status])
|
|
|
|
}
|
2021-02-11 15:22:42 +01:00
|
|
|
commit('UPDATE_WAITING', false)
|
2020-10-12 15:18:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'DISPATCH_MESSAGE' ({ commit }, messages) {
|
|
|
|
const typeToColor = { error: 'danger' }
|
|
|
|
for (const type in messages) {
|
|
|
|
const message = {
|
|
|
|
text: messages[type],
|
|
|
|
type: type in typeToColor ? typeToColor[type] : type
|
|
|
|
}
|
|
|
|
let progressBar = message.text.match(/^\[#*\+*\.*\] > /)
|
|
|
|
if (progressBar) {
|
|
|
|
progressBar = progressBar[0]
|
|
|
|
message.text = message.text.replace(progressBar, '')
|
|
|
|
const progress = { '#': 0, '+': 0, '.': 0 }
|
|
|
|
for (const char of progressBar) {
|
|
|
|
if (char in progress) progress[char] += 1
|
|
|
|
}
|
|
|
|
commit('UPDATE_PROGRESS', Object.values(progress))
|
|
|
|
}
|
|
|
|
if (message.text) {
|
|
|
|
commit('ADD_MESSAGE', message)
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 20:40:32 +02:00
|
|
|
},
|
|
|
|
|
2021-02-11 15:22:42 +01:00
|
|
|
'HANDLE_ERROR' ({ state, commit, dispatch }, error) {
|
|
|
|
if (error.code === 401) {
|
|
|
|
// Unauthorized
|
|
|
|
dispatch('DISCONNECT')
|
|
|
|
} else if (error.logRef) {
|
|
|
|
// Errors that have produced logs
|
|
|
|
router.push({ name: 'tool-log', params: { name: error.logRef } })
|
|
|
|
} else {
|
|
|
|
// Display the error in a modal on the current view.
|
|
|
|
commit('SET_ERROR', error)
|
2020-10-13 20:40:32 +02:00
|
|
|
}
|
2021-02-11 15:22:42 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'DELETE_ERROR' ({ commit }) {
|
|
|
|
commit('SET_ERROR', null)
|
2020-08-27 18:30:45 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getters: {
|
2020-10-12 15:18:57 +02:00
|
|
|
host: state => state.host,
|
2020-08-27 18:30:45 +02:00
|
|
|
connected: state => (state.connected),
|
2020-10-09 18:02:22 +02:00
|
|
|
yunohost: state => (state.yunohost),
|
2020-10-13 20:40:32 +02:00
|
|
|
error: state => state.error,
|
2020-10-09 22:05:23 +02:00
|
|
|
waiting: state => state.waiting,
|
2020-10-12 15:18:57 +02:00
|
|
|
history: state => state.history,
|
|
|
|
lastAction: state => state.history[state.history.length - 1]
|
2020-08-27 18:30:45 +02:00
|
|
|
}
|
|
|
|
}
|