import React from "react"; import PropTypes from "prop-types"; import ModalDialog from "../modal-dialog"; import Button from "../button"; import AvatarEditorBody from "./sub-components/avatar-editor-body"; import StyledButtonsWrapper from "./styled-avatar-editor"; class AvatarEditor extends React.Component { constructor(props) { super(props); this.avatarEditorBodyRef = React.createRef(); this.state = { existImage: !!this.props.image, visible: props.visible, x: 0, y: 0, width: 0, height: 0, croppedImage: "", }; } onImageChange = (file) => { this.setState({ croppedImage: file, }); if (typeof this.props.onImageChange === "function") this.props.onImageChange(file); }; onDeleteImage = () => { this.setState({ existImage: false, }); if (typeof this.props.onDeleteImage === "function") this.props.onDeleteImage(); }; onSizeChange = (data) => { this.setState(data); }; onPositionChange = (data) => { this.setState(data); }; onLoadFileError = (error) => { if (typeof this.props.onLoadFileError === "function") this.props.onLoadFileError(error); }; onLoadFile = (file, needSave) => { if (typeof this.props.onLoadFile === "function") { var fileData = { existImage: this.state.existImage, position: { x: this.state.x, y: this.state.y, width: this.state.width, height: this.state.height, }, croppedImage: this.state.croppedImage, }; needSave ? this.props.onLoadFile(file, fileData) : this.props.onLoadFile(file); } if (!this.state.existImage) this.setState({ existImage: true }); }; onSaveButtonClick = () => { this.props.onSave && this.props.onSave(); this.avatarEditorBodyRef.current.onSaveImage(); }; onCancelButtonClick = () => { this.props.onCancel(); }; onClose = () => { if (this.state.visible) { this.setState({ visible: false }); this.props.onClose(); } }; componentDidUpdate(prevProps) { if (this.props.visible !== prevProps.visible) { this.setState({ visible: this.props.visible }); } if (this.props.image !== prevProps.image) { this.setState({ existImage: !!this.props.image }); } } keyPress = (e) => { if (e.keyCode === 13) { this.onSaveButtonClick(); } }; componentDidMount() { addEventListener("keydown", this.keyPress, false); } componentWillUnmount() { removeEventListener("keydown", this.keyPress, false); } render() { const { displayType, className, id, style, headerLabel, maxSize, accept, image, selectNewPhotoLabel, orDropFileHereLabel, unknownTypeError, maxSizeFileError, unknownError, saveButtonLabel, saveButtonLoading, useModalDialog, cancelButtonLabel, maxSizeLabel, } = this.props; return useModalDialog ? ( {headerLabel}