All files / report/components/LogoUpload index.jsx

92.31% Statements 24/26
62.5% Branches 25/40
91.67% Functions 11/12
100% Lines 24/24
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190                          1x               1x 1x 1x               1x 1x 1x   1x       1x 1x   1x       1x             1x                 1x         1x         1x         1x             1x                                                                 1x 1x   1x 1x                                                                                                   1x             1x                      
import React, { Component } from 'react';
import Dropzone from 'react-dropzone';
import PropTypes from 'prop-types';
import {
  Text,
  Button,
  CloseIcon,
} from '@bufferapp/components';
import ReactLoading from 'react-loading';
 
import styled from 'styled-components';
import { geyser, torchRed, outerSpaceLight, curiousBlue } from '@bufferapp/components/style/color';
 
const DropzoneWrapper = styled.div`
  transition: all 0.2s ease-out;
  float: right;
  height: 64px;
  width: 160px;
  position: relative;
  display: flex;
 
  border-width: ${props => props.logoUrl ? '0px' : '1px'};
  border-style: ${props => props.logoUrl ? 'none' : 'solid'};
  border-color: ${props => props.isLogoUploadErrored ? torchRed : '#b8b8b8' };
  border-radius: 4px;
 
  &:hover > button > span {
    opacity: 1.0;
  }
 
  &:hover {
    cursor: ${props => props.logoUrl ? 'default' : 'pointer'};
    border-width: ${props => props.logoUrl ? '0px' : '1px'};
    border-style: ${props => props.logoUrl ? 'none' : 'solid'};
    border-color: ${curiousBlue};
    box-shadow: ${props => props.logoUrl ? 'none' : '0 1px 2px ' + outerSpaceLight};
  }
 
  &:active {
    border-width: ${props => props.logoUrl ? '0px' : '1px'};
    border-style: ${props => props.logoUrl ? 'none' : 'solid'};
    border-color: '#b8b8b8';
    box-shadow: ${props => props.logoUrl ? 'none' : '0 1px 4px ' + outerSpaceLight};
  }
`;
 
const DropzoneContainer = styled.div`
  width: 100%;
  height: 100%;
  position: absolute;
  text-align: center;
`;
 
const LogoPlaceholder = styled.div`
  transition: all 0.2s ease-out;
  height: 64px;
  width: 160px;
  position: absolute;
  box-sizing: border-box;
  text-align: right;
`;
 
const LogoImage = styled.img`
  max-width: 100%;
  max-height: 100%;
`;
 
const DropZone = {
  height: '64px',
  width: '160px',
};
 
const UploadLogoText = styled.span`
  display: block;
  margin-top: 13px;
`;
 
const UploadingText = styled.span`
  display: block;
  padding-left: 48px;
  margin-top: -13px;
  //margin-top: 21px;
`;
 
const Box = styled.span`
  transition: all 0.2s ease-out;
  position: absolute;
  top: -6px;
  right: -6px;
  background: #FFFFFF;
  border: 1px solid ${geyser};
  box-sizing: border-box;
  box-shadow: 0px 1px 2px rgba(0, 0, 0, 0.05);
  width: 1.5rem;
  height: 1.5rem;
  align-items: center;
  justify-content: center;
  margin: 0 0 0 .25rem;
  z-index: 9999;
  border-radius: 50%;
  display: flex;
  opacity: 0;
 
  &:hover {
    cursor: pointer;
    border: 1px solid ${curiousBlue};
    box-shadow: 0 1px 2px ${outerSpaceLight};
  }
  &:active {
    border: 1px solid #0e5c98;
    box-shadow: 0 1px 4px ${outerSpaceLight};
  }
`;
 
class LogoUpload extends Component {
  render() {
    const { logoUrl, uploadLogo, deleteLogo, isLogoUploading,
      isLogoDropzoneDisabled, isLogoUploadErrored, exporting } = this.props;
    const deleteLogoAction = () => deleteLogo();
    // Do not display the logo box in export when logo is not set
    Iif (logoUrl === '' && exporting) return null;
    return (
      <DropzoneWrapper 
        isLogoUploading={isLogoUploading}
        logoUrl={logoUrl}
        isLogoUploadErrored={isLogoUploadErrored} >
        {!logoUrl &&
          <DropzoneContainer>
            <UploadLogoText>
              {!isLogoUploading &&
                <Text weight="medium" color="outerSpace">Upload logo</Text>
              }
            </UploadLogoText>
            {!isLogoUploading && !isLogoUploadErrored &&
                <Text size="small" color="shuttleGray">PNG or JPG</Text>
            }
            {!isLogoUploading && isLogoUploadErrored &&
                <Text size="small" color="torchRed">Oops! 5MB maximum</Text>
            }
            {isLogoUploading && 
              <UploadingText>
                <ReactLoading delay={0} className='loading' type='bubbles' color={geyser} />
              </UploadingText>
            }
          </DropzoneContainer>
        }
        <Dropzone
          name="file"
          disablePreview
          disableClick={isLogoDropzoneDisabled || logoUrl}
          onDropAccepted={uploadLogo}
          multiple={false}
          accept="image/png,image/jpeg"
          style={DropZone}
        >
          <LogoPlaceholder>
            <LogoImage src={logoUrl} />
          </LogoPlaceholder>
        </Dropzone>
        {logoUrl &&
          <Button noStyle onClick={deleteLogoAction}>
            <Box>
              <CloseIcon />
            </Box>
          </Button>
        }
      </DropzoneWrapper>
    );
  }
}
 
LogoUpload.defaultProps = {
  isLogoUploading: false,
  isLogoDropzoneDisabled: false,
  isLogoUploadErrored: false,
  exporting: false,
};
 
LogoUpload.propTypes = {
  logoUrl: PropTypes.string.isRequired,
  uploadLogo: PropTypes.func.isRequired,
  deleteLogo: PropTypes.func.isRequired,
  isLogoUploading: PropTypes.bool,
  isLogoDropzoneDisabled: PropTypes.bool,
  isLogoUploadErrored: PropTypes.bool,
  exporting: PropTypes.bool,
};
 
export default LogoUpload;