All files / src/AttachmentViewer AttachmentImage.js

59.09% Statements 13/22
8.33% Branches 1/12
57.14% Functions 4/7
59.09% Lines 13/22

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                            9x 9x 9x 9x       9x 9x                                             9x                       9x 9x 9x   9x                                       2x 2x  
/* eslint-disable css-modules/no-unused-class */
 
/** * Libraries ** */
import React, { Component } from 'react';
import { Container } from '@zohodesk/components/es/Layout';
import { AttachmentImage_defaultProps } from './props/defaultProps';
import { AttachmentImage_propTypes } from './props/propTypes';
import { checkImageValidity } from './utils';
import Image from '../Image/Image';
/** * CSS ** */
import style from './AttachmentViewer.module.css';
 
export default class AttachmentImage extends Component {
  constructor(props) {
    super(props);
    this.state = { isLoading: true, isImageValid: false };
    this.handleImageValidation = this.handleImageValidation.bind(this);
    this.renderImage = this.renderImage.bind(this);
  }
 
  handleImageValidation(){
    const { src }= this.props;
    checkImageValidity(src).then((isValid) => {
      this.setState({ isLoading: false, isImageValid: isValid });
    })
  }
 
  renderImage(){
    const { src, onClick, alt, id, dataId, isCover, customClass, onLoad, onError } = this.props;
    const { isImageValid }= this.state;
    const { customImageClass= '' }= customClass;
    return <Image
            htmlId={id}
            dataId={isImageValid ? dataId : `${dataId}_alt`}
            src={src}
            onClick={onClick}
            className={customImageClass}
            alt={alt}
            isCover={isCover}
            onLoad={onLoad}
            onError={onError}
          /> 
  }
 
  componentDidMount(){
    this.handleImageValidation();
  }
 
  componentDidUpdate(prevProps){
    const { src }= this.props;
    if(prevProps.src !== src){
      this.setState({ isLoading: true });
      this.handleImageValidation();
    }
  }
 
  render() {
    const { dataId, id, customClass, children } = this.props;
    const { isLoading, isImageValid } = this.state;
    const { customChildrenClass='' }= customClass;
 
    return (
      <>
        {isLoading ? (
          <div className={`${style.spinLoad}`} data-id={`${dataId}_loader`} data-test-id={`${dataId}_loader`}>
            <div className={style.loader} />
          </div>
        ) : (
          isImageValid ? (
            this.renderImage()
          ) : children ? (
            <Container align='both' htmlId={id} dataId={`${dataId}_custom`} className={customChildrenClass}>{children}</Container>
          ) : (
            this.renderImage() //alt ui
          )
        )}
      </>
    );
  }
}
 
AttachmentImage.propTypes = AttachmentImage_propTypes;
AttachmentImage.defaultProps = AttachmentImage_defaultProps;