All files / components/ChatListShell ChatListShell.js

51.51% Statements 17/33
26.08% Branches 6/23
50% Functions 4/8
53.12% Lines 17/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142                    2x 2x   2x             2x       2x 2x 2x   2x 2x     2x 2x     2x       2x 2x                                         2x                                                                                                                                 2x             2x              
import { ArrowLeftOutlined } from "@ant-design/icons";
import { useSubscription } from "@apollo/client";
import { Select, Tabs } from "antd";
import PropTypes from "prop-types";
import React, { useContext, useEffect, useState } from "react";
import ChatContext from "../../context/chat/ChatContext";
import { LOAD_USERS } from "../../GraphQL/Queries";
import ChatList from "../ChatList/ChatList";
import SearchList from "../SearchList/SearchList";
import styles from "./ChatListShell.module.scss";
const { TabPane } = Tabs;
const { Option } = Select;
 
const ChatListShell = ({
  options,
  query,
  resetChatList,
  isUsername,
  setQueryMessagesBody,
}) => {
  const { fetchUnrepliedCount, newMessages, modifyCount } = useContext(
    ChatContext
  );
 
  const [chatOptions, setChatOptions] = useState([]);
  const [selectedOption, setSelectedOption] = useState("allchats");
  const { data } = useSubscription(LOAD_USERS);
 
  useEffect(() => {
    setChatOptions(options);
  }, [options]);
 
  useEffect(() => {
    fetchUnrepliedCount();
  }, []);
 
  const handleChatOptions = (value) => {
    setSelectedOption(value);
  };
 
  useEffect(() => {
    Iif (newMessages.length > 0) {
      // iterate to check if latest message inside each conversation matches newMessage
      let newMessageExist = false;
      if (newMessages[0].type === "received" && data && data.queryHC_Lead) {
        for (let i = 0; i < data.queryHC_Lead.length; i++) {
          const thread = data.queryHC_Lead[i];
          if (thread.threads.length > 0) {
            let messageId = thread.threads[0].messages[0]?.id;
            if (messageId === newMessages[0].id) {
              newMessageExist = true;
              break;
            }
          }
        }
      }
      if (!newMessageExist) {
        modifyCount(newMessages);
      }
    }
  }, [newMessages]);
 
  return (
    <div
      className={
        isUsername
          ? `${styles.ChatListShell__hide + " " + styles.ChatListShell}`
          : styles.ChatListShell
      }
    >
      <div className={styles.ChatListShell__bar}>
        {query.parameter.length === 0 ? (
          <Select
            defaultValue="allchats"
            bordered={false}
            value={selectedOption}
            data-testid="ChatListShell-select"
            onChange={handleChatOptions}
          >
            {chatOptions?.map((op) => {
              return (
                <Option key={op.id} value={op.value}>
                  {op.label}
                </Option>
              );
            })}
          </Select>
        ) : (
          <div>
            <ArrowLeftOutlined onClick={resetChatList} />
            <span style={{ marginLeft: "10px", fontWeight: "600" }}>
              Search
            </span>
          </div>
        )}
      </div>
      {query?.parameter.length === 0 ? (
        <div
          className={styles.ChatListShell__mobilebar}
          data-testid="ChatListShell-mobilebar"
        >
          <Tabs
            defaultActiveKey="allchats"
            onChange={handleChatOptions}
            centered
          >
            {chatOptions?.map((op) => {
              return <TabPane tab={op.label} key={op.value}></TabPane>;
            })}
          </Tabs>
        </div>
      ) : null}
      {query?.parameter.length === 0 ? (
        selectedOption === "allchats" ? (
          <ChatList data={data} />
        ) : (
          <div></div>
        )
      ) : (
        <SearchList query={query} setQueryMessagesBody={setQueryMessagesBody} />
      )}
    </div>
  );
};
 
export default ChatListShell;
 
ChatListShell.propTypes = {
  query: PropTypes.shape({
    parameter: PropTypes.string,
  }),
  resetChatList: PropTypes.func,
};
 
ChatListShell.defaultProps = {
  query: { parameter: "" },
  resetChatList: () =>
    console.log(
      "This function will reset the ChatListSheel to its default state"
    ),
};