-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
62 lines (52 loc) · 1.76 KB
/
App.tsx
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
import React, { useEffect } from 'react';
import axios from 'axios';
import { loadData, loadMessages, selectUsers } from './components/chatAppSlice';
import { useAppDispatch, useAppSelector } from './app/hooks';
import ChatBox, { PostMessageProps } from './components/ChatBox/ChatBox';
import './App.css';
function App() {
const
dispatch = useAppDispatch(),
users = useAppSelector(selectUsers) || [],
getUsers = async () => {
const usersResp = await axios.get('/users');
return usersResp;
},
getMessages = async () => {
const messageResp = await axios.get('/messages');
return messageResp;
},
handlePost = (request: PostMessageProps) => {
axios.post('/messages', request).then((response) => {
getMessages().then((resp) => {
dispatch(loadMessages(resp.data));
});
}).catch((error) => { console.log(error) });
},
handleDelete = (id: string) => {
axios.delete(`/messages/${id}`)
.catch((error) => console.log(error));
};
useEffect(() => {
// load users
getUsers().then((response) => {
dispatch(loadData(response.data));
});
// load messages
getMessages().then((response) => {
dispatch(loadMessages(response.data));
});
}, [dispatch]);
return (
<div className="App">
<header className="App-header">
<h3 className="App-header-text">Chat App</h3>
</header>
<div className="App-content">
<ChatBox sender={users[0]} receiver={users[1]} onPost={handlePost} onDelete={handleDelete} />
<ChatBox sender={users[1]} receiver={users[0]} onPost={handlePost} onDelete={handleDelete} />
</div>
</div>
);
}
export default App;