Skip to content

Commit

Permalink
changing env var to host param
Browse files Browse the repository at this point in the history
  • Loading branch information
sidmohanty11 committed Mar 31, 2022
1 parent 6ae5ea5 commit 8a4c3c3
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 22 deletions.
1 change: 0 additions & 1 deletion app/components/inappchat/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { default as messagesSortedByDate } from './sortMessagesByDate';
export { default as emojify } from './emojify';
export { emojis } from './emojisThatAnimate';
export { useSsl, rcURL } from './url';
7 changes: 0 additions & 7 deletions app/components/inappchat/helpers/url.js

This file was deleted.

15 changes: 9 additions & 6 deletions app/components/inappchat/inappchat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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, messagesSortedByDate, rcURL, useSsl } from "./helpers";
import { emojify, messagesSortedByDate } from "./helpers";
import {
Message,
MessageBody,
Expand All @@ -22,8 +22,11 @@ import InappchatTextInput from "./inappchattextinput";

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

const InAppChat = ({ closeChat, cookies, rid }) => {
const InAppChat = ({ host, closeChat, cookies, rid }) => {
const [messages, setMessages] = useState([]);
const isAuth = cookies.rc_token && cookies.rc_uid;
const rcURL = new URL(host);
const useSsl = !/http:\/\//.test(host);

useEffect(() => {
const runRealtime = async (token, rid) => {
Expand All @@ -42,7 +45,7 @@ const InAppChat = ({ closeChat, cookies, rid }) => {
};
async function getData() {
try {
const data = await getMessages(rid, cookies);
const data = await getMessages(host, rid, cookies);
setMessages(data.messages);
} catch (err) {
console.log(err.message);
Expand All @@ -56,7 +59,7 @@ const InAppChat = ({ closeChat, cookies, rid }) => {
if (message.trim() === "") {
return;
}
const msg = await sendMessage(rid, message, cookies);
const msg = await sendMessage(host, rid, message, cookies);
setMessages([...messages, msg.message]);
};

Expand All @@ -67,7 +70,7 @@ const InAppChat = ({ closeChat, cookies, rid }) => {
</div>
<div className={styles.chatbox}>
<Box>
{cookies.rc_token && cookies.rc_uid ? (
{isAuth ? (
messagesSortedByDate(messages)?.map((m) => (
<Message className="customclass" clickable key={m._id}>
<MessageContainer>
Expand All @@ -94,7 +97,7 @@ const InAppChat = ({ closeChat, cookies, rid }) => {
) : (
<p>
Please login into{" "}
<a href="https://open.rocket.chat" target="_blank">
<a href={host} target="_blank">
RocketChat
</a>{" "}
to chat!
Expand Down
10 changes: 4 additions & 6 deletions app/components/inappchat/lib/api.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { rcURL } from "../helpers";

export const getMessages = async (rid, cookies) => {
export const getMessages = async (host, rid, cookies) => {
try {
const messages = await fetch(
`${rcURL.origin}/api/v1/channels.messages?roomId=${rid}`,
`${host}/api/v1/channels.messages?roomId=${rid}`,
{
headers: {
"Content-Type": "application/json",
Expand All @@ -20,9 +18,9 @@ export const getMessages = async (rid, cookies) => {
}
};

export const sendMessage = async (rid, message, cookies) => {
export const sendMessage = async (host, rid, message, cookies) => {
try {
const msg = await fetch(`${rcURL.origin}/api/v1/chat.sendMessage`, {
const msg = await fetch(`${host}/api/v1/chat.sendMessage`, {
body: `{"message": { "rid": "${rid}", "msg": "${message}" }}`,
headers: {
"Content-Type": "application/json",
Expand Down
2 changes: 1 addition & 1 deletion app/pages/virtualconf/greenroom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Greenroom = ({ cookies }) => {
<div className={styles.container}>
</div>
{openChat ? (
<InAppChat closeChat={handleOpenChat} cookies={cookies} rid={greenroom_rid} />
<InAppChat host="http://localhost:3000" closeChat={handleOpenChat} cookies={cookies} rid={greenroom_rid} />
) : (
<Button style={{ float: 'right' }} onClick={handleOpenChat}>Open Chat</Button>
)}
Expand Down
2 changes: 1 addition & 1 deletion app/pages/virtualconf/mainstage/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function ConfMainStage({ cookies }) {

</Container>
{openChat ? (
<InAppChat closeChat={handleOpenChat} cookies={cookies} rid={rid} />
<InAppChat host="http://localhost:3000" closeChat={handleOpenChat} cookies={cookies} rid={rid} />
) : (
<Button onClick={handleOpenChat}>Open Chat</Button>
)}
Expand Down

0 comments on commit 8a4c3c3

Please sign in to comment.