2020-09-27 15:18:07 +02:00
|
|
|
import api, { timeout } from '@/helpers/api'
|
|
|
|
import router from '@/router'
|
2020-08-27 18:30:45 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
state: {
|
|
|
|
connected: localStorage.getItem('connected') === 'true',
|
|
|
|
yunohost: null // yunohost app infos: Object {version, repo}
|
|
|
|
},
|
|
|
|
|
|
|
|
mutations: {
|
|
|
|
'SET_CONNECTED' (state, connected) {
|
|
|
|
localStorage.setItem('connected', connected)
|
|
|
|
state.connected = connected
|
|
|
|
},
|
|
|
|
|
|
|
|
'SET_YUNOHOST_INFOS' (state, yunohost) {
|
|
|
|
state.yunohost = yunohost
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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-09-27 15:18:07 +02:00
|
|
|
'DISCONNECT' ({ commit }, route) {
|
2020-08-27 18:30:45 +02:00
|
|
|
commit('SET_CONNECTED', false)
|
|
|
|
commit('SET_YUNOHOST_INFOS', null)
|
2020-09-27 15:18:07 +02:00
|
|
|
// Do not redirect if the current route is `login` so the view can display an error.
|
|
|
|
if (router.currentRoute.name === 'login') return
|
|
|
|
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.
|
|
|
|
return timeout(api.get('installed'), 5000).then(({ installed }) => {
|
|
|
|
return installed
|
|
|
|
}).catch(err => {
|
|
|
|
if (retry > 0) {
|
|
|
|
return dispatch('CHECK_INSTALL', --retry)
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
})
|
2020-08-27 18:30:45 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getters: {
|
|
|
|
connected: state => (state.connected),
|
|
|
|
yunohost: state => (state.yunohost)
|
|
|
|
}
|
|
|
|
}
|