sdy

add queries, variables

1 import React from "react"; 1 import React from "react";
2 -import { useSubscription, useMutation } from "@apollo/react-hooks"; 2 +import { useSubscription, 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 { NEW_MESSAGE, SUBSCRIPTION_MSG } from "./ChatQueries"; 5 +import {
6 + SUBSCRIPTION_MSG,
7 + WHOLE_MESSAGE,
8 + GET_ROOM_BY_NAME,
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";
12 +import getRoomByName from "../../../../back/src/api/Room/getRoomByName/getRoomByName";
8 13
9 export default withRouter(({ location }) => { 14 export default withRouter(({ location }) => {
10 - const [createMsg] = useMutation(NEW_MESSAGE); 15 + const { pathname } = location;
16 + const roomName = pathname.slice(1, pathname.length);
17 +
18 + let messageObj, outcomingMsg, roomId;
19 +
20 + if (roomName !== undefined) {
21 + const {
22 + data: { getRoomByName },
23 + } = useQuery(GET_ROOM_BY_NAME, { variables: { roomName } });
24 + roomId = getRoomByName.id;
25 + }
26 +
27 + const [createMsg] = useMutation(WHOLE_MESSAGE);
11 const { data } = useSubscription(SUBSCRIPTION_MSG); 28 const { data } = useSubscription(SUBSCRIPTION_MSG);
12 29
13 const message = useInput(""); 30 const message = useInput("");
14 - let messageObj, outcomingMsg;
15 31
16 const onSubmit = async (e) => { 32 const onSubmit = async (e) => {
17 e.preventDefault(); 33 e.preventDefault();
...@@ -20,10 +36,12 @@ export default withRouter(({ location }) => { ...@@ -20,10 +36,12 @@ export default withRouter(({ location }) => {
20 messageObj = await createMsg({ 36 messageObj = await createMsg({
21 variables: { 37 variables: {
22 message: message.value, 38 message: message.value,
39 + roomId,
23 }, 40 },
24 }); 41 });
25 const { text } = messageObj; 42 const { text } = messageObj;
26 outcomingMsg = text; 43 outcomingMsg = text;
44 + console.log(messageObj);
27 } catch { 45 } catch {
28 toast.error("text must be not empty"); 46 toast.error("text must be not empty");
29 } 47 }
......