All files / molecule/photoUploader/src/ThumbCard index.js

21.42% Statements 3/14
0% Branches 0/14
0% Functions 0/5
21.42% Lines 3/14

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 143 144 145 146 147 148 149 150 151 152                                1x                                                                                                                                                                                                                             1x   1x                                            
import cx from 'classnames'
import PropTypes from 'prop-types'
 
import AtomButton, {atomButtonDesigns, atomButtonSizes} from '@s-ui/react-atom-button'
import AtomIcon, {ATOM_ICON_COLORS, ATOM_ICON_SIZES} from '@s-ui/react-atom-icon'
 
import {DEFAULT_VIEW_TYPE, VIEW_TYPE} from './../config.js'
import {
  ACTION_THUMB_CARD_CLASS_NAME,
  BUTTON_THUMB_CARD_CLASS_NAME,
  CONTAINER_THUMB_CARD_CLASS_NAME,
  ICON_THUMB_CARD_CLASS_NAME,
  IMAGE_THUMB_CARD_CLASS_NAME,
  THUMB_CARD_CLASS_NAME
} from './config.js'
 
const ThumbCard = ({
  callbackDeleteItem,
  callbackRetryUpload,
  callbackRotateItem,
  content: Content = () => null,
  deleteButtonAriaLabel,
  deleteIcon,
  dragIcon,
  iconSize = ATOM_ICON_SIZES.small,
  image,
  index,
  mainPhotoLabel,
  outputImageAspectRatioDisabled,
  rejectPhotosIcon,
  retryButtonAriaLabel,
  retryIcon,
  rotateButtonAriaLabel,
  rotateIcon,
  viewType
}) => {
  const hasErrors = image.hasErrors
 
  const isDefaultView = viewType === DEFAULT_VIEW_TYPE
  const isListView = viewType === VIEW_TYPE.LIST
 
  const counterClass = cx(`${THUMB_CARD_CLASS_NAME}-counter`, {
    [`${THUMB_CARD_CLASS_NAME}-mainCounter`]: index === 0 && isDefaultView
  })
 
  const imageThumbClass = cx(IMAGE_THUMB_CARD_CLASS_NAME, {
    [`${IMAGE_THUMB_CARD_CLASS_NAME}--ratioDisabled`]: outputImageAspectRatioDisabled
  })
 
  const thumbCardLabel = index === 0 && isDefaultView ? mainPhotoLabel : index + 1
 
  return (
    <div className={THUMB_CARD_CLASS_NAME}>
      <div className={counterClass}>{thumbCardLabel}</div>
      <div className={CONTAINER_THUMB_CARD_CLASS_NAME}>
        {hasErrors ? (
          <div className={`${ICON_THUMB_CARD_CLASS_NAME}`}>
            <AtomIcon size={ATOM_ICON_SIZES.extraLarge}>{rejectPhotosIcon}</AtomIcon>
          </div>
        ) : (
          <>
            {isListView && <AtomIcon size={iconSize}>{dragIcon}</AtomIcon>}
            <img src={image.preview} className={imageThumbClass} />
          </>
        )}
      </div>
      <Content file={image} index={index} />
      <div className={ACTION_THUMB_CARD_CLASS_NAME}>
        <div className={BUTTON_THUMB_CARD_CLASS_NAME}>
          <AtomButton
            aria-label={`${deleteButtonAriaLabel} ${thumbCardLabel}`}
            design={atomButtonDesigns.LINK}
            fullWidth
            onClick={() => callbackDeleteItem(index)}
            size={atomButtonSizes.SMALL}
            tabIndex="0"
            type="button"
          >
            <div className={`${BUTTON_THUMB_CARD_CLASS_NAME}Icon`}>
              <AtomIcon color={ATOM_ICON_COLORS.currentColor} size={iconSize}>
                {deleteIcon}
              </AtomIcon>
            </div>
          </AtomButton>
        </div>
        {hasErrors ? (
          <div className={BUTTON_THUMB_CARD_CLASS_NAME}>
            <AtomButton
              aria-label={`${retryButtonAriaLabel} ${thumbCardLabel}`}
              design={atomButtonDesigns.LINK}
              fullWidth
              onClick={() => callbackRetryUpload(index)}
              size={atomButtonSizes.SMALL}
              tabIndex="0"
              type="button"
            >
              <div className={`${BUTTON_THUMB_CARD_CLASS_NAME}Icon`}>
                <AtomIcon color={ATOM_ICON_COLORS.currentColor} size={iconSize}>
                  {retryIcon}
                </AtomIcon>
              </div>
            </AtomButton>
          </div>
        ) : (
          <div className={BUTTON_THUMB_CARD_CLASS_NAME}>
            <AtomButton
              aria-label={`${rotateButtonAriaLabel} ${thumbCardLabel}`}
              design={atomButtonDesigns.LINK}
              fullWidth
              onClick={() => callbackRotateItem(index)}
              size={atomButtonSizes.SMALL}
              tabIndex="0"
              type="button"
            >
              <div className={`${BUTTON_THUMB_CARD_CLASS_NAME}Icon`}>
                <AtomIcon color={ATOM_ICON_COLORS.currentColor} size={iconSize}>
                  {rotateIcon}
                </AtomIcon>
              </div>
            </AtomButton>
          </div>
        )}
      </div>
    </div>
  )
}
 
ThumbCard.displayName = 'ThumbCard'
 
ThumbCard.propTypes = {
  callbackDeleteItem: PropTypes.func,
  callbackRetryUpload: PropTypes.func,
  callbackRotateItem: PropTypes.func,
  content: PropTypes.func,
  deleteButtonAriaLabel: PropTypes.string.isRequired,
  deleteIcon: PropTypes.node.isRequired,
  dragIcon: PropTypes.node.isRequired,
  iconSize: PropTypes.oneOf(Object.keys(ATOM_ICON_SIZES)),
  image: PropTypes.object.isRequired,
  index: PropTypes.number,
  mainPhotoLabel: PropTypes.string,
  outputImageAspectRatioDisabled: PropTypes.bool,
  rejectPhotosIcon: PropTypes.node.isRequired,
  retryButtonAriaLabel: PropTypes.string.isRequired,
  retryIcon: PropTypes.node.isRequired,
  rotateButtonAriaLabel: PropTypes.string.isRequired,
  rotateIcon: PropTypes.node.isRequired,
  viewType: PropTypes.oneOf(Object.keys(VIEW_TYPE))
}
 
export default ThumbCard