Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Re-rendering of InAppChat #93

Merged
merged 2 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 3 additions & 46 deletions app/components/inappchat/inappchat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { useEffect, useState } from "react";
import { Rocketchat } from "@rocket.chat/sdk";
import { getMessages, sendMessage } from "./lib/api";
import styles from "../../styles/Inappchat.module.css";
import { emojify, emojis, messagesSortedByDate, rcURL, useSsl } from "./helpers";
import { emojify, messagesSortedByDate, rcURL, useSsl } from "./helpers";
import {
Message,
MessageBody,
MessageContainer,
TextInput,
Icon,
Box,
MessageHeader,
Expand All @@ -19,12 +18,11 @@ import {
MessageUsername,
} from "./lib/fuselage";
import MDPreview from "../mdpreview";
import InappchatTextInput from "./inappchattextinput";

const rcClient = new Rocketchat({ logger: console, protocol: "ddp" });

const InAppChat = ({ closeChat, cookies, rid }) => {
const [message, setMessage] = useState("");
const [emojiClicked, setEmojiClicked] = useState(false);
const [messages, setMessages] = useState([]);

useEffect(() => {
Expand All @@ -51,7 +49,6 @@ const InAppChat = ({ closeChat, cookies, rid }) => {
return;
}
const msg = await sendMessage(rid, message, cookies);
setMessage("");
setMessages([...messages, msg.message]);
};

Expand Down Expand Up @@ -97,47 +94,7 @@ const InAppChat = ({ closeChat, cookies, rid }) => {
)}
</Box>
</div>
{emojiClicked && (
<div className={styles.emojisHolder}>
{emojis.map((e) => (
<div
key={e.id}
className={styles.animatedEmoji}
dangerouslySetInnerHTML={{ __html: emojify(e.value) }}
onClick={() => {
setEmojiClicked((prevState) => !prevState);
sendMsg(e.value);
}}
/>
))}
</div>
)}
<TextInput
placeholder="Message"
value={message}
onChange={(e) => {
setMessage(e.target.value);
}}
onKeyDown={(e) => {
if (e.keyCode === 13) {
sendMsg(message);
}
}}
addon={
<>
{message.trim() !== "" ? (
<Icon onClick={() => sendMsg(message)} name="send" size="x20" />
) : (
<Icon
name="emoji"
size="x20"
onClick={(e) => setEmojiClicked((prevState) => !prevState)}
/>
)}
</>
}
w={"400px"}
/>
<InappchatTextInput sendMsg={sendMsg} />
</div>
);
};
Expand Down
64 changes: 64 additions & 0 deletions app/components/inappchat/inappchattextinput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { useState } from "react";
import { TextInput, Icon } from "./lib/fuselage";
import { emojify, emojis } from "./helpers";
import styles from "../../styles/Inappchat.module.css";

const InappchatTextInput = ({ sendMsg }) => {
const [emojiClicked, setEmojiClicked] = useState(false);
const [message, setMessage] = useState("");
return (
<>
{emojiClicked && (
<div className={styles.emojisHolder}>
{emojis.map((e) => (
<div
key={e.id}
className={styles.animatedEmoji}
dangerouslySetInnerHTML={{ __html: emojify(e.value) }}
onClick={() => {
setEmojiClicked((prevState) => !prevState);
sendMsg(e.value);
}}
/>
))}
</div>
)}
<TextInput
placeholder="Message"
value={message}
onChange={(e) => {
setMessage(e.target.value);
}}
onKeyDown={(e) => {
if (e.keyCode === 13) {
sendMsg(message);
setMessage("");
}
}}
addon={
<>
{message.trim() !== "" ? (
<Icon
onClick={() => {
sendMsg(message);
setMessage("");
}}
name="send"
size="x20"
/>
) : (
<Icon
name="emoji"
size="x20"
onClick={(e) => setEmojiClicked((prevState) => !prevState)}
/>
)}
</>
}
w={"400px"}
/>
</>
);
};

export default InappchatTextInput;