error when user uses his own URL to copy a quiz

This commit is contained in:
Philippe 2025-03-11 22:12:36 -04:00
parent 71f57fd2cf
commit 66ce4937d9
3 changed files with 34 additions and 17 deletions

View file

@ -40,10 +40,10 @@ const ShareQuizModal: React.FC<ShareQuizModalProps> = ({ quiz }) => {
const quizUrl = `${window.location.origin}/teacher/share/${quiz._id}`;
navigator.clipboard.writeText(quizUrl)
.then(() => {
window.alert('URL copied to clipboard!');
window.alert('URL a été copiée avec succès.');
})
.catch(() => {
window.alert('Failed to copy URL to clipboard.');
window.alert('Une erreur est survenue lors de la copie de l\'URL.');
});
handleCloseModal();

View file

@ -1,18 +1,12 @@
// EditorQuiz.tsx
import React, { useState, useEffect } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { FolderType } from '../../../Types/FolderType';
import './share.css';
import { Button, NativeSelect } from '@mui/material';
import ReturnButton from 'src/components/ReturnButton/ReturnButton';
import ApiService from '../../../services/ApiService';
const Share: React.FC = () => {
console.log('Component rendered');
const navigate = useNavigate();
const { id } = useParams<string>();
@ -23,7 +17,6 @@ const Share: React.FC = () => {
useEffect(() => {
const fetchData = async () => {
console.log("QUIZID : " + id)
if (!id) {
window.alert(`Une erreur est survenue.\n Le quiz n'a pas été trouvé\nVeuillez réessayer plus tard`)
console.error('Quiz not found for id:', id);
@ -36,9 +29,17 @@ const Share: React.FC = () => {
navigate("/login");
return;
}
const quizIds = await ApiService.getAllQuizIds();
if (quizIds.includes(id)) {
window.alert(`Le quiz que vous essayez d'importer existe déjà sur votre compte.`)
navigate('/teacher/dashboard');
return;
}
const userFolders = await ApiService.getUserFolders();
if (userFolders.length == 0) {
window.alert(`Vous n'avez aucun dossier.\nVeuillez en créer un et revenir à ce lien`)
navigate('/teacher/dashboard');

View file

@ -74,8 +74,6 @@ class ApiService {
return false;
}
console.log("ApiService: isLoggedIn: Token:", token);
// Update token expiry
this.saveToken(token);
@ -91,7 +89,6 @@ class ApiService {
}
try {
console.log("ApiService: isLoggedInTeacher: Token:", token);
const decodedToken = jwtDecode(token) as { roles: string[] };
/////// REMOVE BELOW
@ -103,7 +100,6 @@ class ApiService {
const userRoles = decodedToken.roles;
const requiredRole = 'teacher';
console.log("ApiService: isLoggedInTeacher: UserRoles:", userRoles);
if (!userRoles || !userRoles.includes(requiredRole)) {
return false;
}
@ -178,7 +174,6 @@ class ApiService {
const result: AxiosResponse = await axios.post(url, body, { headers: headers });
console.log(result);
if (result.status == 200) {
//window.location.href = result.request.responseURL;
window.location.href = '/login';
@ -190,7 +185,6 @@ class ApiService {
return true;
} catch (error) {
console.log("Error details: ", error);
if (axios.isAxiosError(error)) {
const err = error as AxiosError;
@ -553,7 +547,6 @@ public async login(email: string, password: string): Promise<any> {
const headers = this.constructRequestHeaders();
const body = { folderId };
console.log(headers);
const result: AxiosResponse = await axios.post(url, body, { headers: headers });
if (result.status !== 200) {
@ -1169,6 +1162,29 @@ public async login(email: string, password: string): Promise<any> {
}
// NOTE : Get Image pas necessaire
public async getAllQuizIds(): Promise<string[]> {
try {
const folders = await this.getUserFolders();
const allQuizIds: string[] = [];
for (const folder of folders) {
const folderQuizzes = await this.getFolderContent(folder._id);
if (Array.isArray(folderQuizzes)) {
allQuizIds.push(...folderQuizzes.map(quiz => quiz._id));
}
}
return allQuizIds;
} catch (error) {
console.error('Failed to get all quiz ids:', error);
throw error;
}
}
}
const apiService = new ApiService();