Skip to content

Commit

Permalink
using cookies from component level
Browse files Browse the repository at this point in the history
  • Loading branch information
sidmohanty11 committed Mar 31, 2022
1 parent 8a4c3c3 commit 2d81f9d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 23 deletions.
8 changes: 5 additions & 3 deletions app/components/inappchat/inappchat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState } from "react";
import { Rocketchat } from "@rocket.chat/sdk";
import Cookie from 'js-cookie';
import { getMessages, sendMessage } from "./lib/api";
import styles from "../../styles/Inappchat.module.css";
import { emojify, messagesSortedByDate } from "./helpers";
Expand All @@ -22,8 +23,9 @@ import InappchatTextInput from "./inappchattextinput";

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

const InAppChat = ({ host, closeChat, cookies, rid }) => {
const InAppChat = ({ host, closeChat, rid }) => {
const [messages, setMessages] = useState([]);
const cookies = { rc_token: Cookie.get('rc_token'), rc_uid: Cookie.get('rc_uid') };
const isAuth = cookies.rc_token && cookies.rc_uid;
const rcURL = new URL(host);
const useSsl = !/http:\/\//.test(host);
Expand Down Expand Up @@ -97,15 +99,15 @@ const InAppChat = ({ host, closeChat, cookies, rid }) => {
) : (
<p>
Please login into{" "}
<a href={host} target="_blank">
<a href={host} rel="noopener noreferrer" target="_blank">
RocketChat
</a>{" "}
to chat!
</p>
)}
</Box>
</div>
{cookies.rc_token && cookies.rc_uid && <InappchatTextInput sendMsg={sendMsg} />}
{isAuth && <InappchatTextInput sendMsg={sendMsg} />}
</div>
);
};
Expand Down
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dompurify": "^2.3.5",
"emoji-toolkit": "^6.6.0",
"firebase": "^9.6.3",
"js-cookie": "^3.0.1",
"marked": "^4.0.12",
"next": "12.0.7",
"next-auth": "^4.2.1",
Expand Down
13 changes: 3 additions & 10 deletions app/pages/virtualconf/greenroom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import InAppChat from "../../../components/inappchat/inappchat";
import { Button } from "react-bootstrap";

const greenroom_rid = process.env.NEXT_PUBLIC_ROCKET_CHAT_GREENROOM_RID;
const host = process.env.NODE_ENV === "development" ? "http://localhost:3000" : "https://community.liaison.rocketchat.digital";

const Greenroom = ({ cookies }) => {
const Greenroom = () => {
const [openChat, setOpenChat] = useState(false);

const handleOpenChat = () => {
Expand All @@ -23,7 +24,7 @@ const Greenroom = ({ cookies }) => {
<div className={styles.container}>
</div>
{openChat ? (
<InAppChat host="http://localhost:3000" closeChat={handleOpenChat} cookies={cookies} rid={greenroom_rid} />
<InAppChat host={host} closeChat={handleOpenChat} rid={greenroom_rid} />
) : (
<Button style={{ float: 'right' }} onClick={handleOpenChat}>Open Chat</Button>
)}
Expand All @@ -41,11 +42,3 @@ const Greenroom = ({ cookies }) => {
}

export default Greenroom;

Greenroom.getInitialProps = ({ req }) => {
const cookies = req.cookies;

return {
cookies,
};
};
13 changes: 3 additions & 10 deletions app/pages/virtualconf/mainstage/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import Videostreamer from "../../../components/clientsideonly/videostreamer";
import InAppChat from '../../../components/inappchat/inappchat';

const rid = process.env.NEXT_PUBLIC_ROCKET_CHAT_CONF_RID;
const host = process.env.NODE_ENV === "development" ? "http://localhost:3000" : "https://community.liaison.rocketchat.digital";

export default function ConfMainStage({ cookies }) {
export default function ConfMainStage() {
const [openChat, setOpenChat] = useState(false);

const handleOpenChat = () => {
Expand All @@ -32,19 +33,11 @@ export default function ConfMainStage({ cookies }) {

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

ConfMainStage.getInitialProps = ({ req }) => {
const cookies = req.cookies;

return {
cookies,
};
};

0 comments on commit 2d81f9d

Please sign in to comment.