ichieh commited on
Commit
386e7a4
·
1 Parent(s): 4307a66

Rebuild frontend with improved UI and better user experience

Browse files
Files changed (1) hide show
  1. templates/index.html +127 -86
templates/index.html CHANGED
@@ -4,64 +4,80 @@
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>活動座位選擇</title>
7
- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
 
8
  <style>
 
 
 
 
 
 
 
9
  .table-circle {
10
  width: 200px;
11
  height: 200px;
12
  border-radius: 50%;
13
- background-color: #f8f9fa;
14
- border: 2px solid #dee2e6;
15
- margin: 20px;
16
  display: flex;
17
  flex-direction: column;
18
  align-items: center;
19
  justify-content: center;
20
  cursor: pointer;
21
- transition: all 0.3s;
 
22
  }
23
  .table-circle:hover {
24
- background-color: #e9ecef;
 
25
  }
26
  .table-circle.selected {
27
- border-color: #0d6efd;
28
  background-color: #e7f1ff;
29
  }
30
- .tables-container {
31
- display: flex;
32
- flex-wrap: wrap;
33
- justify-content: center;
34
- padding: 20px;
35
  }
36
- .participant-list {
37
- max-height: 100px;
38
- overflow-y: auto;
39
- font-size: 0.8em;
 
 
 
 
40
  }
41
  </style>
42
  </head>
43
  <body>
44
- <div class="container py-5">
45
- <h1 class="text-center mb-5">活動座位選擇</h1>
46
 
47
  <div class="row">
48
- <div class="col-md-8">
49
- <div class="tables-container" id="tablesContainer"></div>
 
 
50
  </div>
51
-
 
 
52
  <div class="col-md-4">
53
  <div class="card">
54
  <div class="card-body">
55
  <h5 class="card-title">登記資料</h5>
56
- <button type="button" class="btn btn-primary w-100" data-bs-toggle="modal" data-bs-target="#registrationForm">確認選位</button>
 
 
57
  </div>
58
  </div>
59
  </div>
60
  </div>
61
  </div>
62
 
63
- <!-- 報名表單 -->
64
- <div id="registrationForm" class="modal fade" tabindex="-1">
65
  <div class="modal-dialog">
66
  <div class="modal-content">
67
  <div class="modal-header">
@@ -97,48 +113,72 @@
97
  </div>
98
  </div>
99
 
 
 
100
  <script>
101
  let selectedTableId = null;
 
 
 
 
 
 
 
102
 
103
- async function loadTables() {
104
- const response = await fetch('/api/tables');
105
- const tables = await response.json();
106
- const container = document.getElementById('tablesContainer');
107
- container.innerHTML = '';
108
-
109
- tables.forEach(table => {
110
- const tableElement = document.createElement('div');
111
- tableElement.className = 'table-circle';
112
- tableElement.innerHTML = `
113
- <h3>桌號 ${table.id}</h3>
114
- <p>${table.current_seats}/${table.capacity} 個座位</p>
115
- <div class="participant-list">
116
- ${table.participants.map(p => `
117
- <div><a href="${p.fb_link}" target="_blank">${p.nickname}</a></div>
118
- `).join('')}
119
- </div>
120
- `;
121
-
122
- if (table.current_seats < table.capacity) {
123
- tableElement.onclick = () => selectTable(table.id);
124
- } else {
125
- tableElement.style.opacity = '0.5';
126
- tableElement.style.cursor = 'not-allowed';
127
- }
128
 
129
- container.appendChild(tableElement);
130
- });
 
 
 
 
 
 
 
 
 
 
 
131
  }
132
 
133
- function selectTable(tableId) {
134
- selectedTableId = tableId;
135
- document.querySelectorAll('.table-circle').forEach(el => {
136
- el.classList.remove('selected');
137
- });
138
- event.currentTarget.classList.add('selected');
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  }
140
 
141
- // 添加字元計數功能
142
  document.getElementById('introduction').addEventListener('input', function() {
143
  const maxLength = 500;
144
  const currentLength = this.value.length;
@@ -149,8 +189,7 @@
149
  }
150
  });
151
 
152
- // 更新提交函數
153
- function submitRegistration() {
154
  const nickname = document.getElementById('nickname').value;
155
  const email = document.getElementById('email').value;
156
  const fb_link = document.getElementById('fb_link').value;
@@ -165,43 +204,45 @@
165
  return;
166
  }
167
 
168
- const data = {
169
- table_id: selectedTableId,
170
- nickname: nickname,
171
- email: email,
172
- fb_link: fb_link || '',
173
- introduction: introduction || ''
174
- };
 
 
 
 
 
 
 
175
 
176
- fetch('/api/register', {
177
- method: 'POST',
178
- headers: {
179
- 'Content-Type': 'application/json',
180
- },
181
- body: JSON.stringify(data)
182
- })
183
- .then(response => response.json())
184
- .then(data => {
185
- if (data.error) {
186
- throw new Error(data.error);
187
  }
188
- Swal.fire({
 
189
  icon: 'success',
190
  title: '報名成功!',
191
  text: '您已成功報名座位'
192
- }).then(() => {
193
- $('#registrationForm').modal('hide');
194
- document.getElementById('seatRegistration').reset();
195
- loadTables();
196
  });
197
- })
198
- .catch(error => {
 
 
 
 
 
199
  Swal.fire({
200
  icon: 'error',
201
  title: '報名失敗',
202
  text: error.message
203
  });
204
- });
205
  }
206
 
207
  // 初始載入桌位資訊
 
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>活動座位選擇</title>
7
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
8
+ <link href="https://cdn.jsdelivr.net/npm/sweetalert2@11.3.0/dist/sweetalert2.min.css" rel="stylesheet">
9
  <style>
10
+ .table-container {
11
+ display: flex;
12
+ flex-wrap: wrap;
13
+ gap: 20px;
14
+ justify-content: center;
15
+ margin: 20px;
16
+ }
17
  .table-circle {
18
  width: 200px;
19
  height: 200px;
20
  border-radius: 50%;
21
+ border: 2px solid #ddd;
 
 
22
  display: flex;
23
  flex-direction: column;
24
  align-items: center;
25
  justify-content: center;
26
  cursor: pointer;
27
+ transition: all 0.3s ease;
28
+ background-color: white;
29
  }
30
  .table-circle:hover {
31
+ border-color: #007bff;
32
+ background-color: #f8f9fa;
33
  }
34
  .table-circle.selected {
35
+ border-color: #007bff;
36
  background-color: #e7f1ff;
37
  }
38
+ .table-circle.full {
39
+ cursor: not-allowed;
40
+ opacity: 0.7;
 
 
41
  }
42
+ .table-name {
43
+ font-size: 1.5rem;
44
+ font-weight: bold;
45
+ margin-bottom: 5px;
46
+ }
47
+ .seats-info {
48
+ font-size: 1rem;
49
+ color: #666;
50
  }
51
  </style>
52
  </head>
53
  <body>
54
+ <div class="container">
55
+ <h1 class="text-center my-4">活動座位選擇</h1>
56
 
57
  <div class="row">
58
+ <div class="col-md-12">
59
+ <div class="table-container" id="tablesContainer">
60
+ <!-- 桌子將由 JavaScript 動態生成 -->
61
+ </div>
62
  </div>
63
+ </div>
64
+
65
+ <div class="row justify-content-center mt-4">
66
  <div class="col-md-4">
67
  <div class="card">
68
  <div class="card-body">
69
  <h5 class="card-title">登記資料</h5>
70
+ <button type="button" class="btn btn-primary w-100" onclick="showRegistrationForm()" disabled id="registerBtn">
71
+ 確認選位
72
+ </button>
73
  </div>
74
  </div>
75
  </div>
76
  </div>
77
  </div>
78
 
79
+ <!-- 報名表單 Modal -->
80
+ <div class="modal fade" id="registrationForm" tabindex="-1">
81
  <div class="modal-dialog">
82
  <div class="modal-content">
83
  <div class="modal-header">
 
113
  </div>
114
  </div>
115
 
116
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
117
+ <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.3.0/dist/sweetalert2.all.min.js"></script>
118
  <script>
119
  let selectedTableId = null;
120
+ const registrationModal = new bootstrap.Modal(document.getElementById('registrationForm'));
121
+
122
+ function showRegistrationForm() {
123
+ if (selectedTableId) {
124
+ registrationModal.show();
125
+ }
126
+ }
127
 
128
+ function selectTable(tableId, isFull) {
129
+ if (isFull) return;
130
+
131
+ // 移除之前選擇的桌子的選中狀態
132
+ const previousSelected = document.querySelector('.table-circle.selected');
133
+ if (previousSelected) {
134
+ previousSelected.classList.remove('selected');
135
+ }
136
+
137
+ // 選中當前桌子
138
+ const currentTable = document.querySelector(`[data-table-id="${tableId}"]`);
139
+ if (currentTable) {
140
+ currentTable.classList.add('selected');
141
+ selectedTableId = tableId;
142
+ document.getElementById('registerBtn').disabled = false;
143
+ }
144
+ }
 
 
 
 
 
 
 
 
145
 
146
+ function createTableElement(table) {
147
+ const isFull = table.current_seats >= table.capacity;
148
+ const div = document.createElement('div');
149
+ div.className = `table-circle${isFull ? ' full' : ''}`;
150
+ div.setAttribute('data-table-id', table.id);
151
+ div.onclick = () => selectTable(table.id, isFull);
152
+
153
+ div.innerHTML = `
154
+ <div class="table-name">桌號 ${table.id}</div>
155
+ <div class="seats-info">${table.current_seats}/${table.capacity} 個座位</div>
156
+ `;
157
+
158
+ return div;
159
  }
160
 
161
+ async function loadTables() {
162
+ try {
163
+ const response = await fetch('/api/tables');
164
+ const data = await response.json();
165
+
166
+ const container = document.getElementById('tablesContainer');
167
+ container.innerHTML = '';
168
+
169
+ data.forEach(table => {
170
+ container.appendChild(createTableElement(table));
171
+ });
172
+ } catch (error) {
173
+ console.error('Error loading tables:', error);
174
+ Swal.fire({
175
+ icon: 'error',
176
+ title: '載入失敗',
177
+ text: '無法載入桌位資訊,請重新整理頁面'
178
+ });
179
+ }
180
  }
181
 
 
182
  document.getElementById('introduction').addEventListener('input', function() {
183
  const maxLength = 500;
184
  const currentLength = this.value.length;
 
189
  }
190
  });
191
 
192
+ async function submitRegistration() {
 
193
  const nickname = document.getElementById('nickname').value;
194
  const email = document.getElementById('email').value;
195
  const fb_link = document.getElementById('fb_link').value;
 
204
  return;
205
  }
206
 
207
+ try {
208
+ const response = await fetch('/api/register', {
209
+ method: 'POST',
210
+ headers: {
211
+ 'Content-Type': 'application/json',
212
+ },
213
+ body: JSON.stringify({
214
+ table_id: selectedTableId,
215
+ nickname,
216
+ email,
217
+ fb_link: fb_link || '',
218
+ introduction: introduction || ''
219
+ })
220
+ });
221
 
222
+ const data = await response.json();
223
+
224
+ if (!response.ok) {
225
+ throw new Error(data.error || '報名失敗');
 
 
 
 
 
 
 
226
  }
227
+
228
+ await Swal.fire({
229
  icon: 'success',
230
  title: '報名成功!',
231
  text: '您已成功報名座位'
 
 
 
 
232
  });
233
+
234
+ registrationModal.hide();
235
+ document.getElementById('seatRegistration').reset();
236
+ document.getElementById('registerBtn').disabled = true;
237
+ selectedTableId = null;
238
+ await loadTables();
239
+ } catch (error) {
240
  Swal.fire({
241
  icon: 'error',
242
  title: '報名失敗',
243
  text: error.message
244
  });
245
+ }
246
  }
247
 
248
  // 初始載入桌位資訊