Optimize API calls, added form loading logs

This commit is contained in:
Julien Nahum 2024-01-25 05:12:45 +01:00
parent eceaae17da
commit 2dbbc38ba4
6 changed files with 21 additions and 24 deletions

View File

@ -219,7 +219,7 @@ export default {
methods: {
async logout() {
// Log out the user.
await this.authStore.logout()
this.authStore.logout()
// Reset store
this.workspacesStore.resetState()

View File

@ -26,7 +26,7 @@
</div>
<!-- Submit Button -->
<v-button dusk="btn_login" :loading="form.busy">
<v-button dusk="btn_login" :loading="form.busy || loading">
Log in to continue
</v-button>
@ -72,6 +72,7 @@ export default {
email: '',
password: ''
}),
loading: false,
remember: false,
showForgotModal: false
}),
@ -79,23 +80,24 @@ export default {
methods: {
login() {
// Submit the form.
this.loading = true
this.form.post('login').then(async (data) => {
// Save the token.
this.authStore.setToken(data.token)
const userData = await opnFetch('user')
this.authStore.setUser(userData)
const workspaces = await fetchAllWorkspaces()
this.workspaceStore.set(workspaces.data.value)
const [userDataResponse, workspacesResponse] = await Promise.all([opnFetch('user'), fetchAllWorkspaces()]);
this.authStore.setUser(userDataResponse)
this.workspaceStore.set(workspacesResponse.data.value)
// Load forms
this.formsStore.loadAll(this.workspaceStore.currentId)
// Redirect home.
this.redirect()
}).catch(() => {
}).catch((error) => {
console.error(error)
}).finally(() => {
this.loading = false
})
},

View File

@ -1,16 +1,16 @@
import {fetchAllWorkspaces} from "~/stores/workspaces.js";
import {opnFetch} from "~/composables/useOpnApi.js";
export default defineNuxtRouteMiddleware(async(to, from) => {
const authStore = useAuthStore()
authStore.initStore(useCookie('token').value, useCookie('admin_token').value)
if (authStore.token && !authStore.user) {
const {data, error} = await useOpnApi('user')
authStore.setUser(data.value)
// Load workspaces
const workspaceStore = useWorkspacesStore()
const {data: workspacesData, error: workspacesError} = await fetchAllWorkspaces()
workspaceStore.save(workspacesData.value)
// Load user data and workspaces
const [userDataResponse, workspacesResponse] = await Promise.all([useOpnApi('user'), fetchAllWorkspaces()]);
authStore.setUser(userDataResponse.data.value)
workspaceStore.save(workspacesResponse.data.value)
}
})

View File

@ -92,6 +92,7 @@ const loadForm = async (setup=false) => {
if (setup) {
const {data, error} = await formsStore.publicLoad(slug)
if (error.value) {
console.error(`Error loading form [${slug}]:`,error.value)
formsStore.stopLoading()
return
}

View File

@ -35,10 +35,7 @@ const deleteAccount = () => {
loading = false
useAlert().success(data.message)
// Log out the user.
await authStore.logout()
// Redirect to login.
authStore.logout()
router.push({ name: 'login' })
}).catch((error) => {
useAlert().error(error.data.message)

View File

@ -75,11 +75,8 @@ export const useAuthStore = defineStore('auth', {
// })
},
async logout() {
try {
await useOpnApi('logout', {method: 'POST'})
} catch (e) {
}
logout() {
opnFetch('logout', {method: 'POST'}).catch((error) => {})
this.user = null
this.setToken(null)