backup
This commit is contained in:
73
site/src/quiz/features/quizSlice.tsx
Normal file
73
site/src/quiz/features/quizSlice.tsx
Normal file
@@ -0,0 +1,73 @@
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
import type {PayloadAction} from '@reduxjs/toolkit';
|
||||
|
||||
function shuffle<T>(array:Array<T>):Array<T> {
|
||||
let currentIndex = array.length;
|
||||
|
||||
// While there remain elements to shuffle...
|
||||
while (currentIndex != 0) {
|
||||
|
||||
// Pick a remaining element...
|
||||
let randomIndex = Math.floor(Math.random() * currentIndex);
|
||||
currentIndex--;
|
||||
|
||||
// And swap it with the current element.
|
||||
[array[currentIndex], array[randomIndex]] = [
|
||||
array[randomIndex], array[currentIndex]];
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
interface QuizState {
|
||||
userAnswers: string[][]; // хранит перемещаемые элементы каждого списка ответов
|
||||
correctAnswers:string[][] |boolean[][];// ответы для вопрсов, в случае с matching просто правильная последоавтельность, для sorting аналогично, для choosе последоватеьность boolean
|
||||
isTestingDone:boolean// запрет на взаимодействие с квизом после окончания тестирования, чтобю юзер не наглел
|
||||
}
|
||||
|
||||
const initialState: QuizState = {
|
||||
userAnswers: [],
|
||||
correctAnswers:[],
|
||||
isTestingDone:false
|
||||
};
|
||||
|
||||
const listsSlice = createSlice({
|
||||
name: 'lists',
|
||||
initialState,
|
||||
reducers: {
|
||||
addList: (state, action: PayloadAction<{index: number; items: string[]}>)=>{
|
||||
const { index, items } = action.payload;
|
||||
state.userAnswers.splice(index, 1, items); // с нулём создаётся по 2 экземпляра, видно в отладке
|
||||
state.correctAnswers.splice(index, 1, items);
|
||||
|
||||
},
|
||||
setDraggedItems: (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[] }>) => {
|
||||
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);
|
||||
})
|
||||
},
|
||||
stopTesting:(state)=>{
|
||||
state.isTestingDone=true;
|
||||
},
|
||||
startTesting:(state)=>{
|
||||
state.isTestingDone=false;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// Экспортируем действия и редьюсер
|
||||
export const { addList, setDraggedItems,mixUp,startTesting,stopTesting } = listsSlice.actions;
|
||||
export type {QuizState}
|
||||
export default listsSlice.reducer;
|
||||
Reference in New Issue
Block a user