Skip to content

Commit 1685016

Browse files
committed
implement channels & messages initialization
1 parent 97cd8f3 commit 1685016

File tree

7 files changed

+100
-11
lines changed

7 files changed

+100
-11
lines changed

frontend/index.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />
5-
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
5+
<link
6+
rel="icon"
7+
type="image/svg+xml"
8+
href="./src/assets/avatar-DIE1AEpS.jpg"
9+
/>
610
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>Vite + React</title>
11+
<title>Chat project</title>
812
</head>
913
<body>
1014
<div id="root" class="vh-100"></div>

frontend/src/App.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111

1212
import Header from './Components/Header.jsx';
1313
import Login from './Components/Login.jsx';
14-
import MainPage from './Components/MainPage.jsx';
14+
import ChatPage from './Components/ChatPage.jsx';
1515
import PageNotFound from './Components/PageNotFound.jsx';
1616

1717
import './index.css';
@@ -37,7 +37,7 @@ const App = () => (
3737
path="/"
3838
element={
3939
<PrivateRoute>
40-
<MainPage />
40+
<ChatPage />
4141
</PrivateRoute>
4242
}
4343
/>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import axios from 'axios';
2+
import { useEffect } from 'react';
3+
import { useNavigate } from 'react-router-dom';
4+
import { useDispatch, useSelector } from 'react-redux';
5+
6+
import { setChannels, setMessages } from '../features/chat/chatSlice';
7+
8+
function ChatPage() {
9+
const dispatch = useDispatch();
10+
const navigate = useNavigate();
11+
const { channels, messages } = useSelector((state) => state.chat);
12+
13+
useEffect(() => {
14+
const authToken = localStorage.getItem('token');
15+
16+
if (!authToken) {
17+
navigate('/login');
18+
return;
19+
}
20+
21+
const getData = async () => {
22+
try {
23+
const [channelsResponse, messagesResponse] = await Promise.all([
24+
axios.get('/api/v1/channels', {
25+
headers: {
26+
Authorization: `Bearer ${authToken}`,
27+
},
28+
}),
29+
axios.get('/api/v1/messages', {
30+
headers: {
31+
Authorization: `Bearer ${authToken}`,
32+
},
33+
}),
34+
]);
35+
36+
dispatch(setChannels(channelsResponse.data));
37+
dispatch(setMessages(messagesResponse.data));
38+
} catch (error) {
39+
console.error('Ошибка при загрузке данных:', error);
40+
}
41+
};
42+
43+
getData();
44+
}, [dispatch, navigate]);
45+
46+
return (
47+
<div>
48+
<h1>Стили будут завтра ;)</h1>
49+
<h2>Каналы:</h2>
50+
<ul>
51+
{channels.map((channel) => (
52+
<li key={channel.id}>{channel.name}</li>
53+
))}
54+
</ul>
55+
56+
<h2>Сообщения:</h2>
57+
<ul>
58+
{messages.map((message) => (
59+
<li key={message.id}>
60+
{message.username}: {message.body}
61+
</li>
62+
))}
63+
</ul>
64+
</div>
65+
);
66+
}
67+
68+
export default ChatPage;

frontend/src/Components/Login.jsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ const Login = () => {
5555
<Col md={8} xxl={6}>
5656
<Card className="shadow-sm">
5757
<Card.Body className="row p-5">
58-
{/* <Row className="align-items-center"> */}
5958
<Col
6059
md={6}
6160
className="d-flex align-items-center justify-content-center"
@@ -110,7 +109,6 @@ const Login = () => {
110109
</div>
111110
)}
112111
</Form>
113-
{/* </Row> */}
114112
</Card.Body>
115113
<Card.Footer className="card_footer text-center p-4">
116114
Нет аккаунта? <a href="#">Регистрация</a>

frontend/src/Components/MainPage.jsx

Lines changed: 0 additions & 5 deletions
This file was deleted.

frontend/src/app/store.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { configureStore } from '@reduxjs/toolkit';
22
import authReducer from '../features/auth/authSlice';
3+
import chatReducer from '../features/chat/chatSlice';
34

45
const store = configureStore({
56
reducer: {
67
auth: authReducer,
8+
chat: chatReducer,
79
},
810
});
911

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createSlice } from '@reduxjs/toolkit';
2+
3+
const initialState = {
4+
channels: [],
5+
messages: [],
6+
};
7+
8+
const chatSlice = createSlice({
9+
name: 'chat',
10+
initialState,
11+
reducers: {
12+
setChannels: (state, action) => {
13+
state.channels = action.payload;
14+
},
15+
setMessages: (state, action) => {
16+
state.messages = action.payload;
17+
},
18+
},
19+
});
20+
21+
export const { setChannels, setMessages } = chatSlice.actions;
22+
export default chatSlice.reducer;

0 commit comments

Comments
 (0)