sdy

update subscription

1 -import React from "react"; 1 +import React, { useEffect, useState } 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";
...@@ -12,6 +12,7 @@ import useInput from "../../Hooks/useInput"; ...@@ -12,6 +12,7 @@ import useInput from "../../Hooks/useInput";
12 import { toast } from "react-toastify"; 12 import { toast } from "react-toastify";
13 13
14 export default withRouter(({ location }) => { 14 export default withRouter(({ location }) => {
15 + const [messageArr, setMessageArr] = useState([]);
15 const { pathname } = location; 16 const { pathname } = location;
16 const roomName = pathname.slice(1, pathname.length); 17 const roomName = pathname.slice(1, pathname.length);
17 const [createMsg] = useMutation(CREATE_MESSAGE); 18 const [createMsg] = useMutation(CREATE_MESSAGE);
...@@ -30,7 +31,12 @@ export default withRouter(({ location }) => { ...@@ -30,7 +31,12 @@ export default withRouter(({ location }) => {
30 roomNum = Number(roomId); 31 roomNum = Number(roomId);
31 } 32 }
32 33
33 - const { subscribeToMore, data: messageList } = useQuery(SEE_ALL_MESSAGE, { 34 + const {
35 + subscribeToMore,
36 + data: messageList,
37 + error: msgQueryError,
38 + loading: msgQueryyLoading,
39 + } = useQuery(SEE_ALL_MESSAGE, {
34 variables: { roomId: roomNum }, 40 variables: { roomId: roomNum },
35 }); 41 });
36 42
...@@ -43,12 +49,27 @@ export default withRouter(({ location }) => { ...@@ -43,12 +49,27 @@ export default withRouter(({ location }) => {
43 messageArray = messageList; 49 messageArray = messageList;
44 } 50 }
45 51
46 - let resultObj = subscribeToMore({ 52 + useEffect(() => {
47 - document: SUBSCRIPTION_MSG, 53 + if (msgQueryError) {
48 - updateQuery: (prev, { subscriptionData }) => { 54 + console.error(msgQueryError);
49 - if (!subscriptionData.data) return prev; 55 + }
50 - }, 56 + if (messageArr) {
51 - }); 57 + setMessageArr(messageArr.seeAllMessage);
58 + }
59 + }, [msgQueryyLoading]);
60 +
61 + const subscribeToNewMessage = () => {
62 + subscribeToMore({
63 + document: SUBSCRIPTION_MSG,
64 + updateQuery: (currentMessages, { subscriptionData }) => {
65 + if (!subscriptionData.data) return currentMessages;
66 + const newMessage = subscriptionData.data.subMessage;
67 + const updateMessages = currentMessages.seeAllMessage.concat(newMessage);
68 + setMessageArr(updateMessages);
69 + return { seeAllMessage: updateMessages };
70 + },
71 + });
72 + };
52 73
53 const onSubmit = async (e) => { 74 const onSubmit = async (e) => {
54 e.preventDefault(); 75 e.preventDefault();
...@@ -75,6 +96,7 @@ export default withRouter(({ location }) => { ...@@ -75,6 +96,7 @@ export default withRouter(({ location }) => {
75 message={message} 96 message={message}
76 onSubmit={onSubmit} 97 onSubmit={onSubmit}
77 messageArray={messageArray} 98 messageArray={messageArray}
99 + subscribeToNewMessage={subscribeToNewMessage}
78 /> 100 />
79 ); 101 );
80 }); 102 });
......