refactored quiz slice, site loads
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
import type {PayloadAction} from '@reduxjs/toolkit';
|
||||
import type { PayloadAction } from '@reduxjs/toolkit';
|
||||
|
||||
function shuffle<T>(array:Array<T>):Array<T> {
|
||||
function shuffle<T>(array: Array<T>): Array<T> {
|
||||
let currentIndex = array.length;
|
||||
|
||||
// While there remain elements to shuffle...
|
||||
@@ -19,55 +19,60 @@ function shuffle<T>(array:Array<T>):Array<T> {
|
||||
}
|
||||
|
||||
interface QuizState {
|
||||
userAnswers: string[][]; // хранит перемещаемые элементы каждого списка ответов
|
||||
correctAnswers:string[][] |boolean[][];// ответы для вопрсов, в случае с matching просто правильная последоавтельность, для sorting аналогично, для choosе последоватеьность boolean
|
||||
isTestingDone:boolean// запрет на взаимодействие с квизом после окончания тестирования, чтобю юзер не наглел
|
||||
userAnswers: (string | boolean)[][]; // хранит перемещаемые элементы каждого списка ответов
|
||||
correctAnswers: (string | boolean)[][];// ответы для вопрсов, в случае с matching просто правильная последоавтельность, для sorting аналогично, для choosе последоватеьность boolean
|
||||
quizTypes: Array<"M" | "S" | "C">;
|
||||
isTestingDone: boolean// запрет на взаимодействие с квизом после окончания тестирования, чтобю юзер не наглел
|
||||
}
|
||||
|
||||
const initialState: QuizState = {
|
||||
userAnswers: [],
|
||||
correctAnswers:[],
|
||||
isTestingDone:false
|
||||
userAnswers: [],
|
||||
correctAnswers: [],
|
||||
quizTypes: [],
|
||||
isTestingDone: false,
|
||||
};
|
||||
|
||||
const listsSlice = createSlice({
|
||||
name: 'lists',
|
||||
initialState,
|
||||
reducers: {
|
||||
addList: (state, action: PayloadAction<{index: number; items: string[]}>)=>{
|
||||
const { index, items } = action.payload;
|
||||
addList: (state, action: PayloadAction<{ index: number; items: string[]; answers: (string|boolean)[]; quizType : "S" | "M" | "C"}>) => {
|
||||
const { index, items,answers,quizType} = action.payload;
|
||||
state.userAnswers.splice(index, 1, items); // с нулём создаётся по 2 экземпляра, видно в отладке
|
||||
state.correctAnswers.splice(index, 1, items);
|
||||
|
||||
state.quizTypes.splice(index, 1, quizType); //
|
||||
state.correctAnswers.splice(index, 1, answers);
|
||||
|
||||
},
|
||||
setDraggedItems: (state, action: PayloadAction<{ index: number; items: string[] }>) => {
|
||||
const { index, items } = action.payload;
|
||||
if (index >= 0 && index < state.userAnswers.length) {
|
||||
state.userAnswers[index] = items; // обновляем конкретный список
|
||||
}
|
||||
setDraggedItems: (state, action: PayloadAction<{ index: number; items: (string|boolean)[] }>) => {
|
||||
const { index, items } = action.payload;
|
||||
if (index >= 0 && index < state.userAnswers.length) {
|
||||
state.userAnswers[index] = items; // обновляем конкретный список
|
||||
}
|
||||
},
|
||||
setCheckedItems: (state, action: PayloadAction<{ index: number; items: string[] }>) => {
|
||||
const { index, items } = action.payload;
|
||||
if (index >= 0 && index < state.userAnswers.length) {
|
||||
state.userAnswers[index] = items; // обновляем конкретный список
|
||||
}
|
||||
setCheckedItems: (state, action: PayloadAction<{ index: number; items: (string|boolean)[] }>) => {
|
||||
const { index, items } = action.payload;
|
||||
if (index >= 0 && index < state.userAnswers.length) {
|
||||
state.userAnswers[index] = items; // обновляем конкретный список
|
||||
}
|
||||
},
|
||||
mixUp: (state) => {
|
||||
state.userAnswers=state.userAnswers.map((list)=>
|
||||
{
|
||||
return shuffle(list);
|
||||
})
|
||||
state.userAnswers = state.userAnswers.map((list) => {
|
||||
if(typeof(list[0])==="boolean"){
|
||||
return list.map(()=>false);
|
||||
}
|
||||
return shuffle<string|boolean>(list);
|
||||
})
|
||||
},
|
||||
stopTesting:(state)=>{
|
||||
state.isTestingDone=true;
|
||||
stopTesting: (state) => {
|
||||
state.isTestingDone = true;
|
||||
},
|
||||
startTesting:(state)=>{
|
||||
state.isTestingDone=false;
|
||||
startTesting: (state) => {
|
||||
state.isTestingDone = false;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// Экспортируем действия и редьюсер
|
||||
export const { addList, setDraggedItems,mixUp,startTesting,stopTesting } = listsSlice.actions;
|
||||
export type {QuizState}
|
||||
export const { addList, setDraggedItems, mixUp, startTesting, stopTesting } = listsSlice.actions;
|
||||
export type { QuizState }
|
||||
export default listsSlice.reducer;
|
||||
Reference in New Issue
Block a user