-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.jsx
More file actions
executable file
·105 lines (99 loc) · 2.88 KB
/
App.jsx
File metadata and controls
executable file
·105 lines (99 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { useEffect, useState } from "react";
import { Route, Routes } from "react-router-dom";
import { toast, ToastContainer } from "react-toastify";
import IO from "socket.io-client";
import AuthenticatedRoutes from "./components/auth/AuthenticatedRoutes";
import RedirectAuthenticated from "./components/auth/RedirectAuthenticated";
import ToastMessages from "./components/ToastMessages";
import ChatContext from "./data/AppContext";
import {
arrayIsEmpty,
BASE_URL,
removeItem,
setDataStorage,
} from "./data/utilsFuns";
import routes from "./routes/routes";
import { verifyUserHasAuthenticated } from "./services/AuthApi";
const socket = IO.connect(BASE_URL);
function App() {
const [isSocketConnect, setIsSocketConnect] = useState(socket.connected);
const [lastConnect, setLastConnect] = useState(null);
const {
userData,
alert,
setSocket,
setAlert,
setUserIsAuthenticated,
userIsAuthenticated,
updateDimensions,
} = ChatContext();
useEffect(() => {
updateDimensions();
// On verifie si l'utilisateur est
setUserIsAuthenticated(verifyUserHasAuthenticated());
if (!arrayIsEmpty(alert)) {
ToastMessages(alert);
setAlert([]);
}
setSocket(socket);
if (userIsAuthenticated) {
removeItem("users");
setDataStorage("contacts", []);
socket.on("connect", () => {
setIsSocketConnect(true);
if (userIsAuthenticated) {
// Si je suis connecter au tout debut je vais rejoindre mon propre salon
socket.emit("join_conversation", {
userConnectId: userData.userId,
userInterlocutorId: userData.userId,
});
socket.emit("user_online", userData);
}
});
socket.on("disconnect", () => {
setIsSocketConnect(false);
});
socket.on("user_login", (user) => {
if (user.email !== userData.email) {
toast.info(user.firstName + " est connecté");
}
});
}
return () => {
socket.off("connect");
socket.off("disconnect");
socket.off("user_login");
socket.off("send_message");
socket.off("user_contact");
socket.off("user_writing");
};
}, [setUserIsAuthenticated, userIsAuthenticated]);
return (
<>
<Routes>
{routes.map(({ path, protect, component }, index) => {
if (protect) {
return (
<Route
key={index}
path={path}
element={<AuthenticatedRoutes>{component}</AuthenticatedRoutes>}
></Route>
);
}
return (
<Route
path={path}
key={index}
element={
<RedirectAuthenticated>{component}</RedirectAuthenticated>
}
></Route>
);
})}
</Routes>
<ToastContainer />
</>
);
}
export default App;