mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
Correction if aucune salle est selectionner
This commit is contained in:
parent
9486eacc53
commit
5436fc3a1f
5 changed files with 62 additions and 27 deletions
|
|
@ -42,10 +42,10 @@ const ManageRoom: React.FC = () => {
|
||||||
const [roomName, setRoomName] = useState<string>(selectedRoom?.title || '');
|
const [roomName, setRoomName] = useState<string>(selectedRoom?.title || '');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (selectedRoom) {
|
if (selectedRoom && !socket) {
|
||||||
setRoomName(selectedRoom.title);
|
createWebSocketRoom();
|
||||||
}
|
}
|
||||||
}, [selectedRoom]);
|
}, [selectedRoom]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
|
|
@ -110,7 +110,7 @@ const ManageRoom: React.FC = () => {
|
||||||
console.log('Creating WebSocket room...');
|
console.log('Creating WebSocket room...');
|
||||||
|
|
||||||
if (!selectedRoom) {
|
if (!selectedRoom) {
|
||||||
setConnectingError('Aucune salle sélectionnée.');
|
setConnectingError('Veuillez sélectionner une salle.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,16 +21,31 @@ export const RoomProvider = ({ children }: { children: React.ReactNode }) => {
|
||||||
const userRooms = await ApiService.getUserRooms();
|
const userRooms = await ApiService.getUserRooms();
|
||||||
const roomsList = userRooms as RoomType[];
|
const roomsList = userRooms as RoomType[];
|
||||||
setRooms(roomsList);
|
setRooms(roomsList);
|
||||||
if (roomsList.length > 0) {
|
|
||||||
const defaultRoom = roomsList[1]; // Set the first created room as the selected one
|
const savedRoomId = localStorage.getItem('selectedRoomId');
|
||||||
setSelectedRoom(defaultRoom);
|
if (savedRoomId) {
|
||||||
localStorage.setItem('selectedRoomId', defaultRoom._id);
|
const savedRoom = roomsList.find(r => r._id === savedRoomId);
|
||||||
} else {
|
if (savedRoom) {
|
||||||
|
setSelectedRoom(savedRoom);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (roomsList.length === 0) {
|
||||||
const randomRoomName = `Room-${Math.floor(Math.random() * 1000000)}`;
|
const randomRoomName = `Room-${Math.floor(Math.random() * 1000000)}`;
|
||||||
await createRoom(randomRoomName);
|
const newRoomId = await ApiService.createRoom(randomRoomName);
|
||||||
|
const newRoom = await ApiService.getRoomContent(newRoomId);
|
||||||
|
setSelectedRoom(newRoom);
|
||||||
|
localStorage.setItem('selectedRoomId', newRoomId);
|
||||||
|
|
||||||
|
const updatedRooms = await ApiService.getUserRooms();
|
||||||
|
setRooms(updatedRooms as RoomType[]);
|
||||||
|
} else {
|
||||||
|
setSelectedRoom(roomsList[0]);
|
||||||
|
localStorage.setItem('selectedRoomId', roomsList[0]._id);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
loadRooms();
|
loadRooms();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|
@ -43,12 +58,16 @@ export const RoomProvider = ({ children }: { children: React.ReactNode }) => {
|
||||||
|
|
||||||
// Créer une salle
|
// Créer une salle
|
||||||
const createRoom = async (title: string) => {
|
const createRoom = async (title: string) => {
|
||||||
const newRoomId = await ApiService.createRoom(title);
|
// Créer la salle et récupérer l'objet complet
|
||||||
|
const newRoom = await ApiService.createRoom(title);
|
||||||
|
|
||||||
|
// Mettre à jour la liste des salles
|
||||||
const updatedRooms = await ApiService.getUserRooms();
|
const updatedRooms = await ApiService.getUserRooms();
|
||||||
setRooms(updatedRooms as RoomType[]);
|
setRooms(updatedRooms as RoomType[]);
|
||||||
selectRoom(newRoomId);
|
|
||||||
|
// Sélectionner la nouvelle salle avec son ID
|
||||||
|
selectRoom(newRoom); // Utiliser l'ID de l'objet retourné
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<RoomContext.Provider value={{ rooms, selectedRoom, selectRoom, createRoom }}>
|
<RoomContext.Provider value={{ rooms, selectedRoom, selectRoom, createRoom }}>
|
||||||
{children}
|
{children}
|
||||||
|
|
|
||||||
|
|
@ -408,7 +408,27 @@ class ApiService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getRoomContent(roomId: string): Promise<RoomType> {
|
||||||
|
try {
|
||||||
|
const url = this.constructRequestUrl(`/room/${roomId}`);
|
||||||
|
const headers = this.constructRequestHeaders();
|
||||||
|
|
||||||
|
const response = await axios.get<{ data: RoomType }>(url, { headers });
|
||||||
|
|
||||||
|
if (response.status !== 200) {
|
||||||
|
throw new Error(`Failed to get room: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.data.data;
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
if (axios.isAxiosError(error)) {
|
||||||
|
const serverError = error.response?.data?.error;
|
||||||
|
throw new Error(serverError || 'Erreur serveur inconnue');
|
||||||
|
}
|
||||||
|
throw new Error('Erreur réseau');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async getRoomTitleByUserId(userId: string): Promise<string[] | string> {
|
public async getRoomTitleByUserId(userId: string): Promise<string[] | string> {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -64,13 +64,6 @@ class RoomsController {
|
||||||
if (!roomId) {
|
if (!roomId) {
|
||||||
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
||||||
}
|
}
|
||||||
|
|
||||||
const owner = await this.rooms.getOwner(roomId);
|
|
||||||
|
|
||||||
if (owner != req.user.userId) {
|
|
||||||
throw new AppError(ROOM_NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
const content = await this.rooms.getContent(roomId);
|
const content = await this.rooms.getContent(roomId);
|
||||||
|
|
||||||
if (!content) {
|
if (!content) {
|
||||||
|
|
|
||||||
|
|
@ -58,15 +58,18 @@ class Rooms {
|
||||||
}
|
}
|
||||||
|
|
||||||
async getContent(roomId) {
|
async getContent(roomId) {
|
||||||
await this.db.connect()
|
await this.db.connect();
|
||||||
const conn = this.db.getConnection();
|
const conn = this.db.getConnection();
|
||||||
|
const roomsCollection = conn.collection('rooms');
|
||||||
const filesCollection = conn.collection('files');
|
if (!ObjectId.isValid(roomId)) {
|
||||||
|
return null; // Évite d'envoyer une requête invalide
|
||||||
const result = await filesCollection.find({ roomId: roomId }).toArray();
|
}
|
||||||
|
|
||||||
|
const result = await roomsCollection.findOne({ _id: new ObjectId(roomId) });
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async delete(roomId) {
|
async delete(roomId) {
|
||||||
await this.db.connect()
|
await this.db.connect()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue