From 5ecb39b6a863f653c4094e62704bb37a5358b958 Mon Sep 17 00:00:00 2001 From: Stefan Niclas Heun Date: Mon, 23 Dec 2024 09:46:26 +0100 Subject: [PATCH] adding dnd functionality to change the order of the questions --- .../ApplicationQuestionConfig.tsx | 65 +++++++++++++++---- .../components/ApplicationQuestionCard.tsx | 20 +++--- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/clients/core/src/Application/ApplicationConfiguration/ApplicationQuestionConfig/ApplicationQuestionConfig.tsx b/clients/core/src/Application/ApplicationConfiguration/ApplicationQuestionConfig/ApplicationQuestionConfig.tsx index 3561a74..df23990 100644 --- a/clients/core/src/Application/ApplicationConfiguration/ApplicationQuestionConfig/ApplicationQuestionConfig.tsx +++ b/clients/core/src/Application/ApplicationConfiguration/ApplicationQuestionConfig/ApplicationQuestionConfig.tsx @@ -18,6 +18,7 @@ import { handleSubmitAllQuestions } from './handlers/handleSubmitAllQuestions' import { computeQuestionsModified } from './handlers/computeQuestionsModified' import { handleQuestionUpdate } from './handlers/handleQuestionUpdate' import { AddQuestionMenu } from './components/AddQuestionMenu' +import { DragDropContext, Draggable, Droppable, DropResult } from 'react-beautiful-dnd' export const ApplicationQuestionConfig = (): JSX.Element => { const { phaseId } = useParams<{ phaseId: string }>() @@ -89,6 +90,24 @@ export const ApplicationQuestionConfig = (): JSX.Element => { setApplicationQuestions((prev) => prev.filter((q) => q.id !== id)) } + const onDragEnd = (result: DropResult) => { + if (!result.destination) { + return + } + + const newQuestions = Array.from(applicationQuestions) + const [reorderedItem] = newQuestions.splice(result.source.index, 1) + newQuestions.splice(result.destination.index, 0, reorderedItem) + + // Update order_num for all questions + const updatedQuestions = newQuestions.map((question, index) => ({ + ...question, + order_num: index + 1, + })) + + setApplicationQuestions(updatedQuestions) + } + if (isApplicationFormPending || isApplicationFormError || isMutatePending || isMutateError) { return (
@@ -143,20 +162,38 @@ export const ApplicationQuestionConfig = (): JSX.Element => { /> )} {applicationQuestions.length > 0 ? ( - applicationQuestions.map((question, index) => ( - q.id === question.id)} - index={index} - onUpdate={(updatedQuestion) => { - handleQuestionUpdate(updatedQuestion, setApplicationQuestions) - }} - ref={(el) => (questionRefs.current[index] = el)} - submitAttempted={submitAttempted} - onDelete={handleDeleteQuestion} - /> - )) + + + {(provided) => ( +
+ {applicationQuestions.map((question, index) => ( + + {(providedQuestionItem) => ( +
+ q.id === question.id)} + index={index} + onUpdate={(updatedQuestion) => { + handleQuestionUpdate(updatedQuestion, setApplicationQuestions) + }} + ref={(el) => (questionRefs.current[index] = el)} + submitAttempted={submitAttempted} + onDelete={handleDeleteQuestion} + /> +
+ )} +
+ ))} + {provided.placeholder} +
+ )} +
+
) : ( diff --git a/clients/core/src/Application/ApplicationConfiguration/ApplicationQuestionConfig/components/ApplicationQuestionCard.tsx b/clients/core/src/Application/ApplicationConfiguration/ApplicationQuestionConfig/components/ApplicationQuestionCard.tsx index a90c9aa..f8ca0fc 100644 --- a/clients/core/src/Application/ApplicationConfiguration/ApplicationQuestionConfig/components/ApplicationQuestionCard.tsx +++ b/clients/core/src/Application/ApplicationConfiguration/ApplicationQuestionConfig/components/ApplicationQuestionCard.tsx @@ -4,7 +4,7 @@ import { zodResolver } from '@hookform/resolvers/zod' import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card' import { Input } from '@/components/ui/input' import { Checkbox } from '@/components/ui/checkbox' -import { ChevronDown, ChevronUp, Trash2 } from 'lucide-react' +import { ChevronDown, ChevronUp, GripVertical, Trash2 } from 'lucide-react' import { ApplicationQuestionMultiSelect } from '@/interfaces/application_question_multi_select' import { ApplicationQuestionText } from '@/interfaces/application_question_text' import { @@ -97,8 +97,16 @@ export const ApplicationQuestionCard = forwardRef< className={`mb-4 ${submitAttempted && !form.formState.isValid ? 'border-red-500' : ''}`} > setIsExpanded(!isExpanded)}> - - {question.title || `Question ${index + 1}`} +
+
+ +
+ {question.title || `Untitled Question`} +

+ Question {index + 1}: {isMultiSelect ? 'Multi-select question' : 'Text question'} +

+
+
{isExpanded ? : }
- -

- {isMultiSelect ? 'Multi-select question' : 'Text question'} -

+
{isExpanded && (