2018-12-13 08:26:39 +03:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../../index';
|
2018-12-13 08:26:39 +03:00
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
|
|
|
|
|
|
|
export default class StatusMessageContextMenu extends React.Component {
|
2018-12-18 20:53:37 +03:00
|
|
|
static propTypes = {
|
|
|
|
// js-sdk User object. Not required because it might not exist.
|
|
|
|
user: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
2018-12-13 08:26:39 +03:00
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = {
|
2019-01-15 01:41:14 +03:00
|
|
|
message: this.comittedStatusMessage,
|
2018-12-13 08:26:39 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 01:41:14 +03:00
|
|
|
componentWillMount() {
|
|
|
|
const { user } = this.props;
|
|
|
|
if (!user) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
user.on("User._unstable_statusMessage", this._onStatusMessageCommitted);
|
|
|
|
}
|
|
|
|
|
2019-03-08 00:57:30 +03:00
|
|
|
componentWillUnmount() {
|
2019-01-15 01:41:14 +03:00
|
|
|
const { user } = this.props;
|
|
|
|
if (!user) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
user.removeListener(
|
|
|
|
"User._unstable_statusMessage",
|
|
|
|
this._onStatusMessageCommitted,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get comittedStatusMessage() {
|
|
|
|
return this.props.user ? this.props.user._unstable_statusMessage : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
_onStatusMessageCommitted = () => {
|
|
|
|
// The `User` object has observed a status message change.
|
|
|
|
this.setState({
|
|
|
|
message: this.comittedStatusMessage,
|
2019-01-16 02:04:26 +03:00
|
|
|
waiting: false,
|
2019-01-15 01:41:14 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-01-16 02:04:26 +03:00
|
|
|
_onClearClick = (e) => {
|
|
|
|
MatrixClientPeg.get()._unstable_setStatusMessage("");
|
|
|
|
this.setState({
|
|
|
|
waiting: true,
|
|
|
|
});
|
2018-12-18 20:53:37 +03:00
|
|
|
};
|
2018-12-13 08:26:39 +03:00
|
|
|
|
2018-12-18 20:53:37 +03:00
|
|
|
_onSubmit = (e) => {
|
2018-12-13 08:26:39 +03:00
|
|
|
e.preventDefault();
|
2018-12-13 09:07:03 +03:00
|
|
|
MatrixClientPeg.get()._unstable_setStatusMessage(this.state.message);
|
2019-01-16 02:04:26 +03:00
|
|
|
this.setState({
|
|
|
|
waiting: true,
|
|
|
|
});
|
2018-12-18 20:53:37 +03:00
|
|
|
};
|
2018-12-13 08:26:39 +03:00
|
|
|
|
2018-12-18 20:53:37 +03:00
|
|
|
_onStatusChange = (e) => {
|
2019-01-15 01:41:14 +03:00
|
|
|
// The input field's value was changed.
|
|
|
|
this.setState({
|
|
|
|
message: e.target.value,
|
|
|
|
});
|
2018-12-18 20:53:37 +03:00
|
|
|
};
|
2018-12-13 08:26:39 +03:00
|
|
|
|
|
|
|
render() {
|
2019-01-16 02:04:26 +03:00
|
|
|
const Spinner = sdk.getComponent('views.elements.Spinner');
|
|
|
|
|
2019-01-15 01:41:14 +03:00
|
|
|
let actionButton;
|
|
|
|
if (this.comittedStatusMessage) {
|
|
|
|
if (this.state.message === this.comittedStatusMessage) {
|
|
|
|
actionButton = <AccessibleButton className="mx_StatusMessageContextMenu_clear"
|
|
|
|
onClick={this._onClearClick}
|
|
|
|
>
|
|
|
|
<span>{_t("Clear status")}</span>
|
|
|
|
</AccessibleButton>;
|
|
|
|
} else {
|
|
|
|
actionButton = <AccessibleButton className="mx_StatusMessageContextMenu_submit"
|
|
|
|
onClick={this._onSubmit}
|
|
|
|
>
|
|
|
|
<span>{_t("Update status")}</span>
|
|
|
|
</AccessibleButton>;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
actionButton = <AccessibleButton className="mx_StatusMessageContextMenu_submit"
|
|
|
|
disabled={!this.state.message} onClick={this._onSubmit}
|
|
|
|
>
|
|
|
|
<span>{_t("Set status")}</span>
|
|
|
|
</AccessibleButton>;
|
|
|
|
}
|
2018-12-14 23:49:35 +03:00
|
|
|
|
2019-01-16 02:04:26 +03:00
|
|
|
let spinner = null;
|
|
|
|
if (this.state.waiting) {
|
|
|
|
spinner = <Spinner w="24" h="24" />;
|
|
|
|
}
|
|
|
|
|
2019-01-15 01:41:14 +03:00
|
|
|
const form = <form className="mx_StatusMessageContextMenu_form"
|
|
|
|
autoComplete="off" onSubmit={this._onSubmit}
|
|
|
|
>
|
|
|
|
<input type="text" className="mx_StatusMessageContextMenu_message"
|
|
|
|
key="message" placeholder={_t("Set a new status...")}
|
|
|
|
autoFocus={true} maxLength="60" value={this.state.message}
|
|
|
|
onChange={this._onStatusChange}
|
|
|
|
/>
|
2019-01-16 02:04:26 +03:00
|
|
|
<div className="mx_StatusMessageContextMenu_actionContainer">
|
|
|
|
{actionButton}
|
|
|
|
{spinner}
|
|
|
|
</div>
|
2018-12-13 08:26:39 +03:00
|
|
|
</form>;
|
|
|
|
|
2019-01-15 01:41:14 +03:00
|
|
|
return <div className="mx_StatusMessageContextMenu">
|
2018-12-13 08:26:39 +03:00
|
|
|
{ form }
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
}
|