mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
editor question per question, delete and add question works
This commit is contained in:
parent
112062c0b2
commit
aa74227981
2 changed files with 81 additions and 46 deletions
|
|
@ -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<EditorProps> = ({ initialValue, onEditorChange, label }) => {
|
||||
const [value, setValue] = useState(initialValue);
|
||||
const editorRef = useRef<HTMLTextAreaElement | null>(null);
|
||||
const Editor: React.FC<EditorProps> = ({ label, values, onValuesChange }) => {
|
||||
const handleChange = (index: number) => (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const newValues = [...values];
|
||||
newValues[index] = event.target.value;
|
||||
onValuesChange(newValues);
|
||||
};
|
||||
|
||||
function handleEditorChange(event: React.ChangeEvent<HTMLTextAreaElement>) {
|
||||
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>
|
||||
<h4>{label}</h4>
|
||||
<TextareaAutosize
|
||||
id="editor-textarea"
|
||||
ref={editorRef}
|
||||
onChange={handleEditorChange}
|
||||
value={value}
|
||||
className="editor"
|
||||
minRows={5}
|
||||
/>
|
||||
</label>
|
||||
<div>
|
||||
{/* Label with increased margin */}
|
||||
<Typography variant="h6" fontWeight="bold" style={{ marginBottom: '24px' }}>
|
||||
{label}
|
||||
</Typography>
|
||||
|
||||
{/* Map through each question */}
|
||||
{values.map((value, index) => (
|
||||
<Box key={index} style={{ marginBottom: '24px' }}>
|
||||
{/* Bold "Question #" title */}
|
||||
<Box display="flex" alignItems="center" justifyContent="space-between">
|
||||
<Typography variant="subtitle1" fontWeight="bold" style={{ marginBottom: '8px' }}>
|
||||
Question {index + 1}
|
||||
</Typography>
|
||||
|
||||
{/* Delete button */}
|
||||
<IconButton
|
||||
onClick={handleDeleteQuestion(index)}
|
||||
aria-label="delete"
|
||||
sx={{ color: 'light-gray',
|
||||
'&:hover': {
|
||||
color: 'red'
|
||||
},
|
||||
}}
|
||||
>
|
||||
<DeleteIcon />
|
||||
</IconButton>
|
||||
</Box>
|
||||
{/* TextField for the question */}
|
||||
<TextField
|
||||
value={value}
|
||||
onChange={handleChange(index)}
|
||||
fullWidth
|
||||
multiline
|
||||
minRows={4}
|
||||
maxRows={Infinity}
|
||||
variant="outlined"
|
||||
style={{ overflow: 'auto'}}
|
||||
/>
|
||||
|
||||
|
||||
</Box>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Editor;
|
||||
export default Editor;
|
||||
|
|
@ -29,7 +29,7 @@ const QuizForm: React.FC = () => {
|
|||
const [filteredValue, setFilteredValue] = useState<string[]>([]);
|
||||
|
||||
const { id } = useParams<EditQuizParams>();
|
||||
const [value, setValue] = useState('');
|
||||
const [values, setValues] = useState<string[]>([]);
|
||||
const [isNewQuiz, setNewQuiz] = useState(false);
|
||||
const [quiz, setQuiz] = useState<QuizType | null>(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<HTMLInputElement>) => {
|
||||
setQuizTitle(event.target.value);
|
||||
|
|
@ -204,6 +200,8 @@ const QuizForm: React.FC = () => {
|
|||
navigator.clipboard.writeText(link);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<div className='quizEditor'>
|
||||
|
||||
|
|
@ -213,7 +211,7 @@ const QuizForm: React.FC = () => {
|
|||
message={`Êtes-vous sûr de vouloir quitter l'éditeur sans sauvegarder le questionnaire?`}
|
||||
/>
|
||||
|
||||
<div className='title'>Éditeur de quiz</div>
|
||||
<div className='title'>Éditeur de Quiz</div>
|
||||
|
||||
<div className='dumb'></div>
|
||||
</div>
|
||||
|
|
@ -253,9 +251,12 @@ const QuizForm: React.FC = () => {
|
|||
|
||||
<div className='edit'>
|
||||
<Editor
|
||||
label="Contenu GIFT du quiz:"
|
||||
initialValue={value}
|
||||
onEditorChange={handleUpdatePreview} />
|
||||
label="Contenu GIFT de chaque question:"
|
||||
values={values}
|
||||
onValuesChange={handleUpdatePreview} />
|
||||
<Button variant="contained" onClick={handleAddQuestion}>
|
||||
Ajouter une question
|
||||
</Button>
|
||||
|
||||
<div className='images'>
|
||||
<div className='upload'>
|
||||
|
|
|
|||
Loading…
Reference in a new issue