sdy

update Chat Container

...@@ -2,31 +2,41 @@ import React from "react"; ...@@ -2,31 +2,41 @@ import React from "react";
2 import { useMutation, useQuery } from "@apollo/react-hooks"; 2 import { useMutation, useQuery } from "@apollo/react-hooks";
3 import ChatPresenter from "./ChatPresenter"; 3 import ChatPresenter from "./ChatPresenter";
4 import { withRouter } from "react-router-dom"; 4 import { withRouter } from "react-router-dom";
5 -import { CREATE_MESSAGE, GET_ROOM_BY_NAME } from "./ChatQueries"; 5 +import {
6 + CREATE_MESSAGE,
7 + GET_ROOM_BY_NAME,
8 + SEE_ALL_MESSAGE,
9 +} from "./ChatQueries";
6 import useInput from "../../Hooks/useInput"; 10 import useInput from "../../Hooks/useInput";
7 import { toast } from "react-toastify"; 11 import { toast } from "react-toastify";
8 12
9 export default withRouter(({ location }) => { 13 export default withRouter(({ location }) => {
10 const { pathname } = location; 14 const { pathname } = location;
11 const roomName = pathname.slice(1, pathname.length); 15 const roomName = pathname.slice(1, pathname.length);
12 -
13 - let messageObj, roomNum, messageText, messageTime, newMsgObj;
14 -
15 - if (roomName !== undefined) {
16 - const { data } = useQuery(GET_ROOM_BY_NAME, { variables: { roomName } });
17 - if (data !== undefined) {
18 - const {
19 - getRoomByName: { id: roomId },
20 - } = data;
21 - roomNum = Number(roomId);
22 - }
23 - }
24 -
25 const [createMsg] = useMutation(CREATE_MESSAGE); 16 const [createMsg] = useMutation(CREATE_MESSAGE);
26 //const { data } = useSubscription(SUBSCRIPTION_MSG); 17 //const { data } = useSubscription(SUBSCRIPTION_MSG);
27 18
28 const message = useInput(""); 19 const message = useInput("");
29 20
21 + let messageObj, roomNum, messageText, messageTime, newMsgObj, messageArray;
22 +
23 + const { data: getRoom } = useQuery(GET_ROOM_BY_NAME, {
24 + variables: { roomName },
25 + });
26 + if (getRoom !== undefined) {
27 + const {
28 + getRoomByName: { id: roomId },
29 + } = getRoom;
30 + roomNum = Number(roomId);
31 + }
32 +
33 + const { data: messageList } = useQuery(SEE_ALL_MESSAGE, {
34 + variables: { roomId: roomNum },
35 + });
36 + if (messageList !== undefined) {
37 + messageArray = messageList;
38 + }
39 +
30 const onSubmit = async (e) => { 40 const onSubmit = async (e) => {
31 e.preventDefault(); 41 e.preventDefault();
32 if (message.value !== undefined || message.value !== "") { 42 if (message.value !== undefined || message.value !== "") {
...@@ -61,7 +71,8 @@ export default withRouter(({ location }) => { ...@@ -61,7 +71,8 @@ export default withRouter(({ location }) => {
61 onSubmit={onSubmit} 71 onSubmit={onSubmit}
62 messageText={messageText} 72 messageText={messageText}
63 messageTime={messageTime} 73 messageTime={messageTime}
64 - newMsgObj={sendingMsgObj} 74 + newMsgObj={newMsgObj}
75 + messageArray={messageArray}
65 /> 76 />
66 ); 77 );
67 }); 78 });
......