p6/7 started (Freezing due to no experience in redux)
This commit is contained in:
36
labs/lab8/src/testing/features/Matching.tsx
Normal file
36
labs/lab8/src/testing/features/Matching.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Grid, List, ListItem, ListItemButton, ListItemText } from '@mui/material';
|
||||
import type {tTasks} from "../quizData"
|
||||
import SortableList from '../features/SortableList';
|
||||
interface ComponentProps {
|
||||
tasks: tTasks;
|
||||
}
|
||||
|
||||
function Matching({tasks}: ComponentProps) {
|
||||
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 answers={answers} />
|
||||
</Grid>
|
||||
</Grid>
|
||||
);
|
||||
}
|
||||
|
||||
export default Matching
|
||||
25
labs/lab8/src/testing/features/Quiz.tsx
Normal file
25
labs/lab8/src/testing/features/Quiz.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Box, Button, Container, Typography } from '@mui/material';
|
||||
import { quiz } from "../quizData";
|
||||
import Matching from './Matching';
|
||||
|
||||
function Quiz() {
|
||||
|
||||
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 tasks={item.tasks} />
|
||||
</Box>
|
||||
))}
|
||||
<Box sx={{ display: 'flex', justifyContent:'space-around' }}>
|
||||
<Button variant="contained">Проверить</Button>
|
||||
<Button variant="contained">Начать снова</Button>
|
||||
</Box>
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
export default Quiz
|
||||
39
labs/lab8/src/testing/features/SortableList.tsx
Normal file
39
labs/lab8/src/testing/features/SortableList.tsx
Normal file
@@ -0,0 +1,39 @@
|
||||
import { DndContext, closestCenter } from '@dnd-kit/core';
|
||||
import { SortableContext, verticalListSortingStrategy, arrayMove } from '@dnd-kit/sortable';
|
||||
import { useState } from 'react';
|
||||
import List from '@mui/material/List';
|
||||
import {SortableItem} from '../components/SortableItem'
|
||||
|
||||
interface ComponentProps {
|
||||
answers: string[];
|
||||
}
|
||||
|
||||
function SortableList({ answers }: ComponentProps ) {
|
||||
const [draggedItems, setDraggedItems] = useState(answers);
|
||||
|
||||
const handleDragEnd = (event: any) => {
|
||||
const { active, over } = event;
|
||||
if (active.id !== over.id) {
|
||||
setDraggedItems((draggedItems) => {
|
||||
const oldIndex = draggedItems.indexOf(active.id);
|
||||
const newIndex = draggedItems.indexOf(over.id);
|
||||
return arrayMove(draggedItems, oldIndex, newIndex);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<DndContext collisionDetection={closestCenter} onDragEnd={handleDragEnd}>
|
||||
<SortableContext items={ draggedItems }
|
||||
strategy={verticalListSortingStrategy}>
|
||||
<List>
|
||||
{draggedItems.map((item) => (
|
||||
<SortableItem key={ item } item={item} id={ item } />
|
||||
))}
|
||||
</List>
|
||||
</SortableContext>
|
||||
</DndContext>
|
||||
);
|
||||
}
|
||||
|
||||
export default SortableList;
|
||||
30
labs/lab8/src/testing/features/quizSlice.tsx
Normal file
30
labs/lab8/src/testing/features/quizSlice.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
import type {PayloadAction} from '@reduxjs/toolkit';
|
||||
interface ListsState {
|
||||
lists: string[][]; // хранит перемещаемые элементы каждого списка ответов
|
||||
}
|
||||
|
||||
const initialState: ListsState = {
|
||||
lists: [],
|
||||
};
|
||||
|
||||
const listsSlice = createSlice({
|
||||
name: 'lists',
|
||||
initialState,
|
||||
reducers: {
|
||||
addList: (state, action: PayloadAction<{index: number; items: string[]}>)=>{
|
||||
const { index, items } = action.payload;
|
||||
state.lists.splice(index, 0, 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; // обновляем конкретный список
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Экспортируем действия и редьюсер
|
||||
export const { addList, setDraggedItems } = listsSlice.actions;
|
||||
export default listsSlice.reducer;
|
||||
Reference in New Issue
Block a user