lab 8 p6/7 done

This commit is contained in:
2026-04-23 16:49:22 +10:00
parent 0b76fc69e5
commit 7684b8d705
5 changed files with 65 additions and 32 deletions

View File

@@ -1,5 +1,7 @@
import { StrictMode } from 'react' import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client' import { createRoot } from 'react-dom/client'
import { Provider } from 'react-redux';
import store from './store';
import { import {
createBrowserRouter, createBrowserRouter,
@@ -39,6 +41,8 @@ const router = createBrowserRouter([
import './styles/index.css' import './styles/index.css'
createRoot(document.getElementById('root')!).render( createRoot(document.getElementById('root')!).render(
<StrictMode> <StrictMode>
<Provider store={store}>
<RouterProvider router={router} /> <RouterProvider router={router} />
</Provider>
</StrictMode>, </StrictMode>,
) )

View File

@@ -0,0 +1,13 @@
import { configureStore } from '@reduxjs/toolkit';
import listsReducer from './testing/features/quizSlice';
const store = configureStore({
reducer: {
lists: listsReducer,
},
});
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
export default store;

View File

@@ -1,11 +1,22 @@
import { Grid, List, ListItem, ListItemButton, ListItemText } from '@mui/material'; 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 SortableList from '../features/SortableList';
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { addList } from './quizSlice';
interface ComponentProps { interface ComponentProps {
tasks: tTasks; tasks: tTasks;
index: number;
} }
function Matching({tasks}: ComponentProps) { function Matching({tasks,index}: ComponentProps) {
const dispatch = useDispatch();
// Добавляем список ответов очередного задания в хранилище
useEffect(() => {
dispatch(addList({ index, items: answers }));
}, []);
const answers: string[] = tasks.map((item) => item.answer); const answers: string[] = tasks.map((item) => item.answer);
return ( return (
<Grid container spacing={2}> <Grid container spacing={2}>
@@ -27,7 +38,7 @@ function Matching({tasks}: ComponentProps) {
</Grid> </Grid>
<Grid size={6}> <Grid size={6}>
<SortableList answers={answers} /> <SortableList index={index} answers={answers}/>
</Grid> </Grid>
</Grid> </Grid>
); );

View File

@@ -11,7 +11,7 @@ function Quiz() {
<Typography variant="h5" gutterBottom> <Typography variant="h5" gutterBottom>
{index + 1}. { item.title } {index + 1}. { item.title }
</Typography> </Typography>
<Matching tasks={item.tasks} /> <Matching index={index} tasks={ item.tasks }/>
</Box> </Box>
))} ))}
<Box sx={{ display: 'flex', justifyContent:'space-around' }}> <Box sx={{ display: 'flex', justifyContent:'space-around' }}>

View File

@@ -1,24 +1,29 @@
import { DndContext, closestCenter } from '@dnd-kit/core'; import { DndContext, closestCenter } from '@dnd-kit/core';
import { SortableContext, verticalListSortingStrategy, arrayMove } from '@dnd-kit/sortable'; import { SortableContext, verticalListSortingStrategy, arrayMove } from '@dnd-kit/sortable';
import { useState } from 'react'; import { useDispatch, useSelector } from 'react-redux';
import { setDraggedItems } from './quizSlice';
import type { RootState } from '../../store';
import List from '@mui/material/List'; import List from '@mui/material/List';
import { SortableItem } from '../components/SortableItem' import { SortableItem } from '../components/SortableItem'
interface ComponentProps { interface ComponentProps {
index: number;
answers: string[]; answers: string[];
} }
function SortableList({ answers }: ComponentProps ) { function SortableList({ index}: ComponentProps) {
const [draggedItems, setDraggedItems] = useState(answers);
const dispatch = useDispatch();
const arr = useSelector((state: RootState) => state.lists.lists[index])
const draggedItems = arr || [];
const handleDragEnd = (event: any) => { const handleDragEnd = (event: any) => {
const { active, over } = event; const { active, over } = event;
if (active.id !== over.id) { if (active.id !== over.id) {
setDraggedItems((draggedItems) => {
const oldIndex = draggedItems.indexOf(active.id); const oldIndex = draggedItems.indexOf(active.id);
const newIndex = draggedItems.indexOf(over.id); const newIndex = draggedItems.indexOf(over.id);
return arrayMove(draggedItems, oldIndex, newIndex); const newList = arrayMove(draggedItems, oldIndex, newIndex);
}); dispatch(setDraggedItems({ index, items: newList }));
} }
}; };