|
| 1 | +import React from 'react'; |
| 2 | +import ImmutablePropTypes from 'react-immutable-proptypes'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | +import { defineMessages, injectIntl, FormattedMessage } from 'react-intl'; |
| 5 | +import ImmutablePureComponent from 'react-immutable-pure-component'; |
| 6 | +import Icon from 'mastodon/components/icon'; |
| 7 | +import Textarea from 'react-textarea-autosize'; |
| 8 | + |
| 9 | +const messages = defineMessages({ |
| 10 | + placeholder: { id: 'account_note.placeholder', defaultMessage: 'No comment provided' }, |
| 11 | +}); |
| 12 | + |
| 13 | +export default @injectIntl |
| 14 | +class Header extends ImmutablePureComponent { |
| 15 | + |
| 16 | + static propTypes = { |
| 17 | + account: ImmutablePropTypes.map.isRequired, |
| 18 | + isEditing: PropTypes.bool, |
| 19 | + isSubmitting: PropTypes.bool, |
| 20 | + accountNote: PropTypes.string, |
| 21 | + onEditAccountNote: PropTypes.func.isRequired, |
| 22 | + onCancelAccountNote: PropTypes.func.isRequired, |
| 23 | + onSaveAccountNote: PropTypes.func.isRequired, |
| 24 | + onChangeAccountNote: PropTypes.func.isRequired, |
| 25 | + intl: PropTypes.object.isRequired, |
| 26 | + }; |
| 27 | + |
| 28 | + handleChangeAccountNote = (e) => { |
| 29 | + this.props.onChangeAccountNote(e.target.value); |
| 30 | + }; |
| 31 | + |
| 32 | + componentWillUnmount () { |
| 33 | + if (this.props.isEditing) { |
| 34 | + this.props.onCancelAccountNote(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + handleKeyDown = e => { |
| 39 | + if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) { |
| 40 | + this.props.onSaveAccountNote(); |
| 41 | + } else if (e.keyCode === 27) { |
| 42 | + this.props.onCancelAccountNote(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + render () { |
| 47 | + const { account, accountNote, isEditing, isSubmitting, intl } = this.props; |
| 48 | + |
| 49 | + if (!account || (!accountNote && !isEditing)) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + let action_buttons = null; |
| 54 | + if (isEditing) { |
| 55 | + action_buttons = ( |
| 56 | + <div className='account__header__account-note__buttons'> |
| 57 | + <button className='text-btn' tabIndex='0' onClick={this.props.onCancelAccountNote} disabled={isSubmitting}> |
| 58 | + <Icon id='times' size={15} /> <FormattedMessage id='account_note.cancel' defaultMessage='Cancel' /> |
| 59 | + </button> |
| 60 | + <div className='flex-spacer' /> |
| 61 | + <button className='text-btn' tabIndex='0' onClick={this.props.onSaveAccountNote} disabled={isSubmitting}> |
| 62 | + <Icon id='check' size={15} /> <FormattedMessage id='account_note.save' defaultMessage='Save' /> |
| 63 | + </button> |
| 64 | + </div> |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + let note_container = null; |
| 69 | + if (isEditing) { |
| 70 | + note_container = ( |
| 71 | + <Textarea |
| 72 | + className='account__header__account-note__content' |
| 73 | + disabled={isSubmitting} |
| 74 | + placeholder={intl.formatMessage(messages.placeholder)} |
| 75 | + value={accountNote} |
| 76 | + onChange={this.handleChangeAccountNote} |
| 77 | + onKeyDown={this.handleKeyDown} |
| 78 | + autoFocus |
| 79 | + /> |
| 80 | + ); |
| 81 | + } else { |
| 82 | + note_container = (<div className='account__header__account-note__content'>{accountNote}</div>); |
| 83 | + } |
| 84 | + |
| 85 | + return ( |
| 86 | + <div className='account__header__account-note'> |
| 87 | + <div className='account__header__account-note__header'> |
| 88 | + <strong><FormattedMessage id='account.account_note_header' defaultMessage='Your note for @{name}' values={{ name: account.get('username') }} /></strong> |
| 89 | + {!isEditing && ( |
| 90 | + <div> |
| 91 | + <button className='text-btn' tabIndex='0' onClick={this.props.onEditAccountNote} disabled={isSubmitting}> |
| 92 | + <Icon id='pencil' size={15} /> <FormattedMessage id='account_note.edit' defaultMessage='Edit' /> |
| 93 | + </button> |
| 94 | + </div> |
| 95 | + )} |
| 96 | + </div> |
| 97 | + {note_container} |
| 98 | + {action_buttons} |
| 99 | + </div> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments