copied tesing folder to the main site
This commit is contained in:
52
site/testing/features/Matching.tsx
Normal file
52
site/testing/features/Matching.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
import { Grid, List, ListItem, ListItemButton, ListItemText } from '@mui/material';
|
||||
import type {tTasks} from "../quizData"
|
||||
import SortableList from '../features/SortableList';
|
||||
import { useEffect } from 'react';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { addList,mixUp } from './quizSlice';
|
||||
interface ComponentProps {
|
||||
tasks: tTasks;
|
||||
index: number;
|
||||
}
|
||||
|
||||
function Matching({tasks,index}: ComponentProps) {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
// Добавляем список ответов очередного задания в хранилище
|
||||
useEffect(() => {
|
||||
dispatch(addList({ index, items: answers }));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(mixUp());
|
||||
}, []);
|
||||
|
||||
|
||||
const answers: string[] = tasks.map((item) => item.answer);
|
||||
return (
|
||||
<Grid container spacing={2}>
|
||||
<Grid size={6}>
|
||||
<List>
|
||||
{tasks.map((item, index) => (
|
||||
<ListItem key={index}>
|
||||
<ListItemButton
|
||||
sx={{
|
||||
border: '1px solid gray',
|
||||
borderRadius: '5px',
|
||||
textAlign: 'right',
|
||||
}}>
|
||||
<ListItemText primary={item.question} />
|
||||
</ListItemButton>
|
||||
</ListItem>
|
||||
))}
|
||||
</List>
|
||||
</Grid>
|
||||
|
||||
<Grid size={6}>
|
||||
<SortableList index={index} answers={answers}/>
|
||||
</Grid>
|
||||
</Grid>
|
||||
);
|
||||
}
|
||||
|
||||
export default Matching
|
||||
72
site/testing/features/Quiz.tsx
Normal file
72
site/testing/features/Quiz.tsx
Normal file
@@ -0,0 +1,72 @@
|
||||
import { Box, Button, Container, Typography } from '@mui/material';
|
||||
import { quiz } from "../quizData";
|
||||
import { useSelector } from 'react-redux';
|
||||
import {useState } from 'react';
|
||||
import Matching from './Matching';
|
||||
import type { RootState } from '../../store';
|
||||
import store from '../../store';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { mixUp, startTesting, stopTesting } from './quizSlice';
|
||||
|
||||
function QuizStats() {
|
||||
|
||||
const checkTask = (index: number): string => {
|
||||
useSelector((state: RootState) => state.lists.lists[index]);
|
||||
let state = store.getState();
|
||||
if (state.lists.correctAnswers.length <= index) {
|
||||
return "";
|
||||
}
|
||||
let correctCounter = state.lists.lists[index].reduce((prev, answ, i) => prev + Number(state.lists.correctAnswers[index][i] === answ), 0)
|
||||
|
||||
if (correctCounter == state.lists.lists[index].length) {
|
||||
return "все ответы верные"
|
||||
}
|
||||
return `верно ${correctCounter}/${state.lists.lists[index].length}`
|
||||
|
||||
}
|
||||
return (
|
||||
<Container sx={{ margin: "auto" }}>
|
||||
<h3>Результаты теста</h3>
|
||||
<Typography>Задание 1: {checkTask(0)}</Typography>
|
||||
<Typography>Задание 2: {checkTask(1)}</Typography>
|
||||
</Container>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
function Quiz() {
|
||||
const dispatch = useDispatch();
|
||||
const [displayingResults, setDisplayingResults] = useState(false);
|
||||
const resetQuiz = () => {
|
||||
setDisplayingResults(false);
|
||||
dispatch(mixUp());
|
||||
dispatch(startTesting());
|
||||
}
|
||||
const checkQuiz = () => {
|
||||
setDisplayingResults(true);
|
||||
dispatch(stopTesting());
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<Container maxWidth="md">
|
||||
{quiz.map((item, index) => (
|
||||
<Box key={item.id} component="section" sx={{ m: 2, p: 2 }}>
|
||||
<Typography variant="h5" gutterBottom>
|
||||
{index + 1}. {item.title}
|
||||
</Typography>
|
||||
<Matching index={index} tasks={item.tasks} />
|
||||
</Box>
|
||||
))}
|
||||
|
||||
<Box sx={{ display: 'flex', justifyContent: 'space-around' }}>
|
||||
<Button onClick={checkQuiz} variant="contained">Проверить</Button>
|
||||
<Button onClick={resetQuiz} variant="contained">Начать снова</Button>
|
||||
</Box>
|
||||
{displayingResults && <QuizStats />}
|
||||
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
export default Quiz
|
||||
45
site/testing/features/SortableList.tsx
Normal file
45
site/testing/features/SortableList.tsx
Normal file
@@ -0,0 +1,45 @@
|
||||
import { DndContext, closestCenter } from '@dnd-kit/core';
|
||||
import { SortableContext, verticalListSortingStrategy, arrayMove } from '@dnd-kit/sortable';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import { setDraggedItems } from './quizSlice';
|
||||
import type { RootState } from '../../store';
|
||||
import List from '@mui/material/List';
|
||||
import { SortableItem } from '../components/SortableItem'
|
||||
|
||||
interface ComponentProps {
|
||||
index: number;
|
||||
answers: string[];
|
||||
}
|
||||
|
||||
function SortableList({ index}: ComponentProps) {
|
||||
|
||||
const dispatch = useDispatch();
|
||||
const arr = useSelector((state: RootState) => state.lists.lists[index])
|
||||
const isDisabled = useSelector((state: RootState) => state.lists.isTestingDone)
|
||||
const draggedItems = arr || [];
|
||||
|
||||
const handleDragEnd = (event: any) => {
|
||||
const { active, over } = event;
|
||||
if (active.id !== over.id) {
|
||||
const oldIndex = draggedItems.indexOf(active.id);
|
||||
const newIndex = draggedItems.indexOf(over.id);
|
||||
const newList = arrayMove(draggedItems, oldIndex, newIndex);
|
||||
dispatch(setDraggedItems({ index, items: newList }));
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<DndContext collisionDetection={closestCenter} onDragEnd={handleDragEnd}>
|
||||
<SortableContext items={draggedItems}
|
||||
strategy={verticalListSortingStrategy}>
|
||||
<List>
|
||||
{draggedItems.map((item) => (
|
||||
<SortableItem key={item} item={item} id={item} isDisabled={isDisabled}/>
|
||||
))}
|
||||
</List>
|
||||
</SortableContext>
|
||||
</DndContext>
|
||||
);
|
||||
}
|
||||
|
||||
export default SortableList;
|
||||
67
site/testing/features/quizSlice.tsx
Normal file
67
site/testing/features/quizSlice.tsx
Normal file
@@ -0,0 +1,67 @@
|
||||
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 ListsState {
|
||||
lists: string[][]; // хранит перемещаемые элементы каждого списка ответов
|
||||
correctAnswers:string[][];
|
||||
isTestingDone:boolean
|
||||
}
|
||||
|
||||
const initialState: ListsState = {
|
||||
lists: [],
|
||||
correctAnswers:[],
|
||||
isTestingDone:false
|
||||
};
|
||||
|
||||
const listsSlice = createSlice({
|
||||
name: 'lists',
|
||||
initialState,
|
||||
reducers: {
|
||||
addList: (state, action: PayloadAction<{index: number; items: string[]}>)=>{
|
||||
const { index, items } = action.payload;
|
||||
state.lists.splice(index, 1, items);
|
||||
state.correctAnswers.splice(index, 1, items);
|
||||
|
||||
},
|
||||
setDraggedItems: (state, action: PayloadAction<{ index: number; items: string[] }>) => {
|
||||
const { index, items } = action.payload;
|
||||
if (index >= 0 && index < state.lists.length) {
|
||||
state.lists[index] = items; // обновляем конкретный список
|
||||
}
|
||||
},
|
||||
mixUp: (state) => {
|
||||
state.lists=state.lists.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 {ListsState}
|
||||
export default listsSlice.reducer;
|
||||
Reference in New Issue
Block a user