import React from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import md5 from 'md5'; import { toastSuccess, toastError } from '../../util/apiNotification'; import { createSubscribedElement } from '../UnstatedUtils'; import AppContainer from '../../services/AppContainer'; import PersonalContainer from '../../services/PersonalContainer'; import ImageCropModal from './ImageCropModal'; class ProfileImageSettings extends React.Component { constructor(appContainer) { super(); this.state = { show: false, src: null, }; this.imageRef = null; this.onSelectFile = this.onSelectFile.bind(this); this.hideModal = this.hideModal.bind(this); this.cancelModal = this.cancelModal.bind(this); this.onCropCompleted = this.onCropCompleted.bind(this); this.onClickSubmit = this.onClickSubmit.bind(this); } async onClickSubmit() { const { t, personalContainer } = this.props; try { await personalContainer.updateProfileImage(); toastSuccess(t('toaster.update_successed', { target: t('Set Profile Image') })); } catch (err) { toastError(err); } } generateGravatarSrc() { const email = this.props.personalContainer.state.email || ''; const hash = md5(email.trim().toLowerCase()); return `https://gravatar.com/avatar/${hash}`; } onSelectFile(e) { if (e.target.files && e.target.files.length > 0) { const reader = new FileReader(); reader.addEventListener('load', () => this.setState({ src: reader.result })); reader.readAsDataURL(e.target.files[0]); this.setState({ show: true }); } } async onCropCompleted(croppedImageUrl) { const { t, personalContainer } = this.props; personalContainer.setState({ croppedImageUrl }); try { await personalContainer.uploadAttachment(croppedImageUrl); toastSuccess(t('toaster.update_successed', { target: t('Upload Image') })); } catch (err) { toastError(err); } this.hideModal(); } showModal() { this.setState({ show: true }); } hideModal() { this.setState({ show: false }); } cancelModal() { this.hideModal(); } render() { const { t, personalContainer } = this.props; const { uploadedPictureSrc, isGravatarEnabled } = personalContainer.state; return (

{ personalContainer.changeIsGravatarEnabled(true) }} />

{ personalContainer.changeIsGravatarEnabled(false) }} />

{uploadedPictureSrc && (

)} {/* TODO GW-1218 create apiV3 for delete image */}
); } } const ProfileImageSettingsWrapper = (props) => { return createSubscribedElement(ProfileImageSettings, props, [AppContainer, PersonalContainer]); }; ProfileImageSettings.propTypes = { t: PropTypes.func.isRequired, // i18next appContainer: PropTypes.instanceOf(AppContainer).isRequired, personalContainer: PropTypes.instanceOf(PersonalContainer).isRequired, }; export default withTranslation()(ProfileImageSettingsWrapper);