ichieh commited on
Commit
0758969
·
1 Parent(s): 1335c94

Add loading screen and prevent duplicate submissions

Browse files
Files changed (1) hide show
  1. templates/index.html +29 -4
templates/index.html CHANGED
@@ -229,6 +229,7 @@
229
  });
230
 
231
  async function submitRegistration() {
 
232
  if (!selectedTableId) {
233
  Swal.fire({
234
  icon: 'error',
@@ -244,6 +245,7 @@
244
  const fb_link = document.getElementById('fb_link').value.trim();
245
  const introduction = document.getElementById('introduction').value.trim();
246
 
 
247
  if (!nickname || !email) {
248
  Swal.fire({
249
  icon: 'error',
@@ -254,6 +256,23 @@
254
  return;
255
  }
256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  try {
258
  const response = await fetch('/api/register', {
259
  method: 'POST',
@@ -271,7 +290,10 @@
271
 
272
  const result = await response.json();
273
 
274
- if (response.ok) {
 
 
 
275
  await Swal.fire({
276
  icon: 'success',
277
  title: '報名成功',
@@ -286,9 +308,6 @@
286
  document.getElementById('introduction').value = '';
287
  document.getElementById('charCount').textContent = '0';
288
 
289
- // 禁用確認按鈕
290
- document.getElementById('confirmBtn').disabled = true;
291
-
292
  // 清除選擇的桌號
293
  selectedTableId = null;
294
 
@@ -305,6 +324,9 @@
305
  await loadTables();
306
  }
307
  } catch (error) {
 
 
 
308
  console.error('Error:', error);
309
  Swal.fire({
310
  icon: 'error',
@@ -314,6 +336,9 @@
314
  });
315
  // 重新載入桌次資料以顯示最新狀態
316
  await loadTables();
 
 
 
317
  }
318
  }
319
  </script>
 
229
  });
230
 
231
  async function submitRegistration() {
232
+ // 檢查是否已選擇座位
233
  if (!selectedTableId) {
234
  Swal.fire({
235
  icon: 'error',
 
245
  const fb_link = document.getElementById('fb_link').value.trim();
246
  const introduction = document.getElementById('introduction').value.trim();
247
 
248
+ // 檢查必填欄位
249
  if (!nickname || !email) {
250
  Swal.fire({
251
  icon: 'error',
 
256
  return;
257
  }
258
 
259
+ // 禁用提交按鈕,避免重複送出
260
+ const confirmBtn = document.getElementById('confirmBtn');
261
+ confirmBtn.disabled = true;
262
+
263
+ // 顯示等待畫面
264
+ const loadingSwal = Swal.fire({
265
+ title: '處理中...',
266
+ text: '正在為您報名,請稍候',
267
+ allowOutsideClick: false,
268
+ allowEscapeKey: false,
269
+ allowEnterKey: false,
270
+ showConfirmButton: false,
271
+ didOpen: () => {
272
+ Swal.showLoading();
273
+ }
274
+ });
275
+
276
  try {
277
  const response = await fetch('/api/register', {
278
  method: 'POST',
 
290
 
291
  const result = await response.json();
292
 
293
+ // 關閉等待畫面
294
+ await loadingSwal.close();
295
+
296
+ if (response.ok) {
297
  await Swal.fire({
298
  icon: 'success',
299
  title: '報名成功',
 
308
  document.getElementById('introduction').value = '';
309
  document.getElementById('charCount').textContent = '0';
310
 
 
 
 
311
  // 清除選擇的桌號
312
  selectedTableId = null;
313
 
 
324
  await loadTables();
325
  }
326
  } catch (error) {
327
+ // 關閉等待畫面
328
+ await loadingSwal.close();
329
+
330
  console.error('Error:', error);
331
  Swal.fire({
332
  icon: 'error',
 
336
  });
337
  // 重新載入桌次資料以顯示最新狀態
338
  await loadTables();
339
+ } finally {
340
+ // 無論成功或失敗,都重新啟用提交按鈕
341
+ confirmBtn.disabled = false;
342
  }
343
  }
344
  </script>