jichoi / lms_front star
박민혁 박민혁 2024-08-23
240823 박민혁 store local Storage 저장 / 로그아웃 시 데이터 삭제
@b363c1c7aeec408730ec43c5bb51d9f9c7e4efc0
client/views/layout/Header.vue
--- client/views/layout/Header.vue
+++ client/views/layout/Header.vue
@@ -133,6 +133,8 @@
         return;
       }
       localStorage.removeItem('token');
+      localStorage.removeItem("vuexState");
+
       this.goToPage('login');
     },
     goToPage(page) {
client/views/pages/AppStore.js
--- client/views/pages/AppStore.js
+++ client/views/pages/AppStore.js
@@ -1,28 +1,40 @@
 import { createStore } from "vuex";
 
+// Local storage에 상태를 저장하는 함수
+function saveStateToLocalStorage(state) {
+    localStorage.setItem('vuexState', JSON.stringify(state));
+  }
+
+// Local storage에서 상태를 불러오는 함수
+function loadStateFromLocalStorage() {
+    const savedState = localStorage.getItem('vuexState');
+    if (savedState) {
+      return JSON.parse(savedState);
+    }
+    return {
+      token: null,
+      userId: null,
+      authcd: null,
+      stdId: null,
+      schdlId: null,
+      bookId: null,
+      unitId: null,
+      unitNm: null,
+      LearningId: null,
+      currentLearningIds: [],
+      prblmTypeId: null,
+      currentLabel: null,
+      currentProblemIndex: 0,
+      textId: null,
+      wdBookIdList: [],
+      currentWdBkIndex: 0,
+    };
+  }
+  
+
 export default createStore({
-    state: {
-        token: null,
-        userId: null,
-        authcd: null,
-        stdId: null,
-        schdlId: null,
-        bookId: null,
-        unitId: null,
-        unitNm: null,
-        LearningId: null,
-        currentLearningIds: [],
-        prblmTypeId: null,
-        currentLabel: null,
-        currentProblemIndex: 0,
-        textId: null,
-        wdBookIdList: [],
-        currentWdBkIndex: 0,
-    },
+    state: loadStateFromLocalStorage(), 
     getters: {
-        isLoggedIn(state) {
-            return !!state.token;
-        },
         getUserInfo(state) {
             return {
                 userId: state.userId,
@@ -73,55 +85,70 @@
             state.bookId = null;
             state.unitId = null;
             state.learningId = null;
+            saveStateToLocalStorage(state);
         },
         setUser(state, userId) {
             state.userId = userId;
+            saveStateToLocalStorage(state);
         },
         setUserNm(state, userNm) {
             state.userNm = userNm;
+            saveStateToLocalStorage(state);
         },
         setAuthcd(state, authcd) {
             state.authcd = authcd;
+            saveStateToLocalStorage(state);
         },
         setStdId(state, stdId) {
             state.stdId = stdId;
+            saveStateToLocalStorage(state);
         },
         setSchdlId(state, schdlId) {
             state.schdlId = schdlId;
+            saveStateToLocalStorage(state);
         },
         setBookId(state, bookId) {
             state.bookId = bookId;
+            saveStateToLocalStorage(state);
         },
         setUnitId(state, unitId) {
             state.unitId = unitId;
+            saveStateToLocalStorage(state);
         },
         setLearningId(state, learningId) {
             state.learningId = learningId;
+            saveStateToLocalStorage(state);
         },
         setTextId(state, textId) {
             state.textId = textId;
+            saveStateToLocalStorage(state);
         },
         setLearningData(state, payload) {
             state.currentLearningIds = payload.learning_id; // Array of IDs
             state.currentLabel = payload.label;
             state.currentProblemIndex = 0; // Reset to the first problem
             state.prblmTypeId = payload.prblmTypeId;
+            saveStateToLocalStorage(state);
         },
         incrementProblemIndex(state) {
             if (state.currentProblemIndex < state.currentLearningIds.length - 1) {
                 state.currentProblemIndex++;
+                saveStateToLocalStorage(state);
             }
         },
         decrementProblemIndex(state) {
             if (state.currentProblemIndex > 0) {
                 state.currentProblemIndex--;
+                saveStateToLocalStorage(state);
             }
         },
         setWdBookIdList(state, wdBookIdList) {
             state.wdBookIdList = wdBookIdList;
+            saveStateToLocalStorage(state);
         },
         setCurrentWdBkIndex(state, currentWdBkIndex) {
             state.currentWdBkIndex = currentWdBkIndex;
+            saveStateToLocalStorage(state);
         }
     },
     actions: {
Add a comment
List