-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
302 lines (260 loc) · 12 KB
/
Copy pathapp.js
File metadata and controls
302 lines (260 loc) · 12 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// app.js - Frontend logic for index.html (Syntiox Ideas Portal)
(function () {
'use strict';
// ── Constants ──────────────────────────────────────────────────────────────
const API_BASE = '/api';
const CATEGORY_CONFIG = {
idea: { label: 'Idea', icon: '💡', placeholder: 'Describe your idea in detail. What problem does it solve? How could it improve Syntiox?' },
bug: { label: 'Bug Report', icon: '🐛', placeholder: 'Describe the bug. What happened? What did you expect? Steps to reproduce...' },
feedback: { label: 'Feedback', icon: '💬', placeholder: 'Share your experience. What do you love? What could be better?' },
qa: { label: 'Q&A', icon: '❓', placeholder: 'Ask anything about Syntiox. Our team will do their best to answer!' },
general: { label: 'General', icon: '📌', placeholder: 'Share anything on your mind regarding Syntiox...' },
};
const CONTACT_CONFIG = {
telegram: { label: 'Telegram Username', placeholder: '@yourusername', inputType: 'text' },
whatsapp: { label: 'WhatsApp Number', placeholder: '+94 77 123 4567', inputType: 'tel' },
number: { label: 'Phone Number', placeholder: '+94 77 123 4567', inputType: 'tel' },
none: { label: 'Anonymous', placeholder: '', inputType: 'text' },
};
// ── DOM References ─────────────────────────────────────────────────────────
const form = document.getElementById('submitForm');
const nameInput = document.getElementById('nameInput');
const contactValue = document.getElementById('contactValue');
const contactLabel = document.getElementById('contactLabel');
const contactValueGroup= document.getElementById('contactValueGroup');
const messageInput = document.getElementById('messageInput');
const charCount = document.getElementById('charCount');
const selectedCategory = document.getElementById('selectedCategory');
const submitBtn = document.getElementById('submitBtn');
const btnText = document.getElementById('btnText');
const btnIcon = document.getElementById('btnIcon');
const photoInput = document.getElementById('photoInput');
const photoPreview = document.getElementById('photoPreview');
const previewImg = document.getElementById('previewImg');
const removePhoto = document.getElementById('removePhoto');
const uploadPlaceholder= document.getElementById('uploadPlaceholder');
const photoUploadArea = document.getElementById('photoUploadArea');
const successOverlay = document.getElementById('successOverlay');
const closeSuccess = document.getElementById('closeSuccess');
const toast = document.getElementById('toast');
const toastIcon = document.getElementById('toastIcon');
const toastMessage = document.getElementById('toastMessage');
let currentCategory = 'idea';
let currentContact = 'telegram';
let photoBase64 = null;
let toastTimer = null;
// ── Category Pill Selection ────────────────────────────────────────────────
document.getElementById('categoryPills').addEventListener('click', (e) => {
const pill = e.target.closest('.category-pill');
if (!pill) return;
const cat = pill.dataset.cat;
currentCategory = cat;
selectedCategory.value = cat;
document.querySelectorAll('.category-pill').forEach(p => {
p.classList.remove('active');
p.setAttribute('aria-pressed', 'false');
});
pill.classList.add('active');
pill.setAttribute('aria-pressed', 'true');
// Update textarea placeholder
if (CATEGORY_CONFIG[cat]) {
messageInput.placeholder = CATEGORY_CONFIG[cat].placeholder;
}
});
// ── Contact Type Selection ─────────────────────────────────────────────────
document.getElementById('contactTypeGroup').addEventListener('click', (e) => {
const btn = e.target.closest('.contact-type-btn');
if (!btn) return;
const type = btn.dataset.contact;
currentContact = type;
document.querySelectorAll('.contact-type-btn').forEach(b => {
b.classList.remove('active');
b.setAttribute('aria-pressed', 'false');
});
btn.classList.add('active');
btn.setAttribute('aria-pressed', 'true');
// Update contact input
if (type === 'none') {
contactValueGroup.style.display = 'none';
contactValue.required = false;
} else {
contactValueGroup.style.display = 'flex';
contactValue.required = true;
const cfg = CONTACT_CONFIG[type];
contactLabel.textContent = cfg.label;
contactValue.placeholder = cfg.placeholder;
contactValue.type = cfg.inputType;
contactValue.value = '';
}
});
// ── Character Count ────────────────────────────────────────────────────────
messageInput.addEventListener('input', () => {
const count = messageInput.value.length;
charCount.textContent = count;
charCount.style.color = count > 2700 ? '#ef4444' : count > 2400 ? '#f59e0b' : 'var(--text-muted)';
});
// ── Photo Upload ───────────────────────────────────────────────────────────
photoInput.addEventListener('change', handlePhotoSelect);
// Drag & drop
photoUploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
photoUploadArea.classList.add('drag-over');
});
photoUploadArea.addEventListener('dragleave', () => {
photoUploadArea.classList.remove('drag-over');
});
photoUploadArea.addEventListener('drop', (e) => {
e.preventDefault();
photoUploadArea.classList.remove('drag-over');
const file = e.dataTransfer.files[0];
if (file) processPhotoFile(file);
});
removePhoto.addEventListener('click', (e) => {
e.stopPropagation();
clearPhoto();
});
function handlePhotoSelect(e) {
const file = e.target.files[0];
if (file) processPhotoFile(file);
}
function processPhotoFile(file) {
if (!file.type.startsWith('image/')) {
showToast('Please select a valid image file.', 'error');
return;
}
if (file.size > 5 * 1024 * 1024) {
showToast('Photo must be smaller than 5MB.', 'error');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
photoBase64 = e.target.result;
previewImg.src = photoBase64;
previewImg.alt = `Preview: ${file.name}`;
uploadPlaceholder.style.display = 'none';
photoPreview.style.display = 'block';
};
reader.readAsDataURL(file);
}
function clearPhoto() {
photoBase64 = null;
photoInput.value = '';
previewImg.src = '';
uploadPlaceholder.style.display = 'block';
photoPreview.style.display = 'none';
}
// ── Form Submission ────────────────────────────────────────────────────────
form.addEventListener('submit', async (e) => {
e.preventDefault();
const name = nameInput.value.trim();
const message = messageInput.value.trim();
const contact = currentContact !== 'none' ? contactValue.value.trim() : '';
// Client-side validation
if (!name || name.length < 2) {
showToast('Please enter your name (at least 2 characters).', 'error');
nameInput.focus();
return;
}
if (!message || message.length < 5) {
showToast('Please write a message (at least 5 characters).', 'error');
messageInput.focus();
return;
}
if (currentContact !== 'none' && !contact) {
showToast('Please enter your contact detail or choose Anonymous.', 'error');
contactValue.focus();
return;
}
// Show loading state
setLoading(true);
const payload = {
name,
contactType: currentContact,
contactValue: contact,
category: currentCategory,
message,
photo: photoBase64,
};
try {
const response = await fetch(`${API_BASE}/submit`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
let data = {};
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
data = await response.json();
}
if (!response.ok) {
throw new Error(data.error || `Submission failed (Status ${response.status}).`);
}
if (response.ok && !contentType?.includes('application/json')) {
throw new Error('Server returned an unexpected non-JSON response.');
}
// Success!
setLoading(false);
showSuccess();
} catch (error) {
setLoading(false);
showToast(error.message || 'Something went wrong. Please try again.', 'error');
}
});
function setLoading(loading) {
submitBtn.disabled = loading;
if (loading) {
btnText.textContent = 'Sending...';
btnIcon.innerHTML = '<span class="spinner"></span>';
} else {
btnText.textContent = 'Send to Syntiox Team';
btnIcon.textContent = '🚀';
}
}
// ── Success State ──────────────────────────────────────────────────────────
function showSuccess() {
successOverlay.classList.add('show');
document.body.style.overflow = 'hidden';
}
closeSuccess.addEventListener('click', () => {
successOverlay.classList.remove('show');
document.body.style.overflow = '';
form.reset();
clearPhoto();
charCount.textContent = '0';
// Reset to defaults
currentCategory = 'idea';
currentContact = 'telegram';
selectedCategory.value = 'idea';
document.querySelectorAll('.category-pill').forEach(p => {
p.classList.toggle('active', p.dataset.cat === 'idea');
p.setAttribute('aria-pressed', p.dataset.cat === 'idea' ? 'true' : 'false');
});
document.querySelectorAll('.contact-type-btn').forEach(b => {
b.classList.toggle('active', b.dataset.contact === 'telegram');
b.setAttribute('aria-pressed', b.dataset.contact === 'telegram' ? 'true' : 'false');
});
contactValueGroup.style.display = 'flex';
contactLabel.textContent = CONTACT_CONFIG.telegram.label;
contactValue.placeholder = CONTACT_CONFIG.telegram.placeholder;
contactValue.type = 'text';
messageInput.placeholder = CATEGORY_CONFIG.idea.placeholder;
});
// Close on backdrop click
successOverlay.addEventListener('click', (e) => {
if (e.target === successOverlay) closeSuccess.click();
});
// ── Toast Notifications ────────────────────────────────────────────────────
function showToast(message, type = 'error') {
if (toastTimer) clearTimeout(toastTimer);
toastIcon.textContent = type === 'error' ? '⚠️' : '✅';
toastMessage.textContent = message;
toast.className = `toast ${type} show`;
toastTimer = setTimeout(() => {
toast.classList.remove('show');
}, 4000);
}
// ── Init ───────────────────────────────────────────────────────────────────
messageInput.placeholder = CATEGORY_CONFIG.idea.placeholder;
contactLabel.textContent = CONTACT_CONFIG.telegram.label;
contactValue.placeholder = CONTACT_CONFIG.telegram.placeholder;
})();