
240823 박민혁 store local Storage 저장 / 로그아웃 시 데이터 삭제
@b363c1c7aeec408730ec43c5bb51d9f9c7e4efc0
--- client/views/layout/Header.vue
+++ client/views/layout/Header.vue
... | ... | @@ -133,6 +133,8 @@ |
133 | 133 |
return; |
134 | 134 |
} |
135 | 135 |
localStorage.removeItem('token'); |
136 |
+ localStorage.removeItem("vuexState"); |
|
137 |
+ |
|
136 | 138 |
this.goToPage('login'); |
137 | 139 |
}, |
138 | 140 |
goToPage(page) { |
--- client/views/pages/AppStore.js
+++ client/views/pages/AppStore.js
... | ... | @@ -1,28 +1,40 @@ |
1 | 1 |
import { createStore } from "vuex"; |
2 | 2 |
|
3 |
+// Local storage에 상태를 저장하는 함수 |
|
4 |
+function saveStateToLocalStorage(state) { |
|
5 |
+ localStorage.setItem('vuexState', JSON.stringify(state)); |
|
6 |
+ } |
|
7 |
+ |
|
8 |
+// Local storage에서 상태를 불러오는 함수 |
|
9 |
+function loadStateFromLocalStorage() { |
|
10 |
+ const savedState = localStorage.getItem('vuexState'); |
|
11 |
+ if (savedState) { |
|
12 |
+ return JSON.parse(savedState); |
|
13 |
+ } |
|
14 |
+ return { |
|
15 |
+ token: null, |
|
16 |
+ userId: null, |
|
17 |
+ authcd: null, |
|
18 |
+ stdId: null, |
|
19 |
+ schdlId: null, |
|
20 |
+ bookId: null, |
|
21 |
+ unitId: null, |
|
22 |
+ unitNm: null, |
|
23 |
+ LearningId: null, |
|
24 |
+ currentLearningIds: [], |
|
25 |
+ prblmTypeId: null, |
|
26 |
+ currentLabel: null, |
|
27 |
+ currentProblemIndex: 0, |
|
28 |
+ textId: null, |
|
29 |
+ wdBookIdList: [], |
|
30 |
+ currentWdBkIndex: 0, |
|
31 |
+ }; |
|
32 |
+ } |
|
33 |
+ |
|
34 |
+ |
|
3 | 35 |
export default createStore({ |
4 |
- state: { |
|
5 |
- token: null, |
|
6 |
- userId: null, |
|
7 |
- authcd: null, |
|
8 |
- stdId: null, |
|
9 |
- schdlId: null, |
|
10 |
- bookId: null, |
|
11 |
- unitId: null, |
|
12 |
- unitNm: null, |
|
13 |
- LearningId: null, |
|
14 |
- currentLearningIds: [], |
|
15 |
- prblmTypeId: null, |
|
16 |
- currentLabel: null, |
|
17 |
- currentProblemIndex: 0, |
|
18 |
- textId: null, |
|
19 |
- wdBookIdList: [], |
|
20 |
- currentWdBkIndex: 0, |
|
21 |
- }, |
|
36 |
+ state: loadStateFromLocalStorage(), |
|
22 | 37 |
getters: { |
23 |
- isLoggedIn(state) { |
|
24 |
- return !!state.token; |
|
25 |
- }, |
|
26 | 38 |
getUserInfo(state) { |
27 | 39 |
return { |
28 | 40 |
userId: state.userId, |
... | ... | @@ -73,55 +85,70 @@ |
73 | 85 |
state.bookId = null; |
74 | 86 |
state.unitId = null; |
75 | 87 |
state.learningId = null; |
88 |
+ saveStateToLocalStorage(state); |
|
76 | 89 |
}, |
77 | 90 |
setUser(state, userId) { |
78 | 91 |
state.userId = userId; |
92 |
+ saveStateToLocalStorage(state); |
|
79 | 93 |
}, |
80 | 94 |
setUserNm(state, userNm) { |
81 | 95 |
state.userNm = userNm; |
96 |
+ saveStateToLocalStorage(state); |
|
82 | 97 |
}, |
83 | 98 |
setAuthcd(state, authcd) { |
84 | 99 |
state.authcd = authcd; |
100 |
+ saveStateToLocalStorage(state); |
|
85 | 101 |
}, |
86 | 102 |
setStdId(state, stdId) { |
87 | 103 |
state.stdId = stdId; |
104 |
+ saveStateToLocalStorage(state); |
|
88 | 105 |
}, |
89 | 106 |
setSchdlId(state, schdlId) { |
90 | 107 |
state.schdlId = schdlId; |
108 |
+ saveStateToLocalStorage(state); |
|
91 | 109 |
}, |
92 | 110 |
setBookId(state, bookId) { |
93 | 111 |
state.bookId = bookId; |
112 |
+ saveStateToLocalStorage(state); |
|
94 | 113 |
}, |
95 | 114 |
setUnitId(state, unitId) { |
96 | 115 |
state.unitId = unitId; |
116 |
+ saveStateToLocalStorage(state); |
|
97 | 117 |
}, |
98 | 118 |
setLearningId(state, learningId) { |
99 | 119 |
state.learningId = learningId; |
120 |
+ saveStateToLocalStorage(state); |
|
100 | 121 |
}, |
101 | 122 |
setTextId(state, textId) { |
102 | 123 |
state.textId = textId; |
124 |
+ saveStateToLocalStorage(state); |
|
103 | 125 |
}, |
104 | 126 |
setLearningData(state, payload) { |
105 | 127 |
state.currentLearningIds = payload.learning_id; // Array of IDs |
106 | 128 |
state.currentLabel = payload.label; |
107 | 129 |
state.currentProblemIndex = 0; // Reset to the first problem |
108 | 130 |
state.prblmTypeId = payload.prblmTypeId; |
131 |
+ saveStateToLocalStorage(state); |
|
109 | 132 |
}, |
110 | 133 |
incrementProblemIndex(state) { |
111 | 134 |
if (state.currentProblemIndex < state.currentLearningIds.length - 1) { |
112 | 135 |
state.currentProblemIndex++; |
136 |
+ saveStateToLocalStorage(state); |
|
113 | 137 |
} |
114 | 138 |
}, |
115 | 139 |
decrementProblemIndex(state) { |
116 | 140 |
if (state.currentProblemIndex > 0) { |
117 | 141 |
state.currentProblemIndex--; |
142 |
+ saveStateToLocalStorage(state); |
|
118 | 143 |
} |
119 | 144 |
}, |
120 | 145 |
setWdBookIdList(state, wdBookIdList) { |
121 | 146 |
state.wdBookIdList = wdBookIdList; |
147 |
+ saveStateToLocalStorage(state); |
|
122 | 148 |
}, |
123 | 149 |
setCurrentWdBkIndex(state, currentWdBkIndex) { |
124 | 150 |
state.currentWdBkIndex = currentWdBkIndex; |
151 |
+ saveStateToLocalStorage(state); |
|
125 | 152 |
} |
126 | 153 |
}, |
127 | 154 |
actions: { |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?