import React from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' import { defineMessages, injectIntl } from 'react-intl' import ImmutablePureComponent from 'react-immutable-pure-component' import ImmutablePropTypes from 'react-immutable-proptypes' import { saveUserProfileInformation } from '../../actions/user' import { me } from '../../initial_state' import Button from '../button' import Block from '../block' import Divider from '../divider' import FileInput from '../file_input' import Input from '../input' import Switch from '../switch' import Heading from '../heading' import Textarea from '../textarea' const messages = defineMessages({ edit_profile: { id: 'account.edit_profile', defaultMessage: 'Edit profile' }, headerPhoto: { id: 'header_photo', defaultMessage: 'Header photo' }, close: { id: 'lightbox.close', defaultMessage: 'Close' }, save: { id: 'lightbox.save', defaultMessage: 'Save' }, }) const mapStateToProps = (state) => ({ account: state.getIn(['accounts', me]), }) const mapDispatchToProps = (dispatch) => ({ onSave: (data) => dispatch(saveUserProfileInformation(data)), }) export default @injectIntl @connect(mapStateToProps, mapDispatchToProps) class EditProfileModal extends ImmutablePureComponent { static propTypes = { account: ImmutablePropTypes.map, intl: PropTypes.object.isRequired, onClose: PropTypes.func.isRequired, onSave: PropTypes.func.isRequired, } state = { avatarSrc: this.props.account ? this.props.account.get('avatar_static') : undefined, bioValue: this.props.account ? this.props.account.get('note_plain') : '', displayNameValue: this.props.account ? this.props.account.get('display_name_plain') : '', headerSrc: this.props.account ? this.props.account.get('header_static') : undefined, locked: this.props.account ? this.props.account.get('locked') : false, } componentDidUpdate (prevProps) { if (prevProps.account !== this.props.account) { if (this.props.account) { this.setState({ avatarSrc: this.props.account.get('avatar_static'), bioValue: this.props.account.get('note_plain'), displayNameValue: this.props.account.get('display_name_plain'), headerSrc: this.props.account.get('header_static'), locked: this.props.account.get('locked'), }) } else { this.setState({ avatarSrc: undefined, bioValue: '', displayNameValue: '', headerSrc: undefined, locked: false, }) } } } handleCoverPhotoChange = (e) => { try { this.setState({ headerSrc: e.target.files[0] }) } catch (error) { // } } handleProfilePhotoChange = (e) => { try { this.setState({ avatarSrc: e.target.files[0] }) } catch (error) { // } } handleDisplayNameChange = (value) => { this.setState({ displayNameValue: value }) } handleBioChange = (value) => { this.setState({ bioValue: value }) } handleLockedChange = ({ target }) => { this.setState({ locked: target.checked }) } handleOnClose = () => { this.props.onClose() } handleOnSave = () => { const { account } = this.props const { avatarSrc, bioValue, displayNameValue, headerSrc, locked, } = this.state const isVerified = account.get('is_verified') const obj = {} obj.locked = locked if (!isVerified && account.get('display_name_plain') !== displayNameValue) obj.displayName = displayNameValue if (account.get('note_plain') !== bioValue) obj.note = bioValue if (account.get('avatar_static') !== avatarSrc) obj.avatar = avatarSrc if (account.get('header_static') !== headerSrc) obj.header = headerSrc this.props.onSave(obj) this.handleOnClose() } render() { const { intl, account } = this.props const { avatarSrc, bioValue, displayNameValue, headerSrc, locked, } = this.state const isVerified = account.get('is_verified') return (