Untitled

mail@pastecode.io avatar
unknown
javascript
2 months ago
2.8 kB
3
Indexable
Never
// Import necessary modules
import { createStore } from 'vuex';

// Create and export the Vuex store
export default createStore({
  state: {
    // Your global state properties
    isLoggedIn: false,
    userID: null,
    email: "",
    firstName: "",
    lastName: "",
    gender: "",
    dateOfBirth: "",
    nickname: "",
    avatar: "",
    aboutMe: "",
    NotificationCount: null, // New property for NotificationCount
  },
  mutations: {
    // Your global mutations
    setLoginStatus(state, status) {
      state.isLoggedIn = status;
    },
    setUserID(state, userID) {
      state.userID = userID;
      localStorage.setItem('userID', userID);
    },
    setEmail(state, email) {
      state.email = email;
    },
    setFirstName(state, first) {
      state.firstName = first;
    },
    setLastName(state, last) {
      state.lastName = last;
    },
    setGender(state, gender) {
      state.gender = gender;
    },
    setDoB(state, DoB) {
      state.dateOfBirth = DoB;
    },
    setNickname(state, nickname) {
      state.nickname = nickname;
    },
    setAvatar(state, avatar) {
      state.avatar = avatar;
    },
    setAboutMe(state, about) {
      state.aboutMe = about;
    },
    setNotificationCount(state, count) { // New mutation for NotificationCount
      state.NotificationCount = count;
    },
    resetState(state) {
      // Reset all state properties to their initial values
      state.isLoggedIn = false;
      state.userID = null;
      state.email = "";
      state.firstName = "";
      state.lastName = "";
      state.gender = "";
      state.dateOfBirth = "";
      state.nickname = "";
      state.avatar = "";
      state.aboutMe = "";
      state.NotificationCount = null;
    },
  },
  actions: {
    // Your global actions
    setUser(context, data) {
      context.commit('setUserID', data);
    },
    setNotificationCount(context, count) { // New action for NotificationCount
      context.commit('setNotificationCount', count);
    },
    resetState(context) {
      context.commit('resetState');
    },
  },
  getters: {
    // Your global getters
    getLoginStatus(state) {
      return state.isLoggedIn;
    },
    getUserID(state) {
      // Check if state.userID is empty or undefined
      if (!state.userID) {
        // If empty, return userID from localStorage
        const storedUserID = localStorage.getItem('userID');
        return storedUserID || null; // You can return null or any default value if localStorage is also empty
      }

      // If state.userID is not empty, return it
      return state.userID;
    },
    getNotificationCount(state) { // New getter for NotificationCount
      return state.NotificationCount;
    },
  },
});
Leave a Comment