fix: DraggableList (WIP)
This commit is contained in:
parent
3c06dd04ad
commit
a56089591b
@ -1,35 +1,30 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import { useParams } from "react-router-dom";
|
||||||
import { Box } from "@mui/material";
|
import { Box } from "@mui/material";
|
||||||
import { DragDropContext } from "react-beautiful-dnd";
|
import { DragDropContext } from "react-beautiful-dnd";
|
||||||
|
|
||||||
import { StrictModeDroppable } from "./StrictModeDroppable";
|
import { StrictModeDroppable } from "./StrictModeDroppable";
|
||||||
import { DraggableListItem } from "./DraggableListItem";
|
import { DraggableListItem } from "./DraggableListItem";
|
||||||
|
|
||||||
|
import { questionStore } from "@root/questions";
|
||||||
|
|
||||||
import { reorder } from "./helper";
|
import { reorder } from "./helper";
|
||||||
|
|
||||||
import type { DropResult } from "react-beautiful-dnd";
|
import type { DropResult } from "react-beautiful-dnd";
|
||||||
import type { Quizes } from "../../../stores/quizes";
|
|
||||||
|
|
||||||
export type ExtendedQuizes = Quizes & {
|
export const DraggableList = () => {
|
||||||
onDelete: () => void;
|
const quizId = Number(useParams().quizId);
|
||||||
};
|
const { listQuestions, setQuestions, removeQuestion } = questionStore();
|
||||||
|
|
||||||
type DraggableListProps = {
|
|
||||||
items: ExtendedQuizes[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export const DraggableList = ({ items }: DraggableListProps) => {
|
|
||||||
const [listItems, setListItems] = useState(items);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setListItems(items);
|
|
||||||
}, [items]);
|
|
||||||
|
|
||||||
const onDragEnd = ({ destination, source }: DropResult) => {
|
const onDragEnd = ({ destination, source }: DropResult) => {
|
||||||
if (destination) {
|
if (destination) {
|
||||||
const newItems = reorder(listItems, source.index, destination.index);
|
const newItems = reorder(
|
||||||
|
listQuestions[quizId],
|
||||||
|
source.index,
|
||||||
|
destination.index
|
||||||
|
);
|
||||||
|
|
||||||
setListItems(newItems);
|
setQuestions(quizId, newItems);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -38,11 +33,11 @@ export const DraggableList = ({ items }: DraggableListProps) => {
|
|||||||
<StrictModeDroppable droppableId="droppable-list">
|
<StrictModeDroppable droppableId="droppable-list">
|
||||||
{(provided, snapshot) => (
|
{(provided, snapshot) => (
|
||||||
<Box ref={provided.innerRef} {...provided.droppableProps}>
|
<Box ref={provided.innerRef} {...provided.droppableProps}>
|
||||||
{listItems.map(({ onDelete }, index) => (
|
{listQuestions[quizId].map((_, index) => (
|
||||||
<DraggableListItem
|
<DraggableListItem
|
||||||
key={index}
|
key={index}
|
||||||
index={index}
|
index={index}
|
||||||
onDelete={onDelete}
|
onDelete={() => removeQuestion(quizId, index)}
|
||||||
activePlus={snapshot.isDraggingOver}
|
activePlus={snapshot.isDraggingOver}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@ -17,13 +17,10 @@ import { questionStore } from "@root/questions";
|
|||||||
import QuizCard from "../createQuize/QuizCard";
|
import QuizCard from "../createQuize/QuizCard";
|
||||||
import { DraggableList } from "./DraggableList";
|
import { DraggableList } from "./DraggableList";
|
||||||
|
|
||||||
import type { ExtendedQuizes } from "./DraggableList";
|
|
||||||
|
|
||||||
export default function QuestionsPage() {
|
export default function QuestionsPage() {
|
||||||
const { listQuizes, updateQuizesList } = quizStore();
|
const { listQuizes, updateQuizesList } = quizStore();
|
||||||
const params = Number(useParams().quizId);
|
const params = Number(useParams().quizId);
|
||||||
const { listQuestions, updateQuestionsList, createQuestion, removeQuestion } =
|
const { listQuestions, createQuestion } = questionStore();
|
||||||
questionStore();
|
|
||||||
const activeStep = listQuizes[params].step;
|
const activeStep = listQuizes[params].step;
|
||||||
const handleNext = () => {
|
const handleNext = () => {
|
||||||
updateQuizesList(params, { step: listQuizes[params].step + 1 });
|
updateQuizesList(params, { step: listQuizes[params].step + 1 });
|
||||||
@ -59,14 +56,7 @@ export default function QuestionsPage() {
|
|||||||
Свернуть всё
|
Свернуть всё
|
||||||
</Link>
|
</Link>
|
||||||
</Box>
|
</Box>
|
||||||
<DraggableList
|
<DraggableList />
|
||||||
items={Object.values(listQuestions[params] as ExtendedQuizes[]).map(
|
|
||||||
(item, index) => ({
|
|
||||||
...item,
|
|
||||||
onDelete: () => removeQuestion(params, index),
|
|
||||||
})
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
display: "flex",
|
display: "flex",
|
||||||
|
|||||||
@ -1,67 +1,83 @@
|
|||||||
import {create} from "zustand";
|
import { create } from "zustand";
|
||||||
import {persist} from "zustand/middleware";
|
import { persist } from "zustand/middleware";
|
||||||
import {quizStore} from "@root/quizes";
|
import { quizStore } from "@root/quizes";
|
||||||
import {useParams} from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
|
|
||||||
|
export type Question = {
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
type: string;
|
||||||
|
required: boolean;
|
||||||
|
deleted: boolean;
|
||||||
|
page: number;
|
||||||
|
content: string;
|
||||||
|
version: number;
|
||||||
|
parent_ids: number[];
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
|
};
|
||||||
|
|
||||||
interface QuestionStore {
|
interface QuestionStore {
|
||||||
listQuestions: any;
|
listQuestions: Record<number, Question[]>;
|
||||||
updateQuestionsList: (id: number, index: number, values: unknown) => void;
|
setQuestions: (id: number, questions: Question[]) => void;
|
||||||
removeQuestion: any;
|
updateQuestionsList: (id: number, index: number, values: unknown) => void;
|
||||||
createQuestion: (id: number) => void;
|
removeQuestion: any;
|
||||||
|
createQuestion: (id: number) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const questionStore = create<QuestionStore>()(
|
export const questionStore = create<QuestionStore>()(
|
||||||
|
persist(
|
||||||
|
(set, get) => ({
|
||||||
|
listQuestions: {},
|
||||||
|
setQuestions: (id: number, questions: Question[]) => {
|
||||||
|
const listQuestions = get()["listQuestions"];
|
||||||
|
|
||||||
persist(
|
set({ listQuestions: { ...listQuestions, [id]: questions } });
|
||||||
(set, get) => ({
|
},
|
||||||
listQuestions: {
|
|
||||||
},
|
|
||||||
updateQuestionsList: (id: number, index: number, values: any) => {
|
|
||||||
const array = get()["listQuestions"][id] || [];
|
|
||||||
array[index] = {
|
|
||||||
...array[index],
|
|
||||||
...values
|
|
||||||
};
|
|
||||||
const state = get()["listQuestions"] || {};
|
|
||||||
state[id] = array
|
|
||||||
set({listQuestions: state});
|
|
||||||
},
|
|
||||||
|
|
||||||
createQuestion:(id: number) => {
|
updateQuestionsList: (id: number, index: number, values: any) => {
|
||||||
const array = get()["listQuestions"][id] || [];
|
const array = get()["listQuestions"][id] || [];
|
||||||
array.push(
|
array[index] = {
|
||||||
{
|
...array[index],
|
||||||
"title": "",
|
...values,
|
||||||
"description": "",
|
};
|
||||||
"type": "",
|
const state = get()["listQuestions"] || {};
|
||||||
"required": true,
|
state[id] = array;
|
||||||
"deleted": true,
|
console.log(state);
|
||||||
"page": 0,
|
set({ listQuestions: state });
|
||||||
"content": "",
|
},
|
||||||
"version": 0,
|
|
||||||
"parent_ids": [
|
|
||||||
0
|
|
||||||
],
|
|
||||||
"created_at": "",
|
|
||||||
"updated_at": ""
|
|
||||||
}
|
|
||||||
)
|
|
||||||
const state = get()["listQuestions"] || {};
|
|
||||||
state[id] = array
|
|
||||||
set({listQuestions: state});
|
|
||||||
},
|
|
||||||
|
|
||||||
removeQuestion: (id:number, index: number) => {
|
createQuestion: (id: number) => {
|
||||||
const array = get()["listQuestions"][id] || [];
|
const array = get()["listQuestions"][id] || [];
|
||||||
array.splice(index, 1)
|
array.push({
|
||||||
const state = get()["listQuestions"] || {};
|
title: "",
|
||||||
state[id] = array
|
description: "",
|
||||||
set({listQuestions: state});
|
type: "",
|
||||||
},
|
required: true,
|
||||||
}),
|
deleted: true,
|
||||||
|
page: 0,
|
||||||
|
content: "",
|
||||||
|
version: 0,
|
||||||
|
parent_ids: [0],
|
||||||
|
created_at: "",
|
||||||
|
updated_at: "",
|
||||||
|
});
|
||||||
|
const state = get()["listQuestions"] || {};
|
||||||
|
state[id] = array;
|
||||||
|
set({ listQuestions: state });
|
||||||
|
},
|
||||||
|
|
||||||
{
|
removeQuestion: (id: number, index: number) => {
|
||||||
name: "question",
|
const array = get()["listQuestions"][id] || [];
|
||||||
}
|
array.splice(index, 1);
|
||||||
)
|
const state = get()["listQuestions"] || {};
|
||||||
);
|
state[id] = array;
|
||||||
|
set({ listQuestions: state });
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
|
||||||
|
{
|
||||||
|
name: "question",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user