# All files / components/PhotoMessage index.js

100% Statements 15/15
100% Branches 10/10
100% Functions 1/1
100% Lines 15/15

    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

    3x

    3x

    3x

    3x

    3x

    3x

    3x

    3x

    3x

    3x

    3x

    3x

    3x

    3x

    3x

     import React from 'react';
    import PropTypes from 'prop-types';
    import styled from 'styled-components';

    import { FaCloudDownloadAlt } from 'react-icons/fa';

    const StyledPhotoContainer = styled.div`
      margin-top: -3px;
      margin-right: -6px;
      margin-left: -6px;

      div {
        position: relative;
        display: flex;
        overflow: hidden;
        justify-content: center;
        border-radius: 5px;
        max-height: 300px;
      }
    `;

    StyledPhotoContainer.displayName = 'PhotoContainer';

    const StyledDownloadButton = styled.button`
      color: #efe;
      display: flex;
      justify-content: center;
      background: none;
      border: none;
      font-size: 3.2em;
      outline: none;
      border: 1px solid #eee;
      border-radius: 100%;
      height: 100px;
      width: 100px;
    `;

    StyledDownloadButton.displayName = 'DownloadButton';

    const StyledDownloadActionsContainer = styled.div`
      position: absolute;
      top: 0;
      right: 0;
      left: 0;
      bottom: 0;
      background-color: rgba(0,0,0,0.5);
      border-radius: 5px;
      display: flex;
    `;

    const StyledText = styled.div`
      padding: 5px 0px;
      max-width: 300px;
      margin: auto;
    `;

    const PhotoMessage = (props) => {
      const {
        data, onDownload, onOpen, text,
      } = props;
      const {
        alt, uri, status, width, height,
      } = data;

      const StyledImageContainer = styled.div`
        position: relative;
        display: flex;
        overflow: hidden;
        justify-content: center;
        border-radius: 5px;
        max-height: 300px;
        width: ${width};
        width: ${height};

        img {
          height: 100%;
          min-height: 100px;
          user-select: none;
        }
      `;
      StyledImageContainer.displayName = 'ImageContainer';

      return (
        <StyledPhotoContainer>
          <StyledImageContainer
            onClick={onOpen}
            onKeyPress={onOpen}
          >
            <img
              src={uri}
              alt={alt}
            />
            {
              status
                && !status.download
                && (
                  <StyledDownloadActionsContainer>
                    {
                      !status.click
                      && (
                        <StyledDownloadButton
                          onClick={onDownload}
                        >
                          <FaCloudDownloadAlt />
                        </StyledDownloadButton>
                      )
                    }
                    {
                      typeof status.loading === 'number'
                        && status.loading !== 0
                        && <div>{status.loading}</div>
                    }
                  </StyledDownloadActionsContainer>
                )
            }
          </StyledImageContainer>
          {
            text
            && (
              <StyledText>
                {text}
              </StyledText>
            )
          }
        </StyledPhotoContainer>
      );
    };

    PhotoMessage.defaultProps = {
      text: '',
      data: {},
      onDownload: null,
      onOpen: null,
    };

    PhotoMessage.propTypes = {
      text: PropTypes.string,
      data: PropTypes.object,
      onDownload: PropTypes.func,
      onOpen: PropTypes.func,
    };

    PhotoMessage.displayName = 'PhotoMessage';

    export default PhotoMessage;