forked from sendbird/sendbird-javascript-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (31 loc) · 985 Bytes
/
Copy pathindex.js
File metadata and controls
36 lines (31 loc) · 985 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { isEmpty, setCookie, getCookie } from './utils';
import { USER_ID, KEY_ENTER } from './const';
const userIdEl = document.querySelector('#user_id');
const nicknameEl = document.querySelector('#user_nickname');
const buttonEl = document.querySelector('#login-button');
document.addEventListener('DOMContentLoaded', () => {
const cookieUserId = getCookie(USER_ID);
if (cookieUserId) {
userIdEl.value = cookieUserId;
}
});
nicknameEl.addEventListener('keydown', e => {
if (e.which === KEY_ENTER) {
login();
}
});
buttonEl.addEventListener('click', () => {
login();
});
const login = () => {
const userId = userIdEl.value.trim();
const nickname = nicknameEl.value.trim();
if (isEmpty(nickname)) {
alert('Please enter user nickname');
return;
}
userIdEl.value = '';
nicknameEl.value = '';
setCookie(USER_ID, userId);
window.location.href = `chat.html?userid=${encodeURIComponent(userId)}&nickname=${encodeURIComponent(nickname)}`;
};