Skip to content

Commit

Permalink
fix data fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman committed Aug 13, 2024
1 parent f8a3d7e commit 48ce72c
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions frontend/src/Components/mainPage/MainPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,50 @@ import axios from 'axios';
import { Container, Row, Spinner } from 'react-bootstrap';
import LeoProfanity from 'leo-profanity';
import { setChannelsList } from '../../slices/channelsSlice';
import { logout, selectCurrentUser } from '../../slices/authSlice';
import { selectCurrentUser, logout } from '../../slices/authSlice';
import { setMessages } from '../../slices/messagesSlice';
import subscribeToSocketEvents from '../../socket';
import routes from '../../routes';
import MainContainer from '../MainContainer';
import Channels from './Channels';
import Messages from './Messages';

const fetchData = async (token, dispatch) => {
let channelsRes;/* eslint-disable-line */
let messagesRes;/* eslint-disable-line */
const fetchChannels = async (token, dispatch, navigate) => {
try {
channelsRes = await axios.get(routes.channelsPath(), {/* eslint-disable-line */
const res = await axios.get(routes.channelsPath(), {
headers: {
Authorization: `Bearer ${token}`,
},
});
messagesRes = await axios.get(routes.messagesPath(), {/* eslint-disable-line */
dispatch(setChannelsList(res.data)); /* eslint-disable-line */
return res.data;
} catch (error) {
if (error.response) { /* eslint-disable-line */
dispatch(logout()); /* eslint-disable-line */
navigate('login'); /* eslint-disable-line */
}
throw error;
}
};

const fetchMessages = async (token, dispatch, navigate) => {
try {
const res = await axios.get(routes.messagesPath(), {
headers: {
Authorization: `Bearer ${token}`,
},
});
} catch (e) {
dispatch(logout()); /* eslint-disable-line */
navigate('login'); /* eslint-disable-line */
dispatch(setMessages(res.data)); /* eslint-disable-line */
return res.data;
} catch (error) {
if (error.response) { /* eslint-disable-line */
dispatch(logout()); /* eslint-disable-line */
navigate('login'); /* eslint-disable-line */
}
throw error;
}
console.log(channelsRes.data);/* eslint-disable-line */
console.log(messagesRes.data);/* eslint-disable-line */
dispatch(setChannelsList(channelsRes.data)); /* eslint-disable-line */
dispatch(setMessages(messagesRes.data)); /* eslint-disable-line */

};

const MainPage = () => {
const [loading, setLoading] = useState(true);
const currentUser = useSelector(selectCurrentUser);
Expand All @@ -53,17 +65,22 @@ const MainPage = () => {
if (!userString) { /* eslint-disable-line */
navigate('login'); /* eslint-disable-line */
} else { /* eslint-disable-line */
fetchData /* eslint-disable-line */
subscribeToSocketEvents(); /* eslint-disable-line */
setLoading(false); /* eslint-disable-line */
try {
await fetchChannels(currentUser.token, dispatch, navigate); /* eslint-disable-line */
await fetchMessages(currentUser.token, dispatch, navigate); /* eslint-disable-line */
subscribeToSocketEvents(); /* eslint-disable-line */
setLoading(false); /* eslint-disable-line */
} catch (error) {
// Ошибка уже обработана в fetchChannels и fetchMessages
}
}
};
loadPageData(); /* eslint-disable-line */
}, [currentUser, dispatch, navigate]);

return (
<MainContainer>
{(loading) ? (
{loading ? (
<div className="d-flex justify-content-center align-items-center vh-100">
<Spinner animation="border" role="status">
<span className="visually-hidden">Loading...</span>
Expand Down

0 comments on commit 48ce72c

Please sign in to comment.