Search code examples
javascriptvue.jscomponentsstorevuex

Component not updating on store change - Vuex


TLDR:

I recorded a video explaining my issue if that helps. https://youtu.be/Qf9Q4zIaox8

Navbar component is not updating on store change.

I have separated my component into two sections using a v-if and v-else. When the user is logged in the v-if shows and when the user is't logged in, the v-else shows.

When I login, server authenticates, and the client commits a mutation to the store and sets the user and client token. When this is set, it sets a property in the state called "userIsLoggedIn" to true - This property is computed in the navbar component. For reasons unknown, the Navbar component does not update once the user is logged in ( despite the isLoggedIn property being true) UNLESS I refresh the page.

Navbar Component:

<template>
    <div class="navbar">
        <div class="container">
            <div class="navbar-brand">
                <h1 class="navbar-item">
                    <router-link to="/" class="has-text-black">Logo</router-link>
                </h1>
            </div>

            <div class="navbar-menu">
                <div class="navbar-end" v-if="isLoggedIn">
                    <div class="navbar-item">
                        <router-link to="/play" class="has-text-black">Play</router-link>
                    </div>
                    <div class="navbar-item">
                        {{user.balance.toFixed(2)}}
                    </div>
                    <div class="navbar-item has-dropdown is-hoverable">
                        <a class="navbar-link">
                            {{user.username}}
                        </a>

                        <div class="navbar-dropdown">
                            <div class="navbar-item">Profile</div>
                            <div class="navbar-item">Deposit</div>
                            <div class="navbar-item" @click="logout">Logout</div>
                        </div>
                    </div>
                    <div class="navbar-item">
                        FAQ
                    </div>
                </div>

                <div class="navbar-end" v-else>
                    <div class="navbar-item">
                        <router-link to="/login" class="has-text-black">Login</router-link>
                    </div>
                    <div class="navbar-item">
                        <router-link to="/register" class="has-text-black">Register</router-link>
                    </div>
                    <div class="navbar-item">FAQ</div>
                </div>
            </div>
        </div> <!-- container -->
    </div> <!-- navbar -->
</template>

<script>

import {mapGetters} from "vuex";
export default {
    name: "Navbar",
    data() {
        return {
            user: this.$store.state.user,
        }
    },
    computed: {
        ...mapGetters([
            "isLoggedIn",
        ])
    },

    methods: {
        logout() {
            this.$store.dispatch("setToken", null);
            this.$store.dispatch("setUser", null);
            this.$router.push({
                path: "/"
            })
        }
    }
}
</script>

Vuex Store:

import Vue from "vue"; 
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate"

Vue.use(Vuex);

export default new Vuex.Store({
    strict: true, 
    plugins: [
        createPersistedState()
    ],
    state: {
        token: null, 
        user: null, 
        isUserLoggedIn: false, 
    },
    getters: {
        isLoggedIn(state) {
            return state.isUserLoggedIn;
        }
    },
    
    mutations: {
        setToken(state, token) {
            state.token = token;
            if (token) {
                state.isUserLoggedIn = true;
            } else {
                state.isUserLoggedIn = false;
            }
        },
        setUser(state, user) {
            state.user = user; 
        }
    },
    actions: {
        setToken ({commit}, token) {
            commit("setToken", token);
        },
        setUser ({commit}, user) {
            commit("setUser", user);
        }
    }
})

Login Component:

<template>
  <div class="hero is-large">
      <div class="container">
          <div class="hero-body">
            
              <div class="field">
                  <div class="notification is-warning" v-show="error">{{error}}</div>
              </div>

              <h1 class="title">Login</h1>


              <div class="field">
                  <label for="username" class="name">Username</label>
                  <input type="text" class="input" name="username" v-model="username">
              </div>

              <div class="field">
                  <label for="username" class="name">Password</label>
                  <input type="password" class="input" name="password" v-model="password">
              </div>

              <div class="field">
                  <button class="button is-link"
                          @click="login"
                          v-bind:class="{'is-loading': pending}">Login</button>
              </div>
          </div>
      </div>
  </div>
</template>

<script>

import AuthenticationService from "../Services/AuthenticationService";

export default {
  name: "Login", 

  data () {
      return {
          username: "",
          password: "", 
          error: null, 
          pending: false, 
      }
  }, 
  methods: {
      async login () {
          try {
            // SET LOADING SPINNER
            this.pending = true; 

            // API REQUEST
            const response = await AuthenticationService.login({
                username: this.username,
                password: this.password, 
            });

            console.log(response);

            // CLEAR FORM
            this.pending = false; 
            this.username = "";
            this.password = ""; 

            this.$store.dispatch("setToken", response.data.token);
            this.$store.dispatch("setUser", response.data.user);

            this.$router.push({
                name: "Landing"
            })
          } catch (error) {
              console.log(error);
          }
      }
  }
}
</script>


Solution

  • I think i found what is wrong.When the user logged in you did not set the isUserLoggedIn to true.So make an action and then a mutation to change to isUserLoggedIn to true when the user logged in successfully.

    Action

    setUserLogged ({commit}) {
     commit("setUserLogged", true);
    }
    

    Mutation

    setUserLogged(state, payload) {
     state.isUserLoggedIn = true
    }
    

    Then in Login component in those lines change this:

    this.$store.dispatch("setToken", response.data.token);
    this.$store.dispatch("setUser", response.data.user);
    

    to this:

    this.$store.dispatch("setToken", response.data.token);
    this.$store.dispatch("setUser", response.data.user);
    this.$store.dispatch("setUserLogged");