Skip to content

Commit 527b52a

Browse files
btfcookiesgeoffrey-wu
authored andcommitted
feat: replace built in alert with custom alert box
1 parent 070cc9e commit 527b52a

7 files changed

Lines changed: 76 additions & 24 deletions

File tree

client/play/mp/MultiplayerTossupBonusClient.js

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import TossupBonusClient from '../TossupBonusClient.js';
55
import { arrayToRange } from '../ranges.js';
66
import upsertPlayerItem from '../upsert-player-item.js';
77
import { setYear } from '../year-slider.js';
8+
import { showAlert } from './alert.js';
89

910
export const MultiplayerClientMixin = (ClientClass) => class extends ClientClass {
1011
constructor (room, userId, socket) {
@@ -50,19 +51,14 @@ export const MultiplayerClientMixin = (ClientClass) => class extends ClientClass
5051

5152
// if a banned/kicked user tries to join a this.room they were removed from this is the response
5253
ackRemovedFromRoom ({ removalType }) {
53-
if (removalType === 'kick') {
54-
window.alert('You were kicked from this room by room players, and cannot rejoin it.');
55-
} else {
56-
window.alert('You were banned from this room by the room owner, and cannot rejoin it.');
57-
}
58-
setTimeout(() => {
59-
window.location.replace('../');
60-
}, 100);
54+
const message = removalType === 'kick'
55+
? 'You were kicked from this room by room players, and cannot rejoin it.'
56+
: 'You were banned from this room by the room owner, and cannot rejoin it.';
57+
showAlert(message, () => window.location.replace('../'));
6158
}
6259

6360
adminLock ({ message }) {
64-
window.alert(message);
65-
window.location.replace('/play/mp');
61+
showAlert(message, () => window.location.replace('/play/mp'));
6662
}
6763

6864
buzz ({ userId, username }) {
@@ -121,10 +117,7 @@ export const MultiplayerClientMixin = (ClientClass) => class extends ClientClass
121117

122118
confirmBan ({ targetId, targetUsername }) {
123119
if (targetId === this.USER_ID) {
124-
window.alert('You were banned from this room by the room owner.');
125-
setTimeout(() => {
126-
window.location.replace('../');
127-
}, 100);
120+
showAlert('You were banned from this room by the room owner.', () => window.location.replace('../'));
128121
} else {
129122
this.logEventConditionally(targetUsername + ' has been banned from this room.');
130123
}
@@ -301,12 +294,12 @@ export const MultiplayerClientMixin = (ClientClass) => class extends ClientClass
301294

302295
failedVotekickPoints ({ userId }) {
303296
if (userId === this.USER_ID) {
304-
window.alert('You can only votekick once you have answered a question correctly!');
297+
showAlert('You can only votekick once you have answered a question correctly!');
305298
}
306299
}
307300

308301
forceUsername ({ message, username }) {
309-
window.alert(message);
302+
showAlert(message);
310303
window.localStorage.setItem('multiplayer-username', username);
311304
document.querySelector('#username').value = username;
312305
}
@@ -378,8 +371,7 @@ export const MultiplayerClientMixin = (ClientClass) => class extends ClientClass
378371

379372
handleError ({ message }) {
380373
this.socket.close(3000);
381-
window.alert(message);
382-
window.location.href = '/multiplayer';
374+
showAlert(message, () => { window.location.href = '/multiplayer'; });
383375
}
384376

385377
join ({ isNew, team, user, userId, username }) {
@@ -764,10 +756,7 @@ export const MultiplayerClientMixin = (ClientClass) => class extends ClientClass
764756

765757
vkHandle ({ targetUsername, targetId }) {
766758
if (this.USER_ID === targetId) {
767-
window.alert('You were vote kicked from this room by others.');
768-
setTimeout(() => {
769-
window.location.replace('../');
770-
}, 100);
759+
showAlert('You were vote kicked from this room by others.', () => window.location.replace('../'));
771760
} else {
772761
this.logEventConditionally(targetUsername + ' has been vote kicked from this room.');
773762
}
@@ -793,6 +782,5 @@ function attachEventListeners (room, socket, client) {
793782
});
794783
});
795784
}
796-
797785
const MultiplayerTossupBonusClient = MultiplayerClientMixin(TossupBonusClient);
798786
export default MultiplayerTossupBonusClient;

client/play/mp/alert.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function showAlert (text, onClose) {
2+
const alertBox = document.getElementById('alert-box');
3+
const closeButton = alertBox.querySelector('.close-button');
4+
const alertBoxText = alertBox.querySelector('.alert-text');
5+
alertBoxText.textContent = text;
6+
alertBox.style.display = 'flex';
7+
alertBox.style.opacity = '1';
8+
closeButton.onclick = function () {
9+
alertBox.style.opacity = '0';
10+
alertBox.addEventListener('transitionend', () => {
11+
alertBox.style.display = 'none';
12+
onClose?.();
13+
}, { once: true });
14+
};
15+
}

client/play/mp/room.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
<!--#include virtual="/nav/index.html" -->
1010

1111
<div class="container-fluid mt-3 mb-5 pb-5 px-xxl-5">
12+
<div id="alert-box" role="alertdialog" aria-modal="true" aria-labelledby="alert-box-text" style="display: none;">
13+
<div id="alert-box-dialog">
14+
<p id="alert-box-text" class="alert-text"></p>
15+
<button class="close-button btn btn-primary">OK</button>
16+
</div>
17+
</div>
1218
<div class="toast-container position-fixed top-0 end-0 p-3">
1319
<div id="star-toast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
1420
<div class="toast-header">

client/play/mp/room.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import MultiplayerTossupBonusClient from './MultiplayerTossupBonusClient.js';
2+
import { showAlert } from './alert.js';
23

34
import CategoryManager from '../../../quizbowl/category-manager.js';
45
import { getDropdownValues } from '../../scripts/utilities/dropdown-checklist.js';
@@ -52,7 +53,7 @@ socket.sendToServer = (data) => socket.send(JSON.stringify(data));
5253

5354
socket.onclose = function (event) {
5455
const { code } = event;
55-
if (code !== 3000) { window.alert('Disconnected from server'); }
56+
if (code !== 3000) { showAlert('Disconnected from server'); }
5657
clearInterval(PING_INTERVAL_ID);
5758
};
5859

scss/custom.scss

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,37 @@ a.list-group-item:hover {
151151
#set-settings {
152152
min-height: 90px;
153153
}
154+
155+
#alert-box {
156+
position: fixed;
157+
inset: 0;
158+
z-index: 1060;
159+
display: flex;
160+
align-items: center;
161+
justify-content: center;
162+
background-color: rgba(0, 0, 0, 0.5);
163+
opacity: 1;
164+
transition: opacity 0.15s ease;
165+
}
166+
167+
#alert-box-dialog {
168+
background-color: var(--bs-body-bg);
169+
color: var(--bs-body-color);
170+
border: 1px solid var(--bs-border-color);
171+
border-radius: var(--bs-border-radius-lg);
172+
padding: 1.5rem;
173+
max-width: 400px;
174+
width: 90%;
175+
box-shadow: var(--alert-dialog-shadow);
176+
display: flex;
177+
flex-direction: column;
178+
gap: 1rem;
179+
180+
.alert-text {
181+
margin: 0;
182+
}
183+
184+
.close-button {
185+
align-self: flex-end;
186+
}
187+
}

scss/themes/light.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ $link-color: #003399;
1313
.timer {
1414
background: linear-gradient(#ddd, #eee);
1515
}
16+
17+
#alert-box-dialog {
18+
--alert-dialog-shadow: 0 0.5rem 1.5rem rgba(0, 0, 0, 0.15);
19+
}
1620
}

scss/themes/night.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,8 @@ $form-switch-checked-bg-image-night: url("data:image/svg+xml,<svg xmlns='http://
211211
.vr {
212212
background-color: #{rgba($black, .55)};
213213
}
214+
215+
#alert-box-dialog {
216+
--alert-dialog-shadow: 0 0.5rem 1.5rem rgba(0, 0, 0, 0.5);
217+
}
214218
}

0 commit comments

Comments
 (0)