aboutsummaryrefslogtreecommitdiff
path: root/src/containers/AvatarCropModal/AvatarCropModal.tsx
blob: e5fcc0877d7f2355bbb75067c030a9ed746562fc (plain)
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
import React, { useState } from 'react';
import { makeStyles } from '@material-ui/core/styles';
import SendIcon from '@material-ui/icons/Send';
import { getCroppedImg } from './canvasUtils';

import ImageCropAreaSelect from '../../components/ImageCropAreaSelect/ImageCropAreaSelect';
import ModalScreen from '../../components/ModalScreen/ModalScreen';

interface Area {
  x: number;
  y: number;
  width?: number;
  height?: number;
}

interface PropTypes {
  avatar: string;
  callback: (file: File) => void;
}

const useStyles = makeStyles(theme => ({
  cropContainer: {
    position: 'relative',
    width: '100%',
    // height: '100vh',
    // background: '#333',
    [theme.breakpoints.up('sm')]: {
      // height: 400,
    }
  }
}));

const AvatarCropModal: React.FC<PropTypes> = ({ avatar, callback }) => {
  const classes = useStyles();
  const [area, setArea] = useState<Area>({ x: 0, y: 0 });

  const handleAction = async () => getCroppedImg(avatar, area)
    .then(croppedImage => callback(croppedImage));

  return (
    <ModalScreen
      title="Choose area"
      actionIcon={<SendIcon />}
      handleAction={handleAction}
      isActionDisabled={false}
    >
      <div className={classes.cropContainer}>
        <ImageCropAreaSelect
          image={avatar}
          setArea={setArea}
        />
      </div>
    </ModalScreen>
  );
};

export default AvatarCropModal;