mirror of
https://github.com/Blah-IM/blahrs.git
synced 2025-05-01 08:41:09 +00:00
Simplify frontend duplicated code
This commit is contained in:
parent
d3c3961298
commit
4e78780569
1 changed files with 16 additions and 14 deletions
|
@ -2,10 +2,8 @@ const msgFlow = document.querySelector('#msg-flow');
|
||||||
const userPubkeyDisplay = document.querySelector('#user-pubkey');
|
const userPubkeyDisplay = document.querySelector('#user-pubkey');
|
||||||
const serverUrlInput = document.querySelector('#server-url');
|
const serverUrlInput = document.querySelector('#server-url');
|
||||||
const roomsInput = document.querySelector('#rooms');
|
const roomsInput = document.querySelector('#rooms');
|
||||||
const leaveRoomBtn = document.querySelector('#leave-room');
|
|
||||||
const joinNewRoomInput = document.querySelector('#join-new-room');
|
const joinNewRoomInput = document.querySelector('#join-new-room');
|
||||||
const chatInput = document.querySelector('#chat');
|
const chatInput = document.querySelector('#chat');
|
||||||
const regenKeyBtn = document.querySelector('#regen-key');
|
|
||||||
|
|
||||||
let serverUrl = null;
|
let serverUrl = null;
|
||||||
let curRoom = null;
|
let curRoom = null;
|
||||||
|
@ -316,7 +314,6 @@ async function joinRoom(ruuid) {
|
||||||
|
|
||||||
async function leaveRoom() {
|
async function leaveRoom() {
|
||||||
try {
|
try {
|
||||||
leaveRoomBtn.disabled = true;
|
|
||||||
await signAndPost(`${serverUrl}/room/${curRoom}/admin`, {
|
await signAndPost(`${serverUrl}/room/${curRoom}/admin`, {
|
||||||
room: curRoom,
|
room: curRoom,
|
||||||
typ: 'remove_member',
|
typ: 'remove_member',
|
||||||
|
@ -327,8 +324,6 @@ async function leaveRoom() {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
log(`failed to leave room: ${e}`);
|
log(`failed to leave room: ${e}`);
|
||||||
} finally {
|
|
||||||
leaveRoomBtn.disabled = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -417,6 +412,22 @@ window.onload = async (_) => {
|
||||||
await connectServer(serverUrlInput.value);
|
await connectServer(serverUrlInput.value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function onButtonClick(selector, handler) {
|
||||||
|
const el = document.querySelector(selector);
|
||||||
|
el.onclick = async () => {
|
||||||
|
try {
|
||||||
|
el.disabled = true;
|
||||||
|
await handler();
|
||||||
|
} finally {
|
||||||
|
el.disabled = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
onButtonClick('#leave-room', leaveRoom);
|
||||||
|
onButtonClick('#regen-key', generateKeypair);
|
||||||
|
onButtonClick('#refresh-rooms', async () => await loadRoomList(true));
|
||||||
|
|
||||||
serverUrlInput.onchange = async (e) => {
|
serverUrlInput.onchange = async (e) => {
|
||||||
await connectServer(e.target.value);
|
await connectServer(e.target.value);
|
||||||
};
|
};
|
||||||
|
@ -425,18 +436,9 @@ chatInput.onkeypress = async (e) => {
|
||||||
await postChat(chatInput.value);
|
await postChat(chatInput.value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
regenKeyBtn.onclick = async (_) => {
|
|
||||||
await generateKeypair();
|
|
||||||
};
|
|
||||||
leaveRoomBtn.onclick = async (_) => {
|
|
||||||
await leaveRoom();
|
|
||||||
};
|
|
||||||
roomsInput.onchange = async (_) => {
|
roomsInput.onchange = async (_) => {
|
||||||
await enterRoom(roomsInput.value);
|
await enterRoom(roomsInput.value);
|
||||||
};
|
};
|
||||||
joinNewRoomInput.onchange = async (_) => {
|
joinNewRoomInput.onchange = async (_) => {
|
||||||
await joinRoom(joinNewRoomInput.value);
|
await joinRoom(joinNewRoomInput.value);
|
||||||
};
|
};
|
||||||
document.querySelector('#refresh-rooms').onclick = async (_) => {
|
|
||||||
await loadRoomList(true);
|
|
||||||
};
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue