2024-03-29 20:08:34 -04:00
|
|
|
// EditorQuiz.tsx
|
2025-03-20 21:11:46 -04:00
|
|
|
import React, { useState, useEffect, CSSProperties } from 'react';
|
2024-03-29 20:08:34 -04:00
|
|
|
import { useParams, useNavigate } from 'react-router-dom';
|
|
|
|
|
|
|
|
|
|
import { FolderType } from '../../../Types/FolderType';
|
|
|
|
|
|
2025-01-16 12:37:07 -05:00
|
|
|
import Editor from 'src/components/Editor/Editor';
|
|
|
|
|
import GiftCheatSheet from 'src/components/GIFTCheatSheet/GiftCheatSheet';
|
|
|
|
|
import GIFTTemplatePreview from 'src/components/GiftTemplate/GIFTTemplatePreview';
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
import { QuizType } from '../../../Types/QuizType';
|
|
|
|
|
|
|
|
|
|
import './editorQuiz.css';
|
2025-03-20 21:11:46 -04:00
|
|
|
import { Button, TextField, NativeSelect, Divider } from '@mui/material';
|
2025-01-16 12:37:07 -05:00
|
|
|
import ReturnButton from 'src/components/ReturnButton/ReturnButton';
|
2025-03-20 21:11:46 -04:00
|
|
|
import ImageGalleryModal from 'src/components/ImageGallery/ImageGalleryModal/ImageGalleryModal';
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
import ApiService from '../../../services/ApiService';
|
2024-09-17 18:56:13 -04:00
|
|
|
import { escapeForGIFT } from '../../../utils/giftUtils';
|
2025-04-07 22:00:28 -04:00
|
|
|
import { ENV_VARIABLES } from 'src/constants';
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
interface EditQuizParams {
|
|
|
|
|
id: string;
|
|
|
|
|
[key: string]: string | undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QuizForm: React.FC = () => {
|
|
|
|
|
const [quizTitle, setQuizTitle] = useState('');
|
|
|
|
|
const [selectedFolder, setSelectedFolder] = useState<string>('');
|
|
|
|
|
const [filteredValue, setFilteredValue] = useState<string[]>([]);
|
|
|
|
|
|
|
|
|
|
const { id } = useParams<EditQuizParams>();
|
|
|
|
|
const [value, setValue] = useState('');
|
|
|
|
|
const [isNewQuiz, setNewQuiz] = useState(false);
|
|
|
|
|
const [quiz, setQuiz] = useState<QuizType | null>(null);
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const [folders, setFolders] = useState<FolderType[]>([]);
|
|
|
|
|
const [imageLinks, setImageLinks] = useState<string[]>([]);
|
|
|
|
|
const handleSelectFolder = (event: React.ChangeEvent<HTMLSelectElement>) => {
|
|
|
|
|
setSelectedFolder(event.target.value);
|
|
|
|
|
};
|
2025-01-14 17:37:29 -05:00
|
|
|
const [showScrollButton, setShowScrollButton] = useState(false);
|
|
|
|
|
|
|
|
|
|
const scrollToTop = () => {
|
|
|
|
|
window.scrollTo({ top: 0, behavior: 'smooth' });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleScroll = () => {
|
|
|
|
|
if (window.scrollY > 300) {
|
|
|
|
|
setShowScrollButton(true);
|
|
|
|
|
} else {
|
|
|
|
|
setShowScrollButton(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener('scroll', handleScroll);
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener('scroll', handleScroll);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2025-01-14 16:21:32 -05:00
|
|
|
const scrollToImagesSection = (event: { preventDefault: () => void; }) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
const section = document.getElementById('images-section');
|
|
|
|
|
if (section) {
|
|
|
|
|
section.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchData = async () => {
|
|
|
|
|
const userFolders = await ApiService.getUserFolders();
|
|
|
|
|
setFolders(userFolders as FolderType[]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fetchData();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchData = async () => {
|
|
|
|
|
try {
|
|
|
|
|
if (!id || id === 'new') {
|
|
|
|
|
setNewQuiz(true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const quiz = await ApiService.getQuiz(id) as QuizType;
|
|
|
|
|
|
|
|
|
|
if (!quiz) {
|
|
|
|
|
window.alert(`Une erreur est survenue.\n Le quiz ${id} n'a pas été trouvé\nVeuillez réessayer plus tard`)
|
|
|
|
|
console.error('Quiz not found for id:', id);
|
|
|
|
|
navigate('/teacher/dashboard');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setQuiz(quiz as QuizType);
|
|
|
|
|
const { title, content, folderId } = quiz;
|
|
|
|
|
|
|
|
|
|
setQuizTitle(title);
|
|
|
|
|
setSelectedFolder(folderId);
|
|
|
|
|
setFilteredValue(content);
|
|
|
|
|
setValue(quiz.content.join('\n\n'));
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
window.alert(`Une erreur est survenue.\n Veuillez réessayer plus tard`)
|
|
|
|
|
console.error('Error fetching quiz:', error);
|
|
|
|
|
navigate('/teacher/dashboard');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fetchData();
|
|
|
|
|
}, [id]);
|
|
|
|
|
|
|
|
|
|
function handleUpdatePreview(value: string) {
|
|
|
|
|
if (value !== '') {
|
|
|
|
|
setValue(value);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-14 21:28:08 -05:00
|
|
|
// 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();
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
if (linesArray[linesArray.length - 1] === '') linesArray.pop();
|
|
|
|
|
|
|
|
|
|
setFilteredValue(linesArray);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleQuizTitleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
setQuizTitle(event.target.value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleQuizSave = async () => {
|
|
|
|
|
try {
|
|
|
|
|
// check if everything is there
|
|
|
|
|
if (quizTitle == '') {
|
|
|
|
|
alert("Veuillez choisir un titre");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (selectedFolder == '') {
|
|
|
|
|
alert("Veuillez choisir un dossier");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isNewQuiz) {
|
|
|
|
|
await ApiService.createQuiz(quizTitle, filteredValue, selectedFolder);
|
|
|
|
|
} else {
|
|
|
|
|
if (quiz) {
|
|
|
|
|
await ApiService.updateQuiz(quiz._id, quizTitle, filteredValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
navigate('/teacher/dashboard');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
window.alert(`Une erreur est survenue.\n Veuillez réessayer plus tard`)
|
|
|
|
|
console.log(error)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// I do not know what this does but do not remove
|
|
|
|
|
if (!isNewQuiz && !quiz) {
|
|
|
|
|
return <div>Chargement...</div>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleCopyToClipboard = async (link: string) => {
|
|
|
|
|
navigator.clipboard.writeText(link);
|
|
|
|
|
}
|
2025-03-02 20:52:08 -05:00
|
|
|
|
2025-03-20 21:11:46 -04:00
|
|
|
const handleCopyImage = (id: string) => {
|
2025-04-07 22:00:28 -04:00
|
|
|
const escLink = `${ENV_VARIABLES.BACKEND_URL}/api/image/get/${id}`;
|
2025-03-20 21:11:46 -04:00
|
|
|
setImageLinks(prevLinks => [...prevLinks, escLink]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return (
|
|
|
|
|
<div className='quizEditor'>
|
|
|
|
|
|
|
|
|
|
<div className='editHeader'>
|
|
|
|
|
<ReturnButton
|
|
|
|
|
askConfirm
|
|
|
|
|
message={`Êtes-vous sûr de vouloir quitter l'éditeur sans sauvegarder le questionnaire?`}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div className='title'>Éditeur de quiz</div>
|
|
|
|
|
|
|
|
|
|
<div className='dumb'></div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-09-17 18:56:13 -04:00
|
|
|
{/* <h2 className="subtitle">Éditeur</h2> */}
|
|
|
|
|
|
|
|
|
|
<TextField
|
|
|
|
|
onChange={handleQuizTitleChange}
|
|
|
|
|
value={quizTitle}
|
|
|
|
|
placeholder="Titre du quiz"
|
|
|
|
|
label="Titre du quiz"
|
|
|
|
|
fullWidth
|
|
|
|
|
/>
|
|
|
|
|
<label>Choisir un dossier:
|
|
|
|
|
<NativeSelect
|
|
|
|
|
id="select-folder"
|
|
|
|
|
color="primary"
|
|
|
|
|
value={selectedFolder}
|
|
|
|
|
onChange={handleSelectFolder}
|
|
|
|
|
disabled={!isNewQuiz}
|
|
|
|
|
style={{ marginBottom: '16px' }} // Ajout de marge en bas
|
|
|
|
|
>
|
|
|
|
|
<option disabled value=""> Choisir un dossier... </option>
|
|
|
|
|
|
|
|
|
|
{folders.map((folder: FolderType) => (
|
|
|
|
|
<option value={folder._id} key={folder._id}> {folder.title} </option>
|
|
|
|
|
))}
|
|
|
|
|
</NativeSelect></label>
|
|
|
|
|
|
|
|
|
|
<Button variant="contained" onClick={handleQuizSave}>
|
|
|
|
|
Enregistrer
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
<Divider style={{ margin: '16px 0' }} />
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
<div className='editSection'>
|
|
|
|
|
|
|
|
|
|
<div className='edit'>
|
2024-09-17 18:56:13 -04:00
|
|
|
<Editor
|
|
|
|
|
label="Contenu GIFT du quiz:"
|
|
|
|
|
initialValue={value}
|
|
|
|
|
onEditorChange={handleUpdatePreview} />
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
<div className='images'>
|
2025-03-20 21:11:46 -04:00
|
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: '10px' }}>
|
|
|
|
|
<h4>Mes images :</h4>
|
|
|
|
|
<ImageGalleryModal handleCopy={handleCopyImage} />
|
2024-03-29 20:08:34 -04:00
|
|
|
</div>
|
2025-03-04 19:49:18 -05:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
<div>
|
2025-01-14 15:06:41 -05:00
|
|
|
<div>
|
2025-01-14 16:21:32 -05:00
|
|
|
<div style={{ display: "inline" }}>(Voir section </div>
|
|
|
|
|
<a href="#images-section"style={{ textDecoration: "none" }} onClick={scrollToImagesSection}>
|
|
|
|
|
<u><em><h4 style={{ display: "inline" }}> 9. Images </h4></em></u>
|
|
|
|
|
</a>
|
|
|
|
|
<div style={{ display: "inline" }}> ci-dessous</div>
|
2025-01-14 15:06:41 -05:00
|
|
|
<div style={{ display: "inline" }}>)</div>
|
|
|
|
|
<br />
|
|
|
|
|
<em> - Cliquez sur un lien pour le copier</em>
|
|
|
|
|
</div>
|
|
|
|
|
<ul>
|
2024-09-17 18:56:13 -04:00
|
|
|
{imageLinks.map((link, index) => {
|
2025-03-20 21:11:46 -04:00
|
|
|
const imgTag = `[markdown]} "texte de l'infobulle") {T}`;
|
2024-09-17 18:56:13 -04:00
|
|
|
return (
|
|
|
|
|
<li key={index}>
|
|
|
|
|
<code
|
|
|
|
|
onClick={() => handleCopyToClipboard(imgTag)}>
|
|
|
|
|
{imgTag}
|
|
|
|
|
</code>
|
|
|
|
|
</li>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2024-03-29 20:08:34 -04:00
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<GiftCheatSheet />
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className='preview'>
|
|
|
|
|
<div className="preview-column">
|
2024-09-17 18:56:13 -04:00
|
|
|
<h4>Prévisualisation</h4>
|
2024-03-29 20:08:34 -04:00
|
|
|
<div>
|
|
|
|
|
<GIFTTemplatePreview questions={filteredValue} />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-01-14 17:37:29 -05:00
|
|
|
{showScrollButton && (
|
|
|
|
|
<Button
|
|
|
|
|
onClick={scrollToTop}
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="primary"
|
|
|
|
|
style={scrollToTopButtonStyle}
|
|
|
|
|
title="Scroll to top"
|
|
|
|
|
>
|
|
|
|
|
↑
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
2024-03-29 20:08:34 -04:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-14 17:37:29 -05:00
|
|
|
const scrollToTopButtonStyle: CSSProperties = {
|
|
|
|
|
position: 'fixed',
|
|
|
|
|
bottom: '40px',
|
|
|
|
|
right: '50px',
|
|
|
|
|
padding: '10px',
|
|
|
|
|
fontSize: '16px',
|
|
|
|
|
color: 'white',
|
|
|
|
|
backgroundColor: '#5271ff',
|
|
|
|
|
border: 'none',
|
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
zIndex: 1000,
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
export default QuizForm;
|