# All files / components/ConversationList index.js

97.22% Statements 35/36
90% Branches 9/10
95.24% Functions 20/21
97.14% Lines 34/35

    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
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170

    1x

    1x

    2x

    1x

    1x
    1x
    1x
    1x
    1x

    1x

    1x

    1x

    1x
    1x
    1x
    1x

    1x

    1x

    1x

    1x
    1x
    1x
    1x

    1x

    1x
    1x

    2x
    2x

    1x

    1x

    1x

    1x

    1x

    1x

     import React, { Component } from 'react';
    import PropTypes from 'prop-types';
    import { Icon } from 'react-icons-kit';
    import { ic_chat_bubble as ChatBubble } from 'react-icons-kit/md/ic_chat_bubble';
    import styled, { css } from 'styled-components';

    import { withAutoScroll } from '../../utils/hoc';
    import { animate, textColor } from '../../utils/style';

    import ConversationBox from '../ConversationBox';

    const MdChatBubble = (props) => <Icon icon={ChatBubble} {...props} />;

    const StyledConversationListWrapper = styled.div`
      display: flex;
      flex-direction: column;
      max-height: none;
      background-color: ${(props) => props.theme.palette.common.white};
      overflow-y: auto;
      ${css`
          animation: ${animate('slide-in')} 3s;
        `};
      `;

    const StyledConversationListSeparator = styled.div`
      margin-top: 10px;
      margin-left: 80px;
      padding: 20px 0px;

      color: ${(props) => textColor(props.theme, 'light', 'disabled')};
      font-size: ${(props) => props.theme.typography.classes.small.fontSize};
      line-height: ${(props) => props.theme.typography.classes.small.lineHeight};
      font-weight: ${(props) => props.theme.typography.weights.fontWeightMedium};
      border-top: 1px solid ${(props) => props.theme.palette.divider};
    `;

    const StyledConversationBoxContainer = styled.div`
      // transition: box-shadow .3s;
      // &:hover {
      //   box-shadow: ${(props) => props.theme.shadows['2p']};
      // }
    `;

    const StyledNoConversation = styled.div`
      position: absolute;
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center; /*centers items on the line (the x-axis by default)*/
      align-items: center; /*centers items on the cross-axis (y by default)*/
      flex-direction: column;

      color: ${(props) => textColor(props.theme, 'dark', 'disabled')};
      font-size: ${(props) => props.theme.typography.classes.body.fontSize};
      line-height: ${(props) => props.theme.typography.classes.body.lineHeight};
      font-weight: ${(props) => props.theme.typography.weights.fontWeightMedium};
    `;
    StyledNoConversation.displayName = 'StyledNoConversation';

    const StyledSvgContainer = styled.div`
      margin-bottom: 10px;
    `;

    const StyledEmptyContainer = styled.div`
      max-width: 80%;

      color: ${(props) => textColor(props.theme, 'dark', 'disabled')};
      font-size: ${(props) => props.theme.typography.classes.footnote.fontSize};
      line-height: ${(props) => props.theme.typography.classes.footnote.lineHeight};
      font-weight: ${(props) => props.theme.typography.weights.fontWeightMedium};
    `;

    class ConversationList extends Component {
      onClick(item, i, e) {
        const { clickItem } = this.props;
        // TODO change for this.props.clickItem instanceof Function
        // When thre is a jest fix to this bug: https://github.com/facebook/jest/issues/6329
        Eif (typeof clickItem === 'function') {
          clickItem(item, i, e);
        }
      }

      render() {
        const {
          clickItem, conversations, cmpRef, className, emptyStateText, noMoreDataText,
        } = this.props;
        return (
          <StyledConversationListWrapper
            innerRef={cmpRef}
            className={className}
          >
            {conversations.length > 0
              && (
                <div>
                  {conversations.map((conversation, index) => (
                    <StyledConversationBoxContainer
                      key={conversation.id}
                      onClick={clickItem && ((e) => this.onClick(conversation, index, e))}
                      onKeyPress={clickItem && ((e) => this.onClick(conversation, index, e))}
                    >
                      <ConversationBox
                        conversation={conversation}
                        key={`conversation-${conversation.id}`}
                      />
                    </StyledConversationBoxContainer>
                  ))
                  }
                  <StyledConversationListSeparator>{noMoreDataText}</StyledConversationListSeparator>
                </div>
              )
            }

            {conversations.length === 0
              && (
                <StyledNoConversation>
                  <StyledSvgContainer>
                    <MdChatBubble color="#e5e9ec" size={40} />
                  </StyledSvgContainer>
                  <StyledEmptyContainer>
                    {emptyStateText}
                  </StyledEmptyContainer>
                </StyledNoConversation>
              )
            }
          </StyledConversationListWrapper>
        );
      }
    }

    ConversationList.propTypes = {
      /**
       * conversations is an array of conversations objects
       * that will be displayed in the component
       */
      conversations: PropTypes.array,
      /**
       * Callback function to be called when an item is clicked
       */
      clickItem: PropTypes.func,
      /**
       * Extra className to style the component
       */
      className: PropTypes.string,
      /**
       * Text to be used to show noMoreDataText
       */
      noMoreDataText: PropTypes.string,
      /**
       * Text to be used when no conversations
       */
      emptyStateText: PropTypes.string,
      /**
       * cmpRef is a function that return this component inner reference
       */
      cmpRef: PropTypes.func,
    };

    ConversationList.defaultProps = {
      conversations: [],
      clickItem: null,
      cmpRef: null,
      emptyStateText: '',
      noMoreDataText: '',
    };

    ConversationList.displayName = 'ConversationList';

    const autoScrollOptions = { threshold: 300, direction: 'top' };
    export default withAutoScroll(autoScrollOptions)(ConversationList);