import React, { Component } from 'react' import Loading from '../components/Loading' class UserSettings extends Component { constructor(props) { super(props) this.state = { userImageMenuOpen: false, changePasswordOpen: false, changeName: false, loading: false, imagePreview: null, currentPassword: '', newPassword: '', newPasswordConfirmation: '', name: '', email: '', emailsAllowed: false, followTopicOnCreated: false, followTopicOnContributed: false, followMapOnCreated: false, followMapOnContributed: false, removeImage: '0' // can be '0' or '1', 0 means keep, 1 means remove } } componentDidMount = () => { this.setState({ name: this.props.currentUser.get('name'), email: this.props.currentUser.get('email'), image: this.props.currentUser.get('image'), emailsAllowed: this.props.currentUser.get('emails_allowed'), followTopicOnCreated: this.props.currentUser.get('follow_topic_on_created'), followTopicOnContributed: this.props.currentUser.get('follow_topic_on_contributed'), followMapOnCreated: this.props.currentUser.get('follow_map_on_created'), followMapOnContributed: this.props.currentUser.get('follow_map_on_contributed') }) } init = (serverData) => { Account.userIconUrl = serverData['user.png'] } initListeners = () => { /* $('#user_image').change(self.showImagePreview) */ } toggleChangePicture = () => { this.setState({ userImageMenuOpen: !this.state.userImageMenuOpen }) } openChangePicture = () => { this.setState({ userImageMenuOpen: true }) } closeChangePicture = () => { this.setState({ userImageMenuOpen: false }) } showLoading = () => { this.setState({ loading: true }) } showImagePreview = () => { var file = $('#user_image')[0].files[0] var reader = new window.FileReader() reader.onload = function(e) { var $canvas = $('').attr({ width: 84, height: 84 }) var context = $canvas[0].getContext('2d') var imageObj = new window.Image() imageObj.onload = function() { $('.userImageDiv canvas').remove() $('.userImageDiv img').hide() var imgWidth = imageObj.width var imgHeight = imageObj.height var dimensionToMatch = imgWidth > imgHeight ? imgHeight : imgWidth // draw cropped image var nonZero = Math.abs(imgHeight - imgWidth) / 2 var sourceX = dimensionToMatch === imgWidth ? 0 : nonZero var sourceY = dimensionToMatch === imgHeight ? 0 : nonZero var sourceWidth = dimensionToMatch var sourceHeight = dimensionToMatch var destX = 0 var destY = 0 var destWidth = 84 var destHeight = 84 context.drawImage(imageObj, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight) $('.userImageDiv').prepend($canvas) } imageObj.src = reader.result } if (file) { reader.readAsDataURL(file) $('.userImageMenu').hide() $('#remove_image').val('0') } } removePicture = () => { /* $('.userImageDiv canvas').remove() $('.userImageDiv img').attr('src', '/images/user.png').show() $('.userImageMenu').hide() var input = $('#user_image') input.replaceWith(input.val('').clone(true)) $('#remove_image').val('1') */ this.setState({ removeImage: '1' }) } changeName = () => { this.setState({ changeName: true }) } showPass = () => { this.setState({ changePasswordOpen: true }) } hidePass = () => { this.setState({ changePasswordOpen: false, currentPassword: '', newPassword: '', newPasswordConfirmation: '' }) } updateForKey = (key) => { return (event) => { const newState = {} if (event.target.type === 'checkbox') { newState[key] = !this.state[key] } else { newState[key] = event.target.value } this.setState(newState) } } render = () => { const id = this.props.currentUser.get('id') const name = this.props.currentUser.get('name') return (

Edit Settings

11835c3
{this.state.userImageMenuOpen &&
  • Upload Photo
  • Remove
  • Cancel
}
{!this.state.changeName &&
{ name }
} {this.state.changeName &&
}
{!this.state.changePasswordOpen &&
Change Password
} {this.state.changePasswordOpen &&
Oops, don't change password
}
{this.state.loading && }
) } } export default UserSettings