Update frontend/src/App.js
Browse files- frontend/src/App.js +10 -23
frontend/src/App.js
CHANGED
|
@@ -13,40 +13,35 @@ function App() {
|
|
| 13 |
const [activeConversationId, setActiveConversationId] = useState(null);
|
| 14 |
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
| 15 |
const [userName, setUserName] = useState('');
|
| 16 |
-
const [page, setPage] = useState("login");
|
| 17 |
const [userRole, setUserRole] = useState('');
|
| 18 |
|
| 19 |
|
| 20 |
-
// Vérifier si l'utilisateur est déjà connecté au chargement
|
| 21 |
useEffect(() => {
|
| 22 |
const storedUserName = localStorage.getItem('userName');
|
| 23 |
const storedUserRole = localStorage.getItem('userRole');
|
| 24 |
|
| 25 |
if (storedUserName) {
|
| 26 |
setUserName(storedUserName);
|
| 27 |
-
setUserRole(storedUserRole);
|
| 28 |
setIsAuthenticated(true);
|
| 29 |
|
| 30 |
-
// Toujours définir la page sur "chat" au début, quel que soit le rôle
|
| 31 |
setPage("chat");
|
| 32 |
|
| 33 |
-
// Charger les conversations de l'utilisateur
|
| 34 |
fetchConversations();
|
| 35 |
|
| 36 |
console.log("Role chargé depuis localStorage:", storedUserRole);
|
| 37 |
}
|
| 38 |
}, []);
|
| 39 |
|
| 40 |
-
// Fonction pour récupérer les conversations depuis l'API
|
| 41 |
const fetchConversations = async () => {
|
| 42 |
try {
|
| 43 |
const response = await fetch('/api/conversations', {
|
| 44 |
-
credentials: 'include',
|
| 45 |
});
|
| 46 |
|
| 47 |
if (response.ok) {
|
| 48 |
const data = await response.json();
|
| 49 |
-
// Formater les données pour correspondre à votre format de conversation
|
| 50 |
const formattedConversations = data.conversations.map(conv => ({
|
| 51 |
id: conv._id,
|
| 52 |
title: conv.title,
|
|
@@ -63,7 +58,6 @@ useEffect(() => {
|
|
| 63 |
}
|
| 64 |
};
|
| 65 |
|
| 66 |
-
// Fonction pour charger les messages d'une conversation sélectionnée
|
| 67 |
const loadConversationMessages = async (conversationId) => {
|
| 68 |
try {
|
| 69 |
const response = await fetch(`/api/conversations/${conversationId}/messages`, {
|
|
@@ -72,7 +66,6 @@ useEffect(() => {
|
|
| 72 |
|
| 73 |
if (response.ok) {
|
| 74 |
const data = await response.json();
|
| 75 |
-
// Formater les messages pour correspondre à votre format
|
| 76 |
const formattedMessages = data.messages.map(msg => ({
|
| 77 |
sender: msg.sender,
|
| 78 |
text: msg.text,
|
|
@@ -85,7 +78,6 @@ useEffect(() => {
|
|
| 85 |
}
|
| 86 |
};
|
| 87 |
|
| 88 |
-
// Au changement de conversation active
|
| 89 |
useEffect(() => {
|
| 90 |
if (activeConversationId && isAuthenticated) {
|
| 91 |
loadConversationMessages(activeConversationId);
|
|
@@ -114,7 +106,6 @@ useEffect(() => {
|
|
| 114 |
};
|
| 115 |
|
| 116 |
try {
|
| 117 |
-
// Enregistrer la nouvelle conversation dans MongoDB
|
| 118 |
const response = await fetch('/api/conversations', {
|
| 119 |
method: 'POST',
|
| 120 |
headers: {
|
|
@@ -126,7 +117,7 @@ useEffect(() => {
|
|
| 126 |
|
| 127 |
if (response.ok) {
|
| 128 |
const data = await response.json();
|
| 129 |
-
conversationId = data.conversation_id;
|
| 130 |
|
| 131 |
const newChat = {
|
| 132 |
id: conversationId,
|
|
@@ -138,7 +129,6 @@ useEffect(() => {
|
|
| 138 |
setConversations([newChat, ...conversations]);
|
| 139 |
setActiveConversationId(conversationId);
|
| 140 |
|
| 141 |
-
// IMPORTANT: Enregistrer le message utilisateur une fois l'ID disponible
|
| 142 |
await fetch(`/api/conversations/${conversationId}/messages`, {
|
| 143 |
method: 'POST',
|
| 144 |
headers: { 'Content-Type': 'application/json' },
|
|
@@ -154,7 +144,6 @@ useEffect(() => {
|
|
| 154 |
return; // Arrêter si on ne peut pas créer la conversation
|
| 155 |
}
|
| 156 |
} else {
|
| 157 |
-
// Sauvegarder le message utilisateur pour une conversation existante
|
| 158 |
try {
|
| 159 |
await fetch(`/api/conversations/${conversationId}/messages`, {
|
| 160 |
method: 'POST',
|
|
@@ -170,10 +159,9 @@ useEffect(() => {
|
|
| 170 |
}
|
| 171 |
}
|
| 172 |
|
| 173 |
-
return conversationId;
|
| 174 |
};
|
| 175 |
|
| 176 |
-
// Fonction pour sauvegarder la réponse du bot
|
| 177 |
const saveBotResponse = async (conversationId, botResponse) => {
|
| 178 |
if (!conversationId) return;
|
| 179 |
|
|
@@ -195,7 +183,6 @@ useEffect(() => {
|
|
| 195 |
};
|
| 196 |
|
| 197 |
const handleLoginSuccess = () => {
|
| 198 |
-
// Récupérer le nom d'utilisateur stocké par le composant Login
|
| 199 |
const storedUserName = localStorage.getItem('userName');
|
| 200 |
const storedUserRole = localStorage.getItem('userRole');
|
| 201 |
|
|
@@ -206,7 +193,6 @@ useEffect(() => {
|
|
| 206 |
setPage(storedUserRole === "Administrateur" ? "Administrateur" : "chat");
|
| 207 |
|
| 208 |
|
| 209 |
-
// Charger les conversations de l'utilisateur
|
| 210 |
fetchConversations();
|
| 211 |
};
|
| 212 |
const handleLogout = async () => {
|
|
@@ -217,15 +203,14 @@ useEffect(() => {
|
|
| 217 |
credentials: 'include',
|
| 218 |
});
|
| 219 |
|
| 220 |
-
// Nettoyer le stockage local
|
| 221 |
localStorage.removeItem('userName');
|
| 222 |
localStorage.removeItem('userId');
|
| 223 |
-
localStorage.removeItem('userRole');
|
| 224 |
|
| 225 |
// Réinitialiser l'état
|
| 226 |
setIsAuthenticated(false);
|
| 227 |
setUserName('');
|
| 228 |
-
setUserRole('');
|
| 229 |
|
| 230 |
setConversations([]);
|
| 231 |
setMessages([]);
|
|
@@ -301,7 +286,9 @@ useEffect(() => {
|
|
| 301 |
activeConversationId={activeConversationId}
|
| 302 |
saveBotResponse={saveBotResponse}
|
| 303 |
userName={userName}
|
| 304 |
-
toLogin={handleLogout}
|
|
|
|
|
|
|
| 305 |
/>
|
| 306 |
}
|
| 307 |
{page === "login" && <Login toSignin={() => setPage("signin")} onLoginSuccess={handleLoginSuccess}/>}
|
|
|
|
| 13 |
const [activeConversationId, setActiveConversationId] = useState(null);
|
| 14 |
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
| 15 |
const [userName, setUserName] = useState('');
|
| 16 |
+
const [page, setPage] = useState("login");
|
| 17 |
const [userRole, setUserRole] = useState('');
|
| 18 |
|
| 19 |
|
|
|
|
| 20 |
useEffect(() => {
|
| 21 |
const storedUserName = localStorage.getItem('userName');
|
| 22 |
const storedUserRole = localStorage.getItem('userRole');
|
| 23 |
|
| 24 |
if (storedUserName) {
|
| 25 |
setUserName(storedUserName);
|
| 26 |
+
setUserRole(storedUserRole);
|
| 27 |
setIsAuthenticated(true);
|
| 28 |
|
|
|
|
| 29 |
setPage("chat");
|
| 30 |
|
|
|
|
| 31 |
fetchConversations();
|
| 32 |
|
| 33 |
console.log("Role chargé depuis localStorage:", storedUserRole);
|
| 34 |
}
|
| 35 |
}, []);
|
| 36 |
|
|
|
|
| 37 |
const fetchConversations = async () => {
|
| 38 |
try {
|
| 39 |
const response = await fetch('/api/conversations', {
|
| 40 |
+
credentials: 'include',
|
| 41 |
});
|
| 42 |
|
| 43 |
if (response.ok) {
|
| 44 |
const data = await response.json();
|
|
|
|
| 45 |
const formattedConversations = data.conversations.map(conv => ({
|
| 46 |
id: conv._id,
|
| 47 |
title: conv.title,
|
|
|
|
| 58 |
}
|
| 59 |
};
|
| 60 |
|
|
|
|
| 61 |
const loadConversationMessages = async (conversationId) => {
|
| 62 |
try {
|
| 63 |
const response = await fetch(`/api/conversations/${conversationId}/messages`, {
|
|
|
|
| 66 |
|
| 67 |
if (response.ok) {
|
| 68 |
const data = await response.json();
|
|
|
|
| 69 |
const formattedMessages = data.messages.map(msg => ({
|
| 70 |
sender: msg.sender,
|
| 71 |
text: msg.text,
|
|
|
|
| 78 |
}
|
| 79 |
};
|
| 80 |
|
|
|
|
| 81 |
useEffect(() => {
|
| 82 |
if (activeConversationId && isAuthenticated) {
|
| 83 |
loadConversationMessages(activeConversationId);
|
|
|
|
| 106 |
};
|
| 107 |
|
| 108 |
try {
|
|
|
|
| 109 |
const response = await fetch('/api/conversations', {
|
| 110 |
method: 'POST',
|
| 111 |
headers: {
|
|
|
|
| 117 |
|
| 118 |
if (response.ok) {
|
| 119 |
const data = await response.json();
|
| 120 |
+
conversationId = data.conversation_id;
|
| 121 |
|
| 122 |
const newChat = {
|
| 123 |
id: conversationId,
|
|
|
|
| 129 |
setConversations([newChat, ...conversations]);
|
| 130 |
setActiveConversationId(conversationId);
|
| 131 |
|
|
|
|
| 132 |
await fetch(`/api/conversations/${conversationId}/messages`, {
|
| 133 |
method: 'POST',
|
| 134 |
headers: { 'Content-Type': 'application/json' },
|
|
|
|
| 144 |
return; // Arrêter si on ne peut pas créer la conversation
|
| 145 |
}
|
| 146 |
} else {
|
|
|
|
| 147 |
try {
|
| 148 |
await fetch(`/api/conversations/${conversationId}/messages`, {
|
| 149 |
method: 'POST',
|
|
|
|
| 159 |
}
|
| 160 |
}
|
| 161 |
|
| 162 |
+
return conversationId;
|
| 163 |
};
|
| 164 |
|
|
|
|
| 165 |
const saveBotResponse = async (conversationId, botResponse) => {
|
| 166 |
if (!conversationId) return;
|
| 167 |
|
|
|
|
| 183 |
};
|
| 184 |
|
| 185 |
const handleLoginSuccess = () => {
|
|
|
|
| 186 |
const storedUserName = localStorage.getItem('userName');
|
| 187 |
const storedUserRole = localStorage.getItem('userRole');
|
| 188 |
|
|
|
|
| 193 |
setPage(storedUserRole === "Administrateur" ? "Administrateur" : "chat");
|
| 194 |
|
| 195 |
|
|
|
|
| 196 |
fetchConversations();
|
| 197 |
};
|
| 198 |
const handleLogout = async () => {
|
|
|
|
| 203 |
credentials: 'include',
|
| 204 |
});
|
| 205 |
|
|
|
|
| 206 |
localStorage.removeItem('userName');
|
| 207 |
localStorage.removeItem('userId');
|
| 208 |
+
localStorage.removeItem('userRole');
|
| 209 |
|
| 210 |
// Réinitialiser l'état
|
| 211 |
setIsAuthenticated(false);
|
| 212 |
setUserName('');
|
| 213 |
+
setUserRole('');
|
| 214 |
|
| 215 |
setConversations([]);
|
| 216 |
setMessages([]);
|
|
|
|
| 286 |
activeConversationId={activeConversationId}
|
| 287 |
saveBotResponse={saveBotResponse}
|
| 288 |
userName={userName}
|
| 289 |
+
toLogin={handleLogout}
|
| 290 |
+
onNewChat={handleNewChat}
|
| 291 |
+
|
| 292 |
/>
|
| 293 |
}
|
| 294 |
{page === "login" && <Login toSignin={() => setPage("signin")} onLoginSuccess={handleLoginSuccess}/>}
|