ChatContainer.js
1.53 KB
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
import React from "react";
import { useSubscription, useMutation, useQuery } from "@apollo/react-hooks";
import ChatPresenter from "./ChatPresenter";
import { withRouter } from "react-router-dom";
import {
SUBSCRIPTION_MSG,
WHOLE_MESSAGE,
GET_ROOM_BY_NAME,
} from "./ChatQueries";
import useInput from "../../Hooks/useInput";
import { toast } from "react-toastify";
import getRoomByName from "../../../../back/src/api/Room/getRoomByName/getRoomByName";
export default withRouter(({ location }) => {
const { pathname } = location;
const roomName = pathname.slice(1, pathname.length);
let messageObj, outcomingMsg, roomId;
if (roomName !== undefined) {
const {
data: { getRoomByName },
} = useQuery(GET_ROOM_BY_NAME, { variables: { roomName } });
roomId = getRoomByName.id;
}
const [createMsg] = useMutation(WHOLE_MESSAGE);
const { data } = useSubscription(SUBSCRIPTION_MSG);
const message = useInput("");
const onSubmit = async (e) => {
e.preventDefault();
if (message.value !== undefined || message.value !== "") {
try {
messageObj = await createMsg({
variables: {
message: message.value,
roomId,
},
});
const { text } = messageObj;
outcomingMsg = text;
console.log(messageObj);
} catch {
toast.error("text must be not empty");
}
}
};
return (
<ChatPresenter
location={location}
message={message}
onSubmit={onSubmit}
outcomingMsg={outcomingMsg}
/>
);
});