I desire for the state of my Vue 2 app to remain consistent across all tabs, even when one tab is not in focus and the state is updated

Snippet of code in my template

 <!-- Toggle Switch Button for online/offline status -->
    <div v-if="user.role=== 'accountant' || user.role=== 'supervisor'">
      <v-chip v-if="onlineStatusValue" color="green" class=" d-flex row-pointer" small dense @click="statusUpdated()">
        <v-icon x-small color="green darken-4">mdi-circle-medium</v-icon>
        <p class="white--text font-style-paragraph pl-2 pt-1">Online</p>
      </v-chip>
      <v-chip v-else color="red" class=" d-flex row-pointer" small dense @click="statusUpdated()">
        <v-icon x-small color="red darken-4">mdi-circle-medium</v-icon>
        <p class="white--text font-style-paragraph pl-2 pt-1">Offline</p>
      </v-chip>
  </div>

Function responsible for updating the state

  methods: {
    /**
     * This method checks and updates the user's online/offline status
     * Socket integration is used within this method
     **/
    statusUpdated() {
      clearTimeout(this.userStatusInterval)
      if (this.onlineStatusValue === false) {
        if(this.role === 'supervisor'){
           window.open("/supervisor-ticketpool");
          // this.$router.push("/supervisor-ticketpool")
        }
        socket.emit("userConnected", this.user._id);
        this.userStatusInterval = setTimeout(function () {
          EventBus.$emit('user-status-online');
        }, 1000);
      }
      else{
        if(this.role === 'supervisor'){
          this.$router.push("/");
        }
        socket.emit("userDisconnected", this.user._id);
      }
 /* ----> Updates the online Status  */  
        this.$store.commit("auth/setOnlineStatus", !this.onlineStatusValue);
    },
}

The current method successfully updates the state, however, it only affects the tab that is currently focused. In case of multiple open tabs, any changes made to the state on the active tab will not be reflected on the other tabs which are inactive. I would like all unfocused tabs to also have their this.onlineStatusValue updated whenever there is a change in the state of this.onlineStatusValue on the active tab, ensuring consistency across all tabs.

Answer №1

This situation might benefit from implementing state management for easier access and manipulation of stored values across tabs. Take a look at Pinia for more information.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Methods for transferring checkbox values from JavaScript/view to controller in MVC

Currently, I am developing a form that allows users to select up to 11 football players from a list using checkboxes. The players are categorized and named by their playing positions. @if(item.PlayerPosition.FantasyFootball_PlayerToSeason.Select(x => x ...

Cross-origin resource sharing (CORS) is preventing access because the origin and allowed origin match

Utilizing Dockerized Ory Kratos and Angular (www folder hosted via nginx for header modifications) on localhost and attempting to run the following code: const headers = { Accept: 'application/json', }; fetch('http://127.0.0.1:4433/self-s ...

Implementing a parallax scroll effect using CSS and dynamically updating the background-position value with JavaScript

How can different Y position percentages be dynamically assigned to multiple layered background images in CSS using JavaScript so that they update as the page scrolls? <style> body { background-image: url(img/bg-pattern-01.png), ur ...

Try incorporating "vector.applyQuaternion" or a similar method in your code within ammo.js

I'm eager to develop a virtual reality shooting game for browsers, utilizing Three.js and Ammo.js for the physics engine and rigid bodies. Although I've successfully set up the VR headset, controllers, and loaded models, I encountered an issue wi ...

Calculations in JavaScript determined by responses provided in the form

I have recently developed a web form that includes various input fields and radio buttons. The purpose of this form is to collect answers from users for cost calculation. However, I am uncertain about the JavaScript logic, especially regarding the radio bu ...

Check if a specific number appears exactly once in an array and output either True or False

I am facing a challenge with comparing two arrays Array1 = [1,1,1,2,2,2,3,3] Array2 =[1,1,2,1] When comparing both arrays, the desired result is True if the number of occurrences of Integer 1 are the same. Array2 = [1,1,2] //Expecting False For the ab ...

Using Vue.js to connect v-html to a custom CSS stylesheet

I am currently working with HTML generated by a function on an external server and I am able to preview this within a tag. Additionally, I can retrieve the CSS information in a similar manner. <template> <div v-html="html"></div ...

The React Native Android app encountered an error loading the JS bundle: updates made to index.android.js are not reflecting in the generated APK file

Setting up a react-native android project on my Windows machine has been successful so far. To run the project, I use the command react-native run-android and then the installed apk is generated on my phone. After making some changes in the index.android. ...

The bot on Discord.js is sending an incorrect message when attempting to use the _leave command on the music bot

I have been following the tutorials from Plexi Development's YouTube channel to create a music bot. I have implemented the code, but whenever I try to use the _leave command, my bot refuses to leave the voice channel even though I am in the same chann ...

Update the content of a div and refresh it when a key on the keyboard is pressed

I need to hide the images in a div when I press a key on the keyboard. How can I achieve this? <div> <span role="checkbox" aria-checked="true" tabindex="0"> <img src="checked.gif" role="presentation" alt="" /> ...

The issue at hand is why the closure is not functioning properly when variables are assigned to the callback of the getCurrentLocation function

Apologies for the extensive amount of code, but it seems like there may be an issue with AppMobi's getCurrentLocation function in this scenario. The problem arises when tapping on list elements triggers an asynchronous getCurrentLocation call which up ...

How can I send an API request with a callback function as an argument using node.js?

If you want to access the getPlayers(callback) method, it is described as follows: getPlayers(callback) callback - This parameter is necessary. It will be called with an object of players players - An object that contains all the players who are curr ...

The SEMrush API is not displaying an 'Access-Control-Allow-Origin' header on the requested resource

When attempting to utilize the SEMrush API, I made a request using jQuery as shown below: $(document).ready(function() { $.get( 'https://api.semrush.com', { type: 'phrase_this', key: ' ...

Having trouble removing the npm package spotifydl from my system

After running the command npm install -g spotifydl, I discovered that the package was obsolete and no longer functioning properly. Subsequently, I attempted to remove it using npm uninstall -g spotifydl, but instead of completely uninstalling, it kept re ...

Utilize an external stylesheet for your Vue web component to avoid inline styles

For my web component, I am utilizing the vue-cli-service to handle the building process. All styles associated with this component are consistently inlined within the javascript file. To create the web component: vue-cli-service build --target wc --name co ...

How can one efficiently make API requests using Vuex?

Being new to both Vue Webpack and Vuex after transitioning from the Ember world, I have set up my Vue Webpack app with Vuex using vue-resource in two different files: /src/store/api.js import Vue from 'vue'; import { store } from './store& ...

What is the best way to combine elements from different arrays to create a comprehensive listing?

My current function successfully pulls data from another source to create a listing. However, the data I require is spread across multiple arrays, causing some items to be returned as "undefined." At the moment, I am only fetching data from the products a ...

Creating custom database query templates in Electron JS

Currently in the process of utilizing ElectronJS for the creation of a basic CRUD system that establishes a connection with an online database (MySQL is used to access data from the database). I have successfully logged the retrieved data to the console, ...

What is the local date format for the Ionic DatePicker?

I have successfully implemented a DatePicker in my Ionic Project, but the date is displaying in the wrong time format. Here is my function: showDatePicker(){ this.datePicker.show({ date: new Date(), mode: 'date', allowOldDates: fal ...

Place the input value within quotation marks when it consists of two or more words

My goal is to have the input value of React Async Select surrounded by quotes if the search term contains multiple words with spaces. const handleSearchText = (value) => { let split = value.split(' ') console.log('split', spl ...