From aa74227981103fb87044d1c236d891a16aee5afd Mon Sep 17 00:00:00 2001 From: Philippe <83185129+phil3838@users.noreply.github.com> Date: Wed, 12 Mar 2025 13:43:11 -0400 Subject: [PATCH] editor question per question, delete and add question works --- client/src/components/Editor/Editor.tsx | 86 +++++++++++++------ .../pages/Teacher/EditorQuiz/EditorQuiz.tsx | 41 ++++----- 2 files changed, 81 insertions(+), 46 deletions(-) diff --git a/client/src/components/Editor/Editor.tsx b/client/src/components/Editor/Editor.tsx index 540cb4a..47d9c70 100644 --- a/client/src/components/Editor/Editor.tsx +++ b/client/src/components/Editor/Editor.tsx @@ -1,37 +1,71 @@ -// Editor.tsx -import React, { useState, useRef } from 'react'; -import './editor.css'; -import { TextareaAutosize } from '@mui/material'; +import React from 'react'; +import { TextField, Typography, IconButton, Box } from '@mui/material'; +import DeleteIcon from '@mui/icons-material/Delete'; // Import delete icon interface EditorProps { label: string; - initialValue: string; - onEditorChange: (value: string) => void; + values: string[]; + onValuesChange: (values: string[]) => void; } -const Editor: React.FC = ({ initialValue, onEditorChange, label }) => { - const [value, setValue] = useState(initialValue); - const editorRef = useRef(null); +const Editor: React.FC = ({ label, values, onValuesChange }) => { + const handleChange = (index: number) => (event: React.ChangeEvent) => { + const newValues = [...values]; + newValues[index] = event.target.value; + onValuesChange(newValues); + }; - function handleEditorChange(event: React.ChangeEvent) { - const text = event.target.value; - setValue(text); - onEditorChange(text || ''); - } + const handleDeleteQuestion = (index: number) => () => { + const newValues = values.filter((_, i) => i !== index); // Remove the question at the specified index + onValuesChange(newValues); + }; return ( - +
+ {/* Label with increased margin */} + + {label} + + + {/* Map through each question */} + {values.map((value, index) => ( + + {/* Bold "Question #" title */} + + + Question {index + 1} + + + {/* Delete button */} + + + + + {/* TextField for the question */} + + + + + ))} +
); }; -export default Editor; +export default Editor; \ No newline at end of file diff --git a/client/src/pages/Teacher/EditorQuiz/EditorQuiz.tsx b/client/src/pages/Teacher/EditorQuiz/EditorQuiz.tsx index 89f822a..65d6c1f 100644 --- a/client/src/pages/Teacher/EditorQuiz/EditorQuiz.tsx +++ b/client/src/pages/Teacher/EditorQuiz/EditorQuiz.tsx @@ -29,7 +29,7 @@ const QuizForm: React.FC = () => { const [filteredValue, setFilteredValue] = useState([]); const { id } = useParams(); - const [value, setValue] = useState(''); + const [values, setValues] = useState([]); const [isNewQuiz, setNewQuiz] = useState(false); const [quiz, setQuiz] = useState(null); const navigate = useNavigate(); @@ -101,7 +101,7 @@ const QuizForm: React.FC = () => { setQuizTitle(title); setSelectedFolder(folderId); setFilteredValue(content); - setValue(quiz.content.join('\n\n')); + setValues(content); } catch (error) { window.alert(`Une erreur est survenue.\n Veuillez réessayer plus tard`) @@ -113,21 +113,17 @@ const QuizForm: React.FC = () => { fetchData(); }, [id]); - function handleUpdatePreview(value: string) { - if (value !== '') { - setValue(value); - } + const handleAddQuestion = () => { + console.log("Adding question"); + console.log("Current values:", values); // Log current state + setValues([...values, '']); + console.log("Updated values:", [...values, '']); // Log new state + }; - // split value when there is at least one blank line - const linesArray = value.split(/\n{2,}/); - - // if the first item in linesArray is blank, remove it - if (linesArray[0] === '') linesArray.shift(); - - if (linesArray[linesArray.length - 1] === '') linesArray.pop(); - - setFilteredValue(linesArray); - } + const handleUpdatePreview = (newValues: string[]) => { + setValues(newValues); + setFilteredValue(newValues.filter(value => value.trim() !== '')); + }; const handleQuizTitleChange = (event: React.ChangeEvent) => { setQuizTitle(event.target.value); @@ -204,6 +200,8 @@ const QuizForm: React.FC = () => { navigator.clipboard.writeText(link); } + + return (
@@ -213,7 +211,7 @@ const QuizForm: React.FC = () => { message={`Êtes-vous sûr de vouloir quitter l'éditeur sans sauvegarder le questionnaire?`} /> -
Éditeur de quiz
+
Éditeur de Quiz
@@ -253,9 +251,12 @@ const QuizForm: React.FC = () => {
+ label="Contenu GIFT de chaque question:" + values={values} + onValuesChange={handleUpdatePreview} /> +