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: { methods: {
async logout() { async logout() {
// Log out the user. // Log out the user.
await this.authStore.logout() this.authStore.logout()
// Reset store // Reset store
this.workspacesStore.resetState() this.workspacesStore.resetState()

View File

@ -26,7 +26,7 @@
</div> </div>
<!-- Submit Button --> <!-- Submit Button -->
<v-button dusk="btn_login" :loading="form.busy"> <v-button dusk="btn_login" :loading="form.busy || loading">
Log in to continue Log in to continue
</v-button> </v-button>
@ -72,6 +72,7 @@ export default {
email: '', email: '',
password: '' password: ''
}), }),
loading: false,
remember: false, remember: false,
showForgotModal: false showForgotModal: false
}), }),
@ -79,23 +80,24 @@ export default {
methods: { methods: {
login() { login() {
// Submit the form. // Submit the form.
this.loading = true
this.form.post('login').then(async (data) => { this.form.post('login').then(async (data) => {
// Save the token. // Save the token.
this.authStore.setToken(data.token) this.authStore.setToken(data.token)
const userData = await opnFetch('user') const [userDataResponse, workspacesResponse] = await Promise.all([opnFetch('user'), fetchAllWorkspaces()]);
this.authStore.setUser(userData) this.authStore.setUser(userDataResponse)
this.workspaceStore.set(workspacesResponse.data.value)
const workspaces = await fetchAllWorkspaces()
this.workspaceStore.set(workspaces.data.value)
// Load forms // Load forms
this.formsStore.loadAll(this.workspaceStore.currentId) this.formsStore.loadAll(this.workspaceStore.currentId)
// Redirect home. // Redirect home.
this.redirect() 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 {fetchAllWorkspaces} from "~/stores/workspaces.js";
import {opnFetch} from "~/composables/useOpnApi.js";
export default defineNuxtRouteMiddleware(async(to, from) => { export default defineNuxtRouteMiddleware(async(to, from) => {
const authStore = useAuthStore() const authStore = useAuthStore()
authStore.initStore(useCookie('token').value, useCookie('admin_token').value) authStore.initStore(useCookie('token').value, useCookie('admin_token').value)
if (authStore.token && !authStore.user) { if (authStore.token && !authStore.user) {
const {data, error} = await useOpnApi('user')
authStore.setUser(data.value)
// Load workspaces
const workspaceStore = useWorkspacesStore() 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) { if (setup) {
const {data, error} = await formsStore.publicLoad(slug) const {data, error} = await formsStore.publicLoad(slug)
if (error.value) { if (error.value) {
console.error(`Error loading form [${slug}]:`,error.value)
formsStore.stopLoading() formsStore.stopLoading()
return return
} }

View File

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

View File

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