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 1975502 commit a1530cf
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions frontend/src/Components/mainPage/MainPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,33 @@ import axios from 'axios';
import { Container, Row, Spinner } from 'react-bootstrap';
import LeoProfanity from 'leo-profanity';
import { setChannelsList } from '../../slices/channelsSlice';
import { selectCurrentUser } from '../../slices/authSlice';
import { logout, selectCurrentUser } 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 fetchChannels = async (token, dispatch) => {
const res = await axios.get(routes.channelsPath(), {
headers: {
Authorization: `Bearer ${token}`,
},
});
dispatch(setChannelsList(res.data)); /* eslint-disable-line */
return res.data;
const fetchData = async (token, dispatch) => {
try {
const cahnnelsRes = await axios.get(routes.channelsPath(), {
headers: {
Authorization: `Bearer ${token}`,
},
});
dispatch(setChannelsList(cahnnelsRes.data)); /* eslint-disable-line */
const messagesRes = await axios.get(routes.messagesPath(), {
headers: {
Authorization: `Bearer ${token}`,
},
});
dispatch(setMessages(messagesRes.data)); /* eslint-disable-line */
} catch (e) {
dispatch(logout()); /* eslint-disable-line */
navigate('login'); /* eslint-disable-line */
}
};

const fetchMessages = async (token, dispatch) => {
const res = await axios.get(routes.messagesPath(), {
headers: {
Authorization: `Bearer ${token}`,
},
});
dispatch(setMessages(res.data)); /* eslint-disable-line */
return res.data;
};

const MainPage = () => {
const [loading, setLoading] = useState(true);
const currentUser = useSelector(selectCurrentUser);
Expand All @@ -49,8 +48,7 @@ const MainPage = () => {
if (!userString) { /* eslint-disable-line */
navigate('login'); /* eslint-disable-line */
} else { /* eslint-disable-line */
await fetchChannels(currentUser.token, dispatch); /* eslint-disable-line */
await fetchMessages(currentUser.token, dispatch); /* eslint-disable-line */
await fetchData /* eslint-disable-line */
subscribeToSocketEvents(); /* eslint-disable-line */
setLoading(false); /* eslint-disable-line */
}
Expand Down

0 comments on commit a1530cf

Please sign in to comment.