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
|
|
|
|
2022-04-21 08:21:35 +03:00
|
|
|
import { CheckIcon, CloseIcon, EditIcon } from '../icons/index.js';
|
2022-04-21 08:21:02 +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();
|
2022-04-21 08:21:02 +03:00
|
|
|
} else {
|
|
|
|
this.handleHideForm();
|
2020-08-13 11:28:25 +03:00
|
|
|
}
|
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
|
|
|
|
2022-04-22 00:55:26 +03:00
|
|
|
const moderatorFlag = html`
|
|
|
|
<img src="/img/moderator-nobackground.svg" class="moderator-flag" />
|
|
|
|
`;
|
|
|
|
|
2021-11-03 05:28:19 +03:00
|
|
|
return html`
|
2022-04-21 08:21:02 +03:00
|
|
|
<div id="user-info">
|
|
|
|
<button
|
2021-11-03 05:28:19 +03:00
|
|
|
id="user-info-display"
|
|
|
|
style=${styles.info}
|
|
|
|
title="Click to update user name"
|
2022-04-21 08:21:02 +03:00
|
|
|
class="flex flex-row justify-end items-center align-middle cursor-pointer py-2 px-4 overflow-hidden w-full"
|
2021-11-03 05:28:19 +03:00
|
|
|
onClick=${this.handleDisplayForm}
|
|
|
|
>
|
2022-04-21 08:21:02 +03:00
|
|
|
<span id="username-display">Change Username</span>
|
|
|
|
<span><${EditIcon} /></span>
|
2022-04-21 08:21:35 +03:00
|
|
|
</button>
|
2021-11-03 05:27:41 +03:00
|
|
|
|
2021-11-03 05:28:19 +03:00
|
|
|
<div
|
|
|
|
id="user-info-change"
|
2022-04-21 08:21:35 +03:00
|
|
|
class="${displayForm
|
|
|
|
? 'flex'
|
|
|
|
: 'hidden'} flex-row flex-no-wrap h-full items-center justify-end"
|
2021-11-03 05:28:19 +03:00
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="username-change-input"
|
2022-04-21 08:21:02 +03:00
|
|
|
class="appearance-none block w-full bg-transparent rounded-md text-white p-2"
|
2021-11-03 05:28:19 +03:00
|
|
|
maxlength="60"
|
|
|
|
placeholder="Update username"
|
|
|
|
defaultValue=${username}
|
|
|
|
onKeydown=${this.handleKeydown}
|
|
|
|
onBlur=${this.handleBlur}
|
|
|
|
ref=${this.textInput}
|
|
|
|
/>
|
2022-04-21 08:21:02 +03:00
|
|
|
<div class="username-buttons-wrapper flex ml-2">
|
|
|
|
<button
|
|
|
|
id="button-update-username"
|
|
|
|
onClick=${this.handleUpdateUsername}
|
|
|
|
type="button"
|
|
|
|
class="bg-purple-500 hover:bg-purple-700 text-white uppercase p-1 ml-1 rounded-md cursor-pointer transition duration-100 user-btn"
|
|
|
|
>
|
2022-04-21 08:21:35 +03:00
|
|
|
<${CheckIcon} />
|
2022-04-21 08:21:02 +03:00
|
|
|
</button>
|
|
|
|
|
|
|
|
<button
|
|
|
|
id="button-cancel-change"
|
|
|
|
onClick=${this.handleHideForm}
|
|
|
|
type="button"
|
|
|
|
class="bg-gray-700 hover:bg-gray-500 text-white uppercase p-1 ml-1 rounded-md cursor-pointer transition duration-100 user-btn"
|
|
|
|
title="cancel"
|
|
|
|
>
|
2022-04-21 08:21:35 +03:00
|
|
|
<${CloseIcon} />
|
2022-04-21 08:21:02 +03:00
|
|
|
</button>
|
|
|
|
</div>
|
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
|
|
|
}
|
|
|
|
}
|