import { createStore } from "vuex"; export default createStore({ state: { token: null, userId: null, authcd: null, stdId: null, schdlId: null, bookId: null, unitId: null, LearningId: null, currentLearningIds: [], prblmTypeId: null, currentLabel: null, currentProblemIndex: 0, }, getters: { isLoggedIn(state) { return !!state.token; }, getUserInfo(state) { return { userId: state.userId, authcd: state.authcd, }; }, getStdId(state) { return state.stdId; }, getSchdlId(state) { return state.schdlId; }, getBookId(state) { return state.bookId; }, getUnitId(state) { return state.unitId; }, getLearningId(state) { return state.learningId; }, currentLearningId(state) { return state.currentLearningIds[state.currentProblemIndex]; }, currentLabel: state => state.currentLabel, currentProblemIndex: state => state.currentProblemIndex, prblmTypeId: state => state.prblmTypeId, }, mutations: { setToken(state, token) { state.token = token; }, clearToken(state) { state.token = null; state.userId = null; state.authcd = null; state.stdId = null; state.schdlId = null; state.bookId = null; state.unitId = null; state.learningId = null; }, setUser(state, userId) { state.userId = userId; }, setAuthcd(state, authcd) { state.authcd = authcd; }, setStdId(state, stdId) { state.stdId = stdId; }, setSchdlId(state, schdlId) { state.schdlId = schdlId; }, setBookId(state, bookId) { state.bookId = bookId; }, setUnitId(state, unitId) { state.unitId = unitId; }, setLearningId(state, learningId) { state.learningId = learningId; }, 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; }, incrementProblemIndex(state) { if (state.currentProblemIndex < state.currentLearningIds.length - 1) { state.currentProblemIndex++; } }, decrementProblemIndex(state) { if (state.currentProblemIndex > 0) { state.currentProblemIndex--; } } }, actions: { login({ commit }, token) { commit("setToken", token); localStorage.setItem("token", token); }, logout({ commit }) { commit("clearToken"); localStorage.removeItem("token"); }, updateStdId({ commit }, payload) { return new Promise((resolve) => { commit("setStdId", payload); resolve(); }); }, updateSchdlId({ commit }, payload) { return new Promise((resolve) => { commit("setSchdlId", payload); resolve(); }); }, updateBookId({ commit }, payload) { return new Promise((resolve) => { commit("setBookId", payload); resolve(); }); }, updateUnitId({ commit }, payload) { return new Promise((resolve) => { commit("setUnitId", payload); resolve(); }); }, updateLearningId({ commit }, learningId) { commit("setLearningId", learningId); }, updateLearningData({ commit }, payload) { commit('setLearningData', payload); }, goToNextProblem({ commit }) { commit('incrementProblemIndex'); }, goToPreviousProblem({ commit }) { commit('decrementProblemIndex'); } }, });