sdy

update onSubmit function

...@@ -4,21 +4,37 @@ import ChatPresenter from "./ChatPresenter"; ...@@ -4,21 +4,37 @@ 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 { NEW_MESSAGE, SUBSCRIPTION_MSG } from "./ChatQueries";
6 import useInput from "../../Hooks/useInput"; 6 import useInput from "../../Hooks/useInput";
7 +import { toast } from "react-toastify";
7 8
8 export default withRouter(({ location }) => { 9 export default withRouter(({ location }) => {
9 const [createMsg] = useMutation(NEW_MESSAGE); 10 const [createMsg] = useMutation(NEW_MESSAGE);
10 const { data } = useSubscription(SUBSCRIPTION_MSG); 11 const { data } = useSubscription(SUBSCRIPTION_MSG);
11 12
12 const message = useInput(""); 13 const message = useInput("");
14 + let messageObj, outcomingMsg;
13 15
14 const onSubmit = async (e) => { 16 const onSubmit = async (e) => {
15 e.preventDefault(); 17 e.preventDefault();
18 + if (message.value !== undefined || message.value !== "") {
19 + try {
20 + messageObj = await createMsg({
21 + variables: {
22 + message: message.value,
23 + },
24 + });
25 + const { text } = messageObj;
26 + outcomingMsg = text;
27 + } catch {
28 + toast.error("text must be not empty");
29 + }
30 + }
16 }; 31 };
17 -
18 - console.log(data);
19 - console.log(createMsg);
20 -
21 return ( 32 return (
22 - <ChatPresenter location={location} message={message} onSubmit={onSubmit} /> 33 + <ChatPresenter
34 + location={location}
35 + message={message}
36 + onSubmit={onSubmit}
37 + outcomingMsg={outcomingMsg}
38 + />
23 ); 39 );
24 }); 40 });
......