feat: online status

This commit is contained in:
Sergey Yarkov 2022-01-29 17:57:34 +03:00
parent bfbf0e8064
commit cdce2c5512
2 changed files with 20 additions and 4 deletions

View File

@ -13,7 +13,10 @@ import { SocketContext } from '@educt/contexts';
type LogoutResultDataType = Record<string, never>; type LogoutResultDataType = Record<string, never>;
const useLogout = () => { const useLogout = () => {
const { authStore } = useRootStore(); const {
authStore,
userStore: { me },
} = useRootStore();
const { socket } = useContext(SocketContext); const { socket } = useContext(SocketContext);
const history = useHistory(); const history = useHistory();
const toast = useToast(); const toast = useToast();
@ -21,11 +24,24 @@ const useLogout = () => {
const logout = async () => { const logout = async () => {
try { try {
const sessionId = window.localStorage.getItem('sessionId');
const result = await authStore.logout(); const result = await authStore.logout();
history.push('/auth'); history.push('/auth');
toast({ title: 'You are logged out.', isClosable: true, status: 'info' }); toast({ title: 'You are logged out.', isClosable: true, status: 'info' });
// window.localStorage.removeItem('sessionId');
socket?.disconnect(); /**
* Destroy session
*/
if (sessionId) {
if (socket) {
socket.auth = { sessionId: undefined };
// socket.emit('user:logout', { sessionId });
socket.disconnect();
}
window.localStorage.removeItem('sessionId');
}
return result.data; return result.data;
} catch (error) { } catch (error) {
handleError(error); handleError(error);

View File

@ -47,7 +47,7 @@ const MainPage: React.FC<IPageProps> = () => {
if (stat) { if (stat) {
setOnline(stat.online); setOnline(stat.online);
} }
}, [stat?.online]); }, [stat]);
if (me === null || courses === null || stat === null) return <LoadingPage />; if (me === null || courses === null || stat === null) return <LoadingPage />;