centralize a bit moar app init stuff (installed/connected)

This commit is contained in:
axolotle 2024-02-09 04:08:35 +01:00
parent 10f4fb86b5
commit 237b36e2e2
6 changed files with 39 additions and 37 deletions

View file

@ -112,13 +112,7 @@ export default {
// This hook is only triggered at page first load
created () {
// From this hook the value of `connected` always come from the localStorage.
// This state may be `true` but session may have expired, by querying
// yunohost infos, api may respond with `Unauthorized` in which case the `connected`
// state will be automaticly reseted and user will be prompt with the login view.
if (this.connected) {
this.$store.dispatch('GET_YUNOHOST_INFOS')
}
this.$store.dispatch('ON_APP_CREATED')
},
mounted () {

View file

@ -35,6 +35,10 @@ router.beforeEach((to, from, next) => {
if (store.getters.error) {
store.dispatch('DISMISS_ERROR', true)
}
if (to.name === 'post-install' && store.getters.installed) {
return next('/')
}
// Allow if connected or route is not protected
if (store.getters.connected || to.meta.noAuth) {
next()

View file

@ -7,6 +7,7 @@ import { timeout, isEmptyValue, isObjectLiteral } from '@/helpers/commons'
export default {
state: {
host: window.location.host, // String
installed: null,
connected: localStorage.getItem('connected') === 'true', // Boolean
yunohost: null, // Object { version, repo }
waiting: false, // Boolean
@ -22,6 +23,10 @@ export default {
},
mutations: {
'SET_INSTALLED' (state, boolean) {
state.installed = boolean
},
'SET_CONNECTED' (state, boolean) {
localStorage.setItem('connected', boolean)
state.connected = boolean
@ -106,23 +111,37 @@ export default {
},
actions: {
'CHECK_INSTALL' ({ dispatch }, retry = 2) {
async 'ON_APP_CREATED' ({ dispatch, state }) {
await dispatch('CHECK_INSTALL')
if (!state.installed) {
router.push({ name: 'post-install' })
} else {
dispatch('CONNECT')
}
},
async 'CHECK_INSTALL' ({ dispatch, commit }, 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 }) => {
try {
const { installed } = await timeout(api.get('installed'), 5000)
commit('SET_INSTALLED', installed)
return installed
}).catch(err => {
} catch (err) {
if (retry > 0) {
return dispatch('CHECK_INSTALL', --retry)
}
throw err
})
}
},
'CONNECT' ({ commit, dispatch }) {
async 'CONNECT' ({ commit, dispatch }) {
// If the user is not connected, the first action will throw
// and login prompt will be shown automaticly
await dispatch('GET_YUNOHOST_INFOS')
commit('SET_CONNECTED', true)
dispatch('GET_YUNOHOST_INFOS')
},
'RESET_CONNECTED' ({ commit }) {
@ -350,6 +369,7 @@ export default {
getters: {
host: state => state.host,
installed: state => state.installed,
connected: state => state.connected,
yunohost: state => state.yunohost,
error: state => state.error,

View file

@ -13,7 +13,7 @@
<template #buttons>
<b-button
type="submit" variant="success"
:disabled="disabled" form="ynh-form"
:disabled="!installed" form="ynh-form"
>
{{ $t('login') }}
</b-button>
@ -22,6 +22,7 @@
</template>
<script>
import { mapGetters } from 'vuex'
import { validationMixin } from 'vuelidate'
import { alphalownumdot_, required, minLength } from '@/helpers/validators'
@ -31,13 +32,11 @@ export default {
mixins: [validationMixin],
props: {
skipInstallCheck: { type: Boolean, default: false },
forceReload: { type: Boolean, default: false }
},
data () {
return {
disabled: !this.skipInstallCheck,
serverError: '',
form: {
username: '',
@ -63,6 +62,10 @@ export default {
}
},
computed: {
...mapGetters(['installed'])
},
validations () {
return {
form: {
@ -86,17 +89,6 @@ export default {
this.serverError = this.$i18n.t('wrong_password_or_username')
})
}
},
created () {
if (this.skipInstallCheck) return
this.$store.dispatch('CHECK_INSTALL').then(installed => {
if (installed) {
this.disabled = false
} else {
this.$router.push({ name: 'post-install' })
}
})
}
}
</script>

View file

@ -75,7 +75,7 @@
<p class="alert alert-success">
<icon iname="thumbs-up" /> {{ $t('installation_complete') }}
</p>
<login skip-install-check />
<login />
</template>
</div>
</template>
@ -201,14 +201,6 @@ export default {
confirmation: { required, passwordMatch: sameAs('password') }
}
}
},
created () {
this.$store.dispatch('CHECK_INSTALL').then(installed => {
if (installed) {
this.$router.push({ name: 'home' })
}
})
}
}
</script>

View file

@ -29,7 +29,7 @@
<template v-if="status === 'success'">
<b-alert variant="success" v-t="'api.reconnecting.success'" />
<login-view skip-install-check force-reload />
<login-view force-reload />
</template>
</b-card-body>
</template>