All files / src/stores intro.store.ts

100% Statements 16/16
100% Branches 4/4
100% Functions 4/4
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36      4x   4x   8x   8x   6x     2x 2x       8x 6x 6x 3x 3x   1x 1x     3x       8x    
import { defineStore } from 'pinia';
import { computed, onMounted, ref } from 'vue';
 
const DONT_SHOW_AGAIN_KEY = 'dontShowAgain';
 
export const useIntroStore = defineStore('intro', () => {
  // Backing ref is required for reactivity to work with localStorage
  const _dontShowAgain = ref<boolean>(false);
 
  const dontShowAgain = computed({
    get(): boolean {
      return _dontShowAgain.value;
    },
    set(value: boolean): void {
      _dontShowAgain.value = value;
      localStorage.setItem(DONT_SHOW_AGAIN_KEY, JSON.stringify(value));
    },
  });
 
  onMounted(() => {
    const value = localStorage.getItem(DONT_SHOW_AGAIN_KEY);
    if (value !== null && value !== undefined) {
      try {
        _dontShowAgain.value = JSON.parse(value) as boolean;
      } catch (error) {
        console.error('Error parsing stored value:', error);
        _dontShowAgain.value = false;
      }
    } else {
      _dontShowAgain.value = false;
    }
  });
 
  return { dontShowAgain };
});