From 00bbf8eeb6c7687692a514022256ccdbd7b4247e Mon Sep 17 00:00:00 2001 From: vlad_haleta Date: Sun, 26 Jun 2022 17:17:21 +0300 Subject: [PATCH] counter bug --- src/services/questionsThunk.ts | 7 ++++++- src/store/reducers/counterOfAnsferSlice.ts | 19 +++++++++++++++++++ src/store/reducers/routingSlice.ts | 5 +++-- src/store/store.ts | 2 ++ src/types/model.ts | 6 ++++++ 5 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/store/reducers/counterOfAnsferSlice.ts diff --git a/src/services/questionsThunk.ts b/src/services/questionsThunk.ts index 42fcabd..08cb2c1 100644 --- a/src/services/questionsThunk.ts +++ b/src/services/questionsThunk.ts @@ -1,4 +1,5 @@ import { data } from "store/data"; +import { setCounter } from "store/reducers/counterOfAnsferSlice"; import { setCurrentContent, setCurrentQuestion, @@ -30,18 +31,22 @@ export const chooseCurrentQuestion = dispatch(switchCorrectVisability(false)); const questionsList = getState().questions.list.ru; + let counterQuestions = getState().counter; const watchedList = questionsList?.filter((question) => !question.watched); const watchedListLength = watchedList?.length; + counterQuestions += 1; + // dispatch(setCounter(counterQuestions)); + console.log(setCounter(counterQuestions)); if (!!watchedListLength && watchedList && questionsList) { const random = randomNumber(0, watchedListLength - 1); const randomQuestion = watchedList?.[random]; - dispatch( setQuestions(checkCurrentQuestion(questionsList, randomQuestion)) ); dispatch(setCurrentQuestion(randomQuestion)); dispatch(setCurrentContent("questions")); + // console.log(setCurrentContent("questions")); localStorage.setItem( APP_NAME, JSON.stringify(checkCurrentQuestion(questionsList, randomQuestion)) diff --git a/src/store/reducers/counterOfAnsferSlice.ts b/src/store/reducers/counterOfAnsferSlice.ts new file mode 100644 index 0000000..a11fe81 --- /dev/null +++ b/src/store/reducers/counterOfAnsferSlice.ts @@ -0,0 +1,19 @@ +/* eslint-disable no-param-reassign */ +import { createSlice, PayloadAction } from "@reduxjs/toolkit"; +import { IStore } from "types/model"; + +const initialState: IStore["counter"] = 1; + +export const counterSlice = createSlice({ + name: "counter", + initialState, + reducers: { + setCounter: (state, action: PayloadAction) => { + state = action.payload; + }, + }, +}); + +export const { setCounter } = counterSlice.actions; + +export default counterSlice.reducer; diff --git a/src/store/reducers/routingSlice.ts b/src/store/reducers/routingSlice.ts index 8939b44..f6cb568 100644 --- a/src/store/reducers/routingSlice.ts +++ b/src/store/reducers/routingSlice.ts @@ -1,6 +1,7 @@ /* eslint-disable no-param-reassign */ -import { createSlice } from "@reduxjs/toolkit"; -import { IStore } from "types/model"; + +import { createSlice, PayloadAction } from "@reduxjs/toolkit"; +import { DynamicContentTypes, IQuestionItem, IStore } from "types/model"; const initialState: IStore["routing"] = { currentPage: "quiz", diff --git a/src/store/store.ts b/src/store/store.ts index 2dac7d1..5f46405 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -2,11 +2,13 @@ import { combineReducers, configureStore } from "@reduxjs/toolkit"; import questionsSlice from "./reducers/questionsSlice"; import routingSlice from "./reducers/routingSlice"; import userSlice from "./reducers/userSlice"; +import counterOfAnsferSlice from "./reducers/counterOfAnsferSlice"; export const rootReducer = combineReducers({ user: userSlice, routing: routingSlice, questions: questionsSlice, + counter: counterOfAnsferSlice, }); export const setupStore = () => { diff --git a/src/types/model.ts b/src/types/model.ts index 804d697..f073c8b 100644 --- a/src/types/model.ts +++ b/src/types/model.ts @@ -16,6 +16,9 @@ export enum CodeLanguagesList { javascript = "javascript", react = "react", } +export enum counterList { + counter = "counter", +} export type OneOfQuestionModelType = keyof typeof QuestionModel; @@ -24,6 +27,8 @@ export type OneOfLanguageListType = keyof typeof LanguageList; export type OneOfPagesListType = keyof typeof PagesList; export type CodeLanguagesListType = keyof typeof CodeLanguagesList; + +// export type counterListType = keyof typeof counterList; interface ITestVariant { id: string; text: string; @@ -63,6 +68,7 @@ export interface IStore { openResetModal: boolean; isCorrectVisible: boolean; }; + counter: number; } export type DynamicContentTypes = "questions" | "answers" | "explanation";