2020-10-05 04:43:31 +03:00
|
|
|
import { h, Component, createRef } from '/js/web_modules/preact.js';
|
|
|
|
import htm from '/js/web_modules/htm.js';
|
2020-08-13 11:28:25 +03:00
|
|
|
const html = htm.bind(h);
|
|
|
|
|
2020-10-14 14:33:55 +03:00
|
|
|
import { setLocalStorage } from '../../utils/helpers.js';
|
2021-11-03 05:28:19 +03:00
|
|
|
import {
|
|
|
|
KEY_USERNAME,
|
|
|
|
KEY_CUSTOM_USERNAME_SET,
|
|
|
|
} from '../../utils/constants.js';
|
2020-08-13 11:28:25 +03:00
|
|
|
|
2020-08-17 19:00:36 +03:00
|
|
|
export default class UsernameForm extends Component {
|
2020-08-13 11:28:25 +03:00
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
displayForm: false,
|
2020-12-12 22:08:23 +03:00
|
|
|
isFocused: false,
|
2020-08-13 11:28:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
this.textInput = createRef();
|
|
|
|
|
|
|
|
this.handleKeydown = this.handleKeydown.bind(this);
|
|
|
|
this.handleDisplayForm = this.handleDisplayForm.bind(this);
|
|
|
|
this.handleHideForm = this.handleHideForm.bind(this);
|
|
|
|
this.handleUpdateUsername = this.handleUpdateUsername.bind(this);
|
2020-12-12 22:08:23 +03:00
|
|
|
this.handleFocus = this.handleFocus.bind(this);
|
|
|
|
this.handleBlur = this.handleBlur.bind(this);
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleDisplayForm() {
|
2020-08-24 13:30:42 +03:00
|
|
|
const { displayForm: curDisplay } = this.state;
|
2020-08-13 11:28:25 +03:00
|
|
|
this.setState({
|
2020-08-24 13:30:42 +03:00
|
|
|
displayForm: !curDisplay,
|
2020-08-13 11:28:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleHideForm() {
|
|
|
|
this.setState({
|
|
|
|
displayForm: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeydown(event) {
|
2021-11-03 05:28:19 +03:00
|
|
|
if (event.keyCode === 13) {
|
|
|
|
// enter
|
|
|
|
this.handleUpdateUsername();
|
|
|
|
} else if (event.keyCode === 27) {
|
|
|
|
// esc
|
|
|
|
this.handleHideForm();
|
|
|
|
}
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleUpdateUsername() {
|
2020-12-12 22:08:23 +03:00
|
|
|
const { username: curName, onUsernameChange } = this.props;
|
2020-08-13 11:28:25 +03:00
|
|
|
let newName = this.textInput.current.value;
|
|
|
|
newName = newName.trim();
|
|
|
|
if (newName !== '' && newName !== curName) {
|
|
|
|
setLocalStorage(KEY_USERNAME, newName);
|
2021-02-19 10:05:52 +03:00
|
|
|
// So we know that the user has set a custom name
|
|
|
|
setLocalStorage(KEY_CUSTOM_USERNAME_SET, true);
|
2020-12-12 22:08:23 +03:00
|
|
|
if (onUsernameChange) {
|
|
|
|
onUsernameChange(newName);
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
this.handleHideForm();
|
|
|
|
}
|
2020-12-12 22:08:23 +03:00
|
|
|
}
|
2020-08-13 11:28:25 +03:00
|
|
|
|
2020-12-12 22:08:23 +03:00
|
|
|
handleFocus() {
|
|
|
|
const { onFocus } = this.props;
|
|
|
|
if (onFocus) {
|
|
|
|
onFocus();
|
|
|
|
}
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 22:08:23 +03:00
|
|
|
handleBlur() {
|
|
|
|
const { onBlur } = this.props;
|
|
|
|
if (onBlur) {
|
|
|
|
onBlur();
|
|
|
|
}
|
2021-11-03 05:28:19 +03:00
|
|
|
}
|
2020-12-12 22:08:23 +03:00
|
|
|
|
2022-03-06 09:20:55 +03:00
|
|
|
componentDidUpdate({}, { displayForm }) {
|
|
|
|
if (this.state.displayForm && !displayForm) {
|
2022-03-06 09:20:23 +03:00
|
|
|
document.getElementById('username-change-input').select();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:28:25 +03:00
|
|
|
render(props, state) {
|
2021-11-03 05:27:41 +03:00
|
|
|
const { username, isModerator } = props;
|
2020-08-13 11:28:25 +03:00
|
|
|
const { displayForm } = state;
|
|
|
|
|
|
|
|
const styles = {
|
|
|
|
info: {
|
2020-08-24 13:30:42 +03:00
|
|
|
display: displayForm ? 'none' : 'flex',
|
2020-08-13 11:28:25 +03:00
|
|
|
},
|
|
|
|
form: {
|
2020-12-12 22:38:08 +03:00
|
|
|
display: displayForm ? 'flex' : 'none',
|
2020-08-13 11:28:25 +03:00
|
|
|
},
|
|
|
|
};
|
2020-08-24 07:23:16 +03:00
|
|
|
|
2021-11-17 04:59:59 +03:00
|
|
|
const moderatorFlag = html`
|
2021-12-01 02:54:29 +03:00
|
|
|
<img src="/img/moderator-nobackground.svg" class="moderator-flag" />
|
2021-11-17 04:59:59 +03:00
|
|
|
`;
|
2022-01-12 21:10:49 +03:00
|
|
|
const userIcon = html`
|
|
|
|
<img src="/img/user-icon.svg" class="user-icon-flag" />
|
|
|
|
`;
|
2021-11-17 04:59:59 +03:00
|
|
|
|
2021-11-03 05:28:19 +03:00
|
|
|
return html`
|
|
|
|
<div id="user-info" class="whitespace-nowrap">
|
|
|
|
<div
|
|
|
|
id="user-info-display"
|
|
|
|
style=${styles.info}
|
|
|
|
title="Click to update user name"
|
|
|
|
class="flex flex-row justify-end items-center align-middle cursor-pointer py-2 px-4 overflow-hidden w-full opacity-1 transition-opacity duration-200 hover:opacity-75"
|
|
|
|
onClick=${this.handleDisplayForm}
|
|
|
|
>
|
|
|
|
<span
|
|
|
|
id="username-display"
|
2022-01-06 22:29:25 +03:00
|
|
|
class="text-indigo-100 text-xs font-semibold truncate overflow-hidden whitespace-no-wrap ${isModerator &&
|
2021-11-03 05:28:19 +03:00
|
|
|
'moderator-flag'}"
|
2022-01-12 21:10:49 +03:00
|
|
|
>${isModerator ? moderatorFlag : userIcon}${username}</span
|
2021-11-03 05:28:19 +03:00
|
|
|
>
|
|
|
|
</div>
|
2021-11-03 05:27:41 +03:00
|
|
|
|
2021-11-03 05:28:19 +03:00
|
|
|
<div
|
|
|
|
id="user-info-change"
|
|
|
|
class="flex flex-row flex-no-wrap p-1 items-center justify-end"
|
|
|
|
style=${styles.form}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="username-change-input"
|
|
|
|
class="appearance-none block w-full bg-gray-200 text-gray-700 border border-black-500 rounded py-1 px-1 leading-tight text-xs focus:bg-white"
|
|
|
|
maxlength="60"
|
|
|
|
placeholder="Update username"
|
|
|
|
defaultValue=${username}
|
|
|
|
onKeydown=${this.handleKeydown}
|
|
|
|
onFocus=${this.handleFocus}
|
|
|
|
onBlur=${this.handleBlur}
|
|
|
|
ref=${this.textInput}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
id="button-update-username"
|
|
|
|
onClick=${this.handleUpdateUsername}
|
|
|
|
type="button"
|
|
|
|
class="bg-blue-500 hover:bg-blue-700 text-white text-xs uppercase p-1 mx-1 rounded cursor-pointer user-btn"
|
|
|
|
>
|
|
|
|
Update
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<button
|
|
|
|
id="button-cancel-change"
|
|
|
|
onClick=${this.handleHideForm}
|
|
|
|
type="button"
|
|
|
|
class="bg-gray-900 hover:bg-gray-800 py-1 px-2 mx-1 rounded cursor-pointer user-btn text-white text-xs uppercase text-opacity-50"
|
|
|
|
title="cancel"
|
|
|
|
>
|
|
|
|
X
|
|
|
|
</button>
|
2020-08-13 11:28:25 +03:00
|
|
|
</div>
|
2021-11-03 05:28:19 +03:00
|
|
|
</div>
|
|
|
|
`;
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
|
|
|
}
|