Seeking assistance with updating the DOM to display a loading element while a login screen is submitted and stopping it when errors occur. Struggling to figure out how to update the DOM when the promise resolves using Vue.js, Vuex, and Vuetify. Setting a variable on the state in vuex doesn't seem to be working as intended, as changing the state affects all loading elements visibility.
Tried various solutions and searched online but couldn't find examples matching our use case due to starting from a boilerplate code.
Here's the front-end code:
<template>
<div class="auth-page">
<div class="container page">
<div class="row">
<div class="col-md-6 offset-md-3 col-xs-12">
<h1 class="text-xs-center">Login</h1>
<p class="text-xs-center"></p>
<ul v-if="errors" class="error-messages">
<li v-for="(v, k) in errors" :key="k">{{ v.detail }}</li>
</ul>
<form v-on:submit.prevent="onSubmit(email, password);">
<fieldset class="form-group">
<input
class="form-control form-control-lg"
type="text"
v-model="email"
placeholder="Email"
/>
</fieldset>
<fieldset class="form-group">
<input
class="form-control form-control-lg"
type="password"
v-model="password"
placeholder="Password"
/>
</fieldset>
<v-progress-circular
right
:size="30"
color="black"
indeterminate
v-show="isLoading"
></v-progress-circular>
<button
class="btn btn-lg btn-primary pull-xs-right"
style="background:black; border-color:black"
>
Login
</button>
</form>
</div>
</div>
</div>
</div>
</template>
<script>
import { mapState } from "vuex";
import { LOGIN } from "@/store/actions.type";
export default {
name: "RwvLogin",
data() {
return {
email: null,
password: null,
isLoading: false
};
},
methods: {
onSubmit(email, password) {
this.isLoading = true;
this.$store
.dispatch(LOGIN, { email, password })
.then(() => this.$router.push({ name: "policies" }))
.catch((e) => {
console.log(e);
this.isLoading = false;
});
}
},
computed: {
...mapState({
errors: state => state.auth.errors
})
}
};
</script>
For the module code:
import ApiService from "@/common/api.service";
import JwtService from "@/common/jwt.service";
import {
LOGIN,
LOGOUT,
REGISTER,
CHECK_AUTH,
UPDATE_USER
} from "./actions.type";
import { SET_AUTH, PURGE_AUTH, SET_ERROR } from "./mutations.type";
const state = {
errors: null,
user: {},
isLoading: false,
isAuthenticated: !!JwtService.getToken()
};
const getters = {
currentUser(state) {
return state.user;
},
isAuthenticated(state) {
return state.isAuthenticated;
},
isLoading(state) {
return state.isLoading;
}
};
const actions = {
[LOGIN](context, credentials) {
return new Promise(resolve => {
ApiService.post("login", credentials)
.then(({ data }) => {
context.commit(SET_AUTH, data.access_token);
resolve(data);
})
.catch(({ response }) => {
if(response.status == 401){
response.data.errors = [{"status":"401","code":0,"title":"Loginn error","detail":"The password entered is incorrect."}]
}
context.commit(SET_ERROR, response.data.errors);
});
});
},
... (omitted for brevity)
mutations and exports follow...
When clicking submit, the LOGIN function interacts with the API and returns either wrong password or validation errors. Successfully displaying the errors.
On submission, this.isLoading = true;
and upon encountering an error, this.isLoading = false;
.
Looking for guidance on where to implement these changes. Any help would be appreciated!