import React from 'react'; import md5 from 'md5'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import AppContainer from '~/client/services/AppContainer'; import PersonalContainer from '~/client/services/PersonalContainer'; import { toastSuccess, toastError } from '~/client/util/apiNotification'; import { withUnstatedContainers } from '../UnstatedUtils'; 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.onClickDeleteBtn = this.onClickDeleteBtn.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 }); } } /** * @param {object} croppedImage cropped profile image for upload */ async onCropCompleted(croppedImage) { const { t, personalContainer } = this.props; try { await personalContainer.uploadAttachment(croppedImage); toastSuccess(t('toaster.update_successed', { target: t('Current Image') })); } catch (err) { toastError(err); } this.hideModal(); } async onClickDeleteBtn() { const { t, personalContainer } = this.props; try { await personalContainer.deleteProfileImage(); toastSuccess(t('toaster.update_successed', { target: t('Current Image') })); } catch (err) { toastError(err); } } showModal() { this.setState({ show: true }); } hideModal() { this.setState({ show: false }); } cancelModal() { this.hideModal(); } render() { const { t, personalContainer } = this.props; const { uploadedPictureSrc, isGravatarEnabled, isUploadedPicture } = personalContainer.state; return (

{ personalContainer.changeIsGravatarEnabled(true) }} />

{ personalContainer.changeIsGravatarEnabled(false) }} />

{uploadedPictureSrc && (

)} {isUploadedPicture && }
); } } const ProfileImageSettingsWrapper = withUnstatedContainers(ProfileImageSettings, [AppContainer, PersonalContainer]); ProfileImageSettings.propTypes = { t: PropTypes.func.isRequired, // i18next appContainer: PropTypes.instanceOf(AppContainer).isRequired, personalContainer: PropTypes.instanceOf(PersonalContainer).isRequired, }; export default withTranslation()(ProfileImageSettingsWrapper);