Compare commits
2 Commits
1335275ed7
...
4532122dee
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4532122dee | ||
|
|
4e2f4eb525 |
72
site/src/quiz/features/Choosing.tsx
Normal file
72
site/src/quiz/features/Choosing.tsx
Normal file
@@ -0,0 +1,72 @@
|
||||
import { Grid, List, ListItem, ListItemButton, ListItemText } from '@mui/material';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import type { tTasks } from "../quizData"
|
||||
import { useEffect } from 'react';
|
||||
import { addList, mixUp } from './quizSlice';
|
||||
import Checkbox from '@mui/material/Checkbox';
|
||||
import type { RootState } from '../../store';
|
||||
import type { ChangeEvent } from 'react';
|
||||
import { setCheckedItems } from './quizSlice';
|
||||
|
||||
|
||||
interface ComponentProps {
|
||||
tasks: tTasks;
|
||||
index: number;
|
||||
}
|
||||
|
||||
function Matching({ tasks, index }: ComponentProps) {
|
||||
const dispatch = useDispatch();
|
||||
const answers: boolean[] = tasks.map((item) => Boolean(item.answer));
|
||||
const items: string[] = tasks.map((item) => String(item.answer));
|
||||
const arr = useSelector((state: RootState) => state.quiz.userAnswers[index]);
|
||||
// const correctAnswers = useSelector((state: RootState) => state.quiz.correctAnswers[index]);
|
||||
const questions = useSelector((state: RootState) => state.quiz.questions[index]);
|
||||
const isDisabled = useSelector((state: RootState) => state.quiz.isTestingDone);
|
||||
useEffect(() => {
|
||||
dispatch(addList({ index, items: items,questions:tasks.map((item) => String(item.question)), answers: answers, quizType: "M" }));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(mixUp());
|
||||
}, []);
|
||||
|
||||
|
||||
const handleCheckboxAction = (checkboxIndex: number) => (_: ChangeEvent<HTMLInputElement, Element>, checked: boolean) => {
|
||||
let answersCopy = arr.map((x)=>(Boolean(x)));
|
||||
answersCopy[checkboxIndex] = checked;
|
||||
dispatch(setCheckedItems({ index, items: answersCopy }));
|
||||
};
|
||||
|
||||
// console.log(arr);
|
||||
|
||||
return (
|
||||
<Grid container spacing={2}>
|
||||
<Grid size={12}>
|
||||
<List>
|
||||
{tasks.map((_, itemIndex) => (
|
||||
<ListItem key={itemIndex}>
|
||||
<ListItemButton
|
||||
sx={{
|
||||
border: '1px solid gray',
|
||||
borderRadius: '5px',
|
||||
textAlign: 'right',
|
||||
}}>
|
||||
<ListItemText primary={arr == undefined ? "loading": questions[itemIndex]} />
|
||||
<Checkbox
|
||||
onChange={isDisabled ? (..._) => { _ } : handleCheckboxAction(itemIndex)}
|
||||
checked={Boolean(arr == undefined ? false : arr[itemIndex])}
|
||||
sx={{ '& .MuiSvgIcon-root': { fontSize: 28 } }}
|
||||
/>
|
||||
</ListItemButton>
|
||||
</ListItem>
|
||||
))}
|
||||
</List>
|
||||
</Grid>
|
||||
<Grid size={12}>
|
||||
|
||||
</Grid>
|
||||
</Grid>
|
||||
);
|
||||
}
|
||||
|
||||
export default Matching
|
||||
@@ -1,51 +1,51 @@
|
||||
import { Grid, List, ListItem, ListItemButton, ListItemText } from '@mui/material';
|
||||
import type {tTasks} from "../quizData"
|
||||
import type { tTasks } from "../quizData"
|
||||
import SortableList from '../features/SortableList';
|
||||
import { useEffect } from 'react';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { addList,mixUp } from './quizSlice';
|
||||
import { addList, mixUp } from './quizSlice';
|
||||
interface ComponentProps {
|
||||
tasks: tTasks;
|
||||
index: number;
|
||||
}
|
||||
tasks: tTasks;
|
||||
index: number;
|
||||
}
|
||||
|
||||
function Matching({tasks,index}: ComponentProps) {
|
||||
function Matching({ tasks, index }: ComponentProps) {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
const answers: string[] = tasks.map((item) => String(item.answer));
|
||||
// Добавляем список ответов очередного задания в хранилище
|
||||
useEffect(() => {
|
||||
dispatch(addList({ index, items: answers }));
|
||||
dispatch(addList({ index, items: answers, answers:answers, questions:tasks.map((item) => String(item.question)), quizType: "M" }));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(mixUp());
|
||||
dispatch(mixUp());
|
||||
}, []);
|
||||
|
||||
|
||||
const answers: string[] = tasks.map((item) => String(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>
|
||||
<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 size={6}>
|
||||
<SortableList index={index} answers={answers} />
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Grid>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +1,50 @@
|
||||
import { Box, Button, Container, Typography } from '@mui/material';
|
||||
import { quiz } from "../quizData";
|
||||
import { useSelector } from 'react-redux';
|
||||
import {useState } from 'react';
|
||||
import { useState } from 'react';
|
||||
import Matching from './Matching';
|
||||
import Sorting from './Sorting';
|
||||
import Choosing from './Choosing';
|
||||
import type { RootState } from '../../store';
|
||||
import store from '../../store';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { mixUp, startTesting, stopTesting } from './quizSlice';
|
||||
|
||||
function QuizStats() {
|
||||
let correctAnswers = useSelector((state: RootState) => state.quiz.correctAnswers);
|
||||
const userAnswers = useSelector((state: RootState) => state.quiz.userAnswers);
|
||||
|
||||
const correctCount = (index: number):number => {
|
||||
let quiz = useSelector((state: RootState) => state.quiz);
|
||||
const correctCount = (index: number): number => {
|
||||
let state = store.getState();
|
||||
if (state.quiz.correctAnswers.length <= index) {
|
||||
return 0;
|
||||
}
|
||||
return quiz.userAnswers[index].reduce((prev, answ, i) => prev + Number(quiz.correctAnswers[index][i] === answ), 0);
|
||||
return userAnswers[index].reduce((prev, answ, i) => prev + Number(correctAnswers[index][i] === answ), 0);
|
||||
}
|
||||
|
||||
const quizPartText = (counter:number, len:number):string=>{
|
||||
const counts = userAnswers.map((_,index)=>correctCount(index));
|
||||
const questionCounts = userAnswers.map((answers)=>answers.length);
|
||||
|
||||
const total= counts.reduce((x,c)=>c+x,0);
|
||||
const totalQuestionCount= questionCounts.reduce((x,c)=>c+x,0);
|
||||
|
||||
const donePercentage = Math.round(total/totalQuestionCount*100*100)/100;
|
||||
|
||||
const quizPartText = (counter: number, len: number, taskIndex:number) => {
|
||||
if (counter == len) {
|
||||
return "все ответы верные"
|
||||
return <Typography key={taskIndex}>Задание {taskIndex+1}: все ответы верные</Typography>
|
||||
}
|
||||
return `верно ${counter}/${len}`
|
||||
return <Typography key={taskIndex}>Задание {taskIndex+1}: верно {counter}/{len}</Typography>
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<Container sx={{ margin: "auto" }}>
|
||||
<h3>Результаты теста</h3>
|
||||
{/* <Typography>Задание 1: {checkTask(0)}</Typography> */}
|
||||
{/* <Typography>Задание 2: {checkTask(1)}</Typography> */}
|
||||
<Container sx={{ margin: "auto", d:"flex",flexDirection:"column" }}>
|
||||
<h2>Результаты теста</h2>
|
||||
{
|
||||
counts.map((count,idx)=>quizPartText(count,questionCounts[idx],idx))
|
||||
}
|
||||
<h3>Итого {donePercentage}% Верно</h3>
|
||||
</Container>
|
||||
)
|
||||
}
|
||||
@@ -53,18 +68,27 @@ function Quiz() {
|
||||
return (
|
||||
<Container maxWidth="md">
|
||||
{quiz.map((item, index) => (
|
||||
<Box key={item.id} component="section" sx={{ m: 2, p: 2 }}>
|
||||
<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} />,<>S</>,<>c</>][(["M","S","C"].indexOf(item.type))]}
|
||||
</Box>
|
||||
))}
|
||||
{[
|
||||
|
||||
<Matching index={index} tasks={item.tasks} key={index}/>,
|
||||
|
||||
<Sorting index={index} tasks={item.tasks} key={index}/>,
|
||||
|
||||
<Choosing index={index} tasks={item.tasks} key={index}/>
|
||||
|
||||
][(["M", "S", "C"].indexOf(item.type))]}
|
||||
</Box>
|
||||
|
||||
))}
|
||||
<Box sx={{ display: 'flex', justifyContent: 'space-around' }}>
|
||||
<Button onClick={checkQuiz} variant="contained">Check</Button>
|
||||
<Button onClick={resetQuiz} variant="contained">Reset</Button>
|
||||
</Box>
|
||||
|
||||
<Box sx={{ display: 'flex', justifyContent: 'space-around' }}>
|
||||
<Button onClick={checkQuiz} variant="contained">Проверить</Button>
|
||||
<Button onClick={resetQuiz} variant="contained">Начать снова</Button>
|
||||
</Box>
|
||||
{displayingResults && <QuizStats />}
|
||||
|
||||
</Container>
|
||||
|
||||
@@ -4,8 +4,7 @@ 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'
|
||||
|
||||
import { SortableItem } from '../components/SortableItem';
|
||||
interface ComponentProps {
|
||||
index: number;
|
||||
answers: string[];
|
||||
|
||||
35
site/src/quiz/features/Sorting.tsx
Normal file
35
site/src/quiz/features/Sorting.tsx
Normal file
@@ -0,0 +1,35 @@
|
||||
import { Grid} 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();
|
||||
const answers: string[] = tasks.map((item) => String(item.answer));
|
||||
// Добавляем список ответов очередного задания в хранилище
|
||||
useEffect(() => {
|
||||
dispatch(addList({ index, items: answers, answers:answers,questions:tasks.map((item) => String(item.question)), quizType: "M" }));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(mixUp());
|
||||
}, []);
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<Grid container spacing={2}>
|
||||
<Grid size={12}>
|
||||
<SortableList index={index} answers={answers} />
|
||||
</Grid>
|
||||
</Grid>
|
||||
);
|
||||
}
|
||||
|
||||
export default Matching
|
||||
@@ -20,6 +20,7 @@ function shuffle<T>(array: Array<T>): Array<T> {
|
||||
|
||||
interface QuizState {
|
||||
userAnswers: (string | boolean)[][]; // хранит перемещаемые элементы каждого списка ответов
|
||||
questions: string[][];
|
||||
correctAnswers: (string | boolean)[][];// ответы для вопрсов, в случае с matching просто правильная последоавтельность, для sorting аналогично, для choosе последоватеьность boolean
|
||||
quizTypes: Array<"M" | "S" | "C">;
|
||||
isTestingDone: boolean// запрет на взаимодействие с квизом после окончания тестирования, чтобю юзер не наглел
|
||||
@@ -29,6 +30,7 @@ const initialState: QuizState = {
|
||||
userAnswers: [],
|
||||
correctAnswers: [],
|
||||
quizTypes: [],
|
||||
questions:[],
|
||||
isTestingDone: false,
|
||||
};
|
||||
|
||||
@@ -36,8 +38,9 @@ const listsSlice = createSlice({
|
||||
name: 'lists',
|
||||
initialState,
|
||||
reducers: {
|
||||
addList: (state, action: PayloadAction<{ index: number; items: string[]; answers: (string|boolean)[]; quizType : "S" | "M" | "C"}>) => {
|
||||
const { index, items,answers,quizType} = action.payload;
|
||||
addList: (state, action: PayloadAction<{ index: number; items: string[]; questions: string[]; answers: (string|boolean)[]; quizType : "S" | "M" | "C"}>) => {
|
||||
const { index, items,questions,answers,quizType} = action.payload;
|
||||
state.questions.splice(index, 1, questions);
|
||||
state.userAnswers.splice(index, 1, items); // с нулём создаётся по 2 экземпляра, видно в отладке
|
||||
state.quizTypes.splice(index, 1, quizType); //
|
||||
state.correctAnswers.splice(index, 1, answers);
|
||||
@@ -49,16 +52,20 @@ const listsSlice = createSlice({
|
||||
state.userAnswers[index] = items; // обновляем конкретный список
|
||||
}
|
||||
},
|
||||
setCheckedItems: (state, action: PayloadAction<{ index: number; items: (string|boolean)[] }>) => {
|
||||
setCheckedItems: (state, action: PayloadAction<{ index: number; items: 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) => {
|
||||
if(typeof(list[0])==="boolean"){
|
||||
return list.map(()=>false);
|
||||
state.userAnswers = state.userAnswers.map((list,index) => {
|
||||
if(typeof(state.correctAnswers[index][0])==="boolean"){
|
||||
list.map(()=>false);
|
||||
let indexes = shuffle( list.map((_,idx)=>idx));
|
||||
state.correctAnswers[index]=state.correctAnswers[index].map((_,i,arr)=>arr[indexes[i]])
|
||||
state.questions[index]=state.questions[index].map((_,i,arr)=>arr[indexes[i]])
|
||||
return list.map(()=>false)
|
||||
}
|
||||
return shuffle<string|boolean>(list);
|
||||
})
|
||||
@@ -73,6 +80,6 @@ const listsSlice = createSlice({
|
||||
});
|
||||
|
||||
// Экспортируем действия и редьюсер
|
||||
export const { addList, setDraggedItems, mixUp, startTesting, stopTesting } = listsSlice.actions;
|
||||
export const { addList, setDraggedItems,setCheckedItems, mixUp, startTesting, stopTesting } = listsSlice.actions;
|
||||
export type { QuizState }
|
||||
export default listsSlice.reducer;
|
||||
@@ -65,7 +65,7 @@ export const quiz: tQuizzes = [
|
||||
{
|
||||
"id": 3,
|
||||
"type": "C",
|
||||
"title": "Вопрос 3 Выбирай!",
|
||||
"title": "Вопрос 3 Выбирай",
|
||||
"tasks": [
|
||||
{
|
||||
"question": "да",
|
||||
@@ -76,15 +76,15 @@ export const quiz: tQuizzes = [
|
||||
"answer": false
|
||||
},
|
||||
{
|
||||
"question": "и снова да",
|
||||
"question": "да1",
|
||||
"answer": true
|
||||
},
|
||||
{
|
||||
"question": "нет",
|
||||
"question": "нет1",
|
||||
"answer": false
|
||||
},
|
||||
{
|
||||
"question": "дааа",
|
||||
"question": "да2",
|
||||
"answer": true
|
||||
},
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user