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-02-19 10:05:52 +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) {
|
|
|
|
if (event.keyCode === 13) { // enter
|
|
|
|
this.handleUpdateUsername();
|
|
|
|
} else if (event.keyCode === 27) { // esc
|
|
|
|
this.handleHideForm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:28:25 +03:00
|
|
|
render(props, state) {
|
2020-10-14 14:33:55 +03:00
|
|
|
const { username } = 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
|
|
|
|
2020-08-13 11:28:25 +03:00
|
|
|
return (
|
|
|
|
html`
|
2020-12-12 22:38:08 +03:00
|
|
|
<div id="user-info" class="whitespace-nowrap">
|
2020-08-22 09:44:10 +03:00
|
|
|
<div id="user-info-display" style=${styles.info} title="Click to update user name" class="flex flex-row justify-end items-center 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" class="text-indigo-600 text-xs font-semibold truncate overflow-hidden whitespace-no-wrap">${username}</span>
|
2020-08-21 01:29:15 +03:00
|
|
|
</div>
|
2020-08-13 11:28:25 +03:00
|
|
|
|
2020-12-12 22:38:08 +03:00
|
|
|
<div id="user-info-change" class="flex flex-row flex-no-wrap p-1 items-center justify-end" style=${styles.form}>
|
2020-08-21 01:29:15 +03:00
|
|
|
<input type="text"
|
|
|
|
id="username-change-input"
|
2020-08-22 09:44:10 +03:00
|
|
|
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"
|
2020-12-04 11:00:50 +03:00
|
|
|
maxlength="60"
|
2020-08-21 01:29:15 +03:00
|
|
|
placeholder="Update username"
|
2020-09-03 08:04:19 +03:00
|
|
|
defaultValue=${username}
|
2020-08-21 01:29:15 +03:00
|
|
|
onKeydown=${this.handleKeydown}
|
2020-12-12 22:08:23 +03:00
|
|
|
onFocus=${this.handleFocus}
|
|
|
|
onBlur=${this.handleBlur}
|
2020-08-21 01:29:15 +03:00
|
|
|
ref=${this.textInput}
|
|
|
|
/>
|
2020-08-22 09:44:10 +03:00
|
|
|
<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>
|
2020-08-17 19:00:36 +03:00
|
|
|
|
2020-08-22 09:44:10 +03:00
|
|
|
<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>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
}
|