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'
|
2021-04-09 21:43:56 +02:00
|
|
|
import i18n from '@/i18n'
|
|
|
|
import { timeout, isObjectLiteral } from '@/helpers/commons'
|
2020-08-27 18:30:45 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
state: {
|
2021-02-19 18:36:22 +01:00
|
|
|
host: window.location.host, // String
|
|
|
|
connected: localStorage.getItem('connected') === 'true', // Boolean
|
|
|
|
yunohost: null, // Object { version, repo }
|
|
|
|
waiting: false, // Boolean
|
|
|
|
history: [], // Array of `request`
|
|
|
|
requests: [], // Array of `request`
|
|
|
|
error: null // null || request
|
2020-08-27 18:30:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
mutations: {
|
2021-02-19 18:36:22 +01:00
|
|
|
'SET_CONNECTED' (state, boolean) {
|
|
|
|
localStorage.setItem('connected', boolean)
|
|
|
|
state.connected = boolean
|
2020-08-27 18:30:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'SET_YUNOHOST_INFOS' (state, yunohost) {
|
|
|
|
state.yunohost = yunohost
|
2020-10-09 18:02:22 +02:00
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
'SET_WAITING' (state, boolean) {
|
2020-10-09 18:02:22 +02:00
|
|
|
state.waiting = boolean
|
2020-10-12 15:18:57 +02:00
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
'ADD_REQUEST' (state, request) {
|
|
|
|
if (state.requests.length > 10) {
|
|
|
|
// We do not remove requests right after it resolves since an error might bring
|
|
|
|
// one back to life but we can safely remove some here.
|
|
|
|
state.requests.shift()
|
|
|
|
}
|
|
|
|
state.requests.push(request)
|
|
|
|
},
|
|
|
|
|
|
|
|
'UPDATE_REQUEST' (state, { request, key, value }) {
|
|
|
|
// This rely on data persistance and reactivity.
|
|
|
|
Vue.set(request, key, value)
|
2021-02-15 15:21:26 +01:00
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
'REMOVE_REQUEST' (state, request) {
|
|
|
|
const index = state.requests.lastIndexOf(request)
|
|
|
|
state.requests.splice(index, 1)
|
2020-10-12 15:18:57 +02:00
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
'ADD_HISTORY_ACTION' (state, request) {
|
|
|
|
state.history.push(request)
|
2020-10-12 15:18:57 +02:00
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
'ADD_MESSAGE' (state, { message, type }) {
|
|
|
|
const request = state.history[state.history.length - 1]
|
|
|
|
request.messages.push(message)
|
|
|
|
if (['error', 'warning'].includes(type)) {
|
|
|
|
request[type + 's']++
|
|
|
|
}
|
2020-10-13 20:40:32 +02:00
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
'SET_ERROR' (state, request) {
|
|
|
|
if (request) {
|
|
|
|
state.error = request
|
|
|
|
} else {
|
|
|
|
state.error = null
|
|
|
|
}
|
2020-08-27 18:30:45 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2021-02-19 18:36:22 +01: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.
|
|
|
|
// FIXME need testing with api not responding
|
|
|
|
return timeout(api.get('installed'), 5000).then(({ installed }) => {
|
|
|
|
return installed
|
|
|
|
}).catch(err => {
|
|
|
|
if (retry > 0) {
|
|
|
|
return dispatch('CHECK_INSTALL', --retry)
|
|
|
|
}
|
|
|
|
throw err
|
2020-09-27 15:18:07 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01: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
|
|
|
},
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
'DISCONNECT' ({ dispatch }, route = router.currentRoute) {
|
2020-10-13 20:40:32 +02:00
|
|
|
dispatch('RESET_CONNECTED')
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
'LOGIN' ({ dispatch }, password) {
|
2021-04-09 21:47:19 +02:00
|
|
|
return api.post('login', { password }, null, { websocket: false }).then(() => {
|
2021-02-19 18:36:22 +01:00
|
|
|
dispatch('CONNECT')
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
'LOGOUT' ({ dispatch }) {
|
|
|
|
dispatch('DISCONNECT')
|
|
|
|
return api.get('logout')
|
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
|
|
|
},
|
|
|
|
|
2021-04-09 21:43:56 +02:00
|
|
|
'INIT_REQUEST' ({ commit }, { method, uri, humanKey, initial, wait, websocket }) {
|
|
|
|
// Try to find a description for an API route to display in history and modals
|
|
|
|
const { key, ...args } = isObjectLiteral(humanKey) ? humanKey : { key: humanKey }
|
|
|
|
const humanRoute = key ? i18n.t('human_routes.' + key, args) : `[${method}] /${uri}`
|
|
|
|
|
|
|
|
let request = { method, uri, humanRoute, initial, status: 'pending' }
|
2021-02-19 18:36:22 +01:00
|
|
|
if (websocket) {
|
|
|
|
request = { ...request, messages: [], date: Date.now(), warnings: 0, errors: 0 }
|
|
|
|
commit('ADD_HISTORY_ACTION', request)
|
|
|
|
}
|
|
|
|
commit('ADD_REQUEST', request)
|
|
|
|
if (wait) {
|
|
|
|
setTimeout(() => {
|
|
|
|
// Display the waiting modal only if the request takes some time.
|
|
|
|
if (request.status === 'pending') {
|
|
|
|
commit('SET_WAITING', true)
|
|
|
|
}
|
|
|
|
}, 400)
|
|
|
|
}
|
2020-10-09 22:05:23 +02:00
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
return request
|
2020-10-12 15:18:57 +02:00
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
'END_REQUEST' ({ commit }, { request, success, wait }) {
|
|
|
|
let status = success ? 'success' : 'error'
|
|
|
|
if (success && (request.warnings || request.errors)) {
|
2021-04-10 14:16:47 +02:00
|
|
|
const messages = request.messages
|
|
|
|
if (messages.length && messages[messages.length - 1].color === 'warning') {
|
|
|
|
request.showWarningMessage = true
|
|
|
|
}
|
2021-02-19 18:36:22 +01:00
|
|
|
status = 'warning'
|
|
|
|
}
|
|
|
|
|
|
|
|
commit('UPDATE_REQUEST', { request, key: 'status', value: status })
|
2021-04-10 14:16:47 +02:00
|
|
|
if (wait && !request.showWarningMessage) {
|
2021-02-19 18:36:22 +01:00
|
|
|
// Remove the overlay after a short delay to allow an error to display withtout flickering.
|
|
|
|
setTimeout(() => {
|
|
|
|
commit('SET_WAITING', false)
|
|
|
|
}, 100)
|
2021-02-15 15:21:26 +01:00
|
|
|
}
|
2020-10-12 15:18:57 +02:00
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
'DISPATCH_MESSAGE' ({ commit }, { request, messages }) {
|
2020-10-12 15:18:57 +02:00
|
|
|
for (const type in messages) {
|
|
|
|
const message = {
|
2021-05-01 12:50:15 +02:00
|
|
|
text: messages[type].replace('\n', '<br>'),
|
2021-02-19 18:36:22 +01:00
|
|
|
color: type === 'error' ? 'danger' : type
|
2020-10-12 15:18:57 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2021-02-19 18:36:22 +01:00
|
|
|
commit('UPDATE_REQUEST', { request, key: 'progress', value: Object.values(progress) })
|
2020-10-12 15:18:57 +02:00
|
|
|
}
|
|
|
|
if (message.text) {
|
2021-02-19 18:36:22 +01:00
|
|
|
commit('ADD_MESSAGE', { request, message, type })
|
2020-10-12 15:18:57 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-13 20:40:32 +02:00
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
'HANDLE_ERROR' ({ commit, dispatch }, error) {
|
2021-02-11 15:22:42 +01:00
|
|
|
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 {
|
2021-02-19 18:36:22 +01:00
|
|
|
// The request is temporarely stored in the error for reference, but we reverse
|
|
|
|
// the ownership to stay generic.
|
|
|
|
const request = error.request
|
|
|
|
delete error.request
|
|
|
|
Vue.set(request, 'error', error)
|
2021-02-11 15:22:42 +01:00
|
|
|
// Display the error in a modal on the current view.
|
2021-02-19 18:36:22 +01:00
|
|
|
commit('SET_ERROR', request)
|
2020-10-13 20:40:32 +02:00
|
|
|
}
|
2021-02-11 15:22:42 +01:00
|
|
|
},
|
|
|
|
|
2021-02-19 18:36:22 +01:00
|
|
|
'REVIEW_ERROR' ({ commit }, request) {
|
|
|
|
request.review = true
|
|
|
|
commit('SET_ERROR', request)
|
|
|
|
},
|
|
|
|
|
|
|
|
'DISMISS_ERROR' ({ commit, state }, { initial, review = false }) {
|
|
|
|
if (initial && !review) {
|
|
|
|
// In case of an initial request (data that is needed by a view to render itself),
|
|
|
|
// try to go back so the user doesn't get stuck at a never ending skeleton view.
|
|
|
|
if (history.length > 2) {
|
|
|
|
history.back()
|
|
|
|
} else {
|
|
|
|
// if the url was opened in a new tab, return to home
|
|
|
|
router.push({ name: 'home' })
|
|
|
|
}
|
|
|
|
}
|
2021-02-11 15:22:42 +01:00
|
|
|
commit('SET_ERROR', null)
|
2021-04-10 14:16:47 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'DISMISS_WARNING' ({ commit, state }, request) {
|
|
|
|
commit('SET_WAITING', false)
|
|
|
|
delete request.showWarningMessage
|
2020-08-27 18:30:45 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getters: {
|
2020-10-12 15:18:57 +02:00
|
|
|
host: state => state.host,
|
2021-02-19 18:36:22 +01:00
|
|
|
connected: state => state.connected,
|
|
|
|
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,
|
2021-02-19 18:36:22 +01:00
|
|
|
lastAction: state => state.history[state.history.length - 1],
|
|
|
|
currentRequest: state => {
|
|
|
|
const request = state.requests.find(({ status }) => status === 'pending')
|
|
|
|
return request || state.requests[state.requests.length - 1]
|
|
|
|
}
|
2020-08-27 18:30:45 +02:00
|
|
|
}
|
|
|
|
}
|