2020-06-30 20:19:50 +03:00
|
|
|
import React from 'react';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import Textarea from 'react-textarea-autosize';
|
2020-07-07 02:24:03 +03:00
|
|
|
import { is } from 'immutable';
|
2020-06-30 20:19:50 +03:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
2020-07-07 02:24:03 +03:00
|
|
|
placeholder: { id: 'account_note.placeholder', defaultMessage: 'Click to add a note' },
|
2020-06-30 20:19:50 +03:00
|
|
|
});
|
|
|
|
|
2020-07-07 02:24:03 +03:00
|
|
|
class InlineAlert extends React.PureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
show: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
mountMessage: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
static TRANSITION_DELAY = 200;
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (!this.props.show && nextProps.show) {
|
|
|
|
this.setState({ mountMessage: true });
|
|
|
|
} else if (this.props.show && !nextProps.show) {
|
|
|
|
setTimeout(() => this.setState({ mountMessage: false }), InlineAlert.TRANSITION_DELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { show } = this.props;
|
|
|
|
const { mountMessage } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span aria-live='polite' role='status' className='inline-alert' style={{ opacity: show ? 1 : 0 }}>
|
|
|
|
{mountMessage && <FormattedMessage id='generic.saved' defaultMessage='Saved' />}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class AccountNote extends ImmutablePureComponent {
|
2020-06-30 20:19:50 +03:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
account: ImmutablePropTypes.map.isRequired,
|
2020-07-07 02:24:03 +03:00
|
|
|
value: PropTypes.string,
|
|
|
|
onSave: PropTypes.func.isRequired,
|
2020-06-30 20:19:50 +03:00
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
2020-07-07 02:24:03 +03:00
|
|
|
state = {
|
|
|
|
value: null,
|
|
|
|
saving: false,
|
|
|
|
saved: false,
|
2020-06-30 20:19:50 +03:00
|
|
|
};
|
|
|
|
|
2020-07-07 02:24:03 +03:00
|
|
|
componentWillMount () {
|
|
|
|
this._reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
const accountWillChange = !is(this.props.account, nextProps.account);
|
|
|
|
const newState = {};
|
|
|
|
|
|
|
|
if (accountWillChange && this._isDirty()) {
|
|
|
|
this._save(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accountWillChange || nextProps.value === this.state.value) {
|
|
|
|
newState.saving = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.value !== nextProps.value) {
|
|
|
|
newState.value = nextProps.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState(newState);
|
|
|
|
}
|
|
|
|
|
2020-06-30 20:19:50 +03:00
|
|
|
componentWillUnmount () {
|
2020-07-07 02:24:03 +03:00
|
|
|
if (this._isDirty()) {
|
|
|
|
this._save(false);
|
2020-06-30 20:19:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 02:24:03 +03:00
|
|
|
setTextareaRef = c => {
|
|
|
|
this.textarea = c;
|
2023-01-30 02:45:35 +02:00
|
|
|
};
|
2020-07-07 02:24:03 +03:00
|
|
|
|
|
|
|
handleChange = e => {
|
|
|
|
this.setState({ value: e.target.value, saving: false });
|
|
|
|
};
|
|
|
|
|
2020-06-30 20:19:50 +03:00
|
|
|
handleKeyDown = e => {
|
|
|
|
if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
|
2020-07-07 02:24:03 +03:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
this._save();
|
|
|
|
|
|
|
|
if (this.textarea) {
|
|
|
|
this.textarea.blur();
|
|
|
|
}
|
2020-06-30 20:19:50 +03:00
|
|
|
} else if (e.keyCode === 27) {
|
2020-07-07 02:24:03 +03:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
this._reset(() => {
|
|
|
|
if (this.textarea) {
|
|
|
|
this.textarea.blur();
|
|
|
|
}
|
|
|
|
});
|
2020-06-30 20:19:50 +03:00
|
|
|
}
|
2023-01-30 02:45:35 +02:00
|
|
|
};
|
2020-06-30 20:19:50 +03:00
|
|
|
|
2020-07-07 02:24:03 +03:00
|
|
|
handleBlur = () => {
|
|
|
|
if (this._isDirty()) {
|
|
|
|
this._save();
|
|
|
|
}
|
2023-01-30 02:45:35 +02:00
|
|
|
};
|
2020-07-07 02:24:03 +03:00
|
|
|
|
|
|
|
_save (showMessage = true) {
|
|
|
|
this.setState({ saving: true }, () => this.props.onSave(this.state.value));
|
|
|
|
|
|
|
|
if (showMessage) {
|
|
|
|
this.setState({ saved: true }, () => setTimeout(() => this.setState({ saved: false }), 2000));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_reset (callback) {
|
|
|
|
this.setState({ value: this.props.value }, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
_isDirty () {
|
|
|
|
return !this.state.saving && this.props.value !== null && this.state.value !== null && this.state.value !== this.props.value;
|
|
|
|
}
|
|
|
|
|
2020-06-30 20:19:50 +03:00
|
|
|
render () {
|
2020-07-07 02:24:03 +03:00
|
|
|
const { account, intl } = this.props;
|
|
|
|
const { value, saved } = this.state;
|
2020-06-30 20:19:50 +03:00
|
|
|
|
2020-07-07 02:24:03 +03:00
|
|
|
if (!account) {
|
2020-06-30 20:19:50 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-07 02:24:03 +03:00
|
|
|
return (
|
|
|
|
<div className='account__header__account-note'>
|
|
|
|
<label htmlFor={`account-note-${account.get('id')}`}>
|
|
|
|
<FormattedMessage id='account.account_note_header' defaultMessage='Note' /> <InlineAlert show={saved} />
|
|
|
|
</label>
|
2020-06-30 20:19:50 +03:00
|
|
|
|
|
|
|
<Textarea
|
2020-07-07 02:24:03 +03:00
|
|
|
id={`account-note-${account.get('id')}`}
|
2020-06-30 20:19:50 +03:00
|
|
|
className='account__header__account-note__content'
|
2020-07-07 02:24:03 +03:00
|
|
|
disabled={this.props.value === null || value === null}
|
2020-06-30 20:19:50 +03:00
|
|
|
placeholder={intl.formatMessage(messages.placeholder)}
|
2020-07-07 02:24:03 +03:00
|
|
|
value={value || ''}
|
|
|
|
onChange={this.handleChange}
|
2020-06-30 20:19:50 +03:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2020-07-07 02:24:03 +03:00
|
|
|
onBlur={this.handleBlur}
|
|
|
|
ref={this.setTextareaRef}
|
2020-06-30 20:19:50 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-03-24 04:17:53 +02:00
|
|
|
|
|
|
|
export default injectIntl(AccountNote);
|