icadLAB-formFaceReg / script.js
doksd's picture
Upload 8 files
e17de98 verified
Raw History Blame Contribute Delete
3.84 kB
// BKP Feedback Form - Real-time UX and basic client-side submission
(function(){
const rating = document.getElementById('rating');
const ratingValue = document.getElementById('ratingValue');
const comments = document.getElementById('comments');
const counter = document.getElementById('commentCounter');
const form = document.getElementById('feedbackForm');
const success = document.getElementById('successMessage');
const error = document.getElementById('errorMessage');
const year = document.getElementById('year');
const ratingLabels = {
1: 'Sangat Tidak Puas',
2: 'Tidak Puas',
3: 'Netral',
4: 'Puas',
5: 'Sangat Puas'
};
function updateRatingLabel(){
const v = String(rating.value);
ratingValue.textContent = `${v} • ${ratingLabels[v] || ''}`;
}
function updateCounter(){
const len = comments.value.length;
counter.textContent = `${len} karakter`;
}
function validateForm(){
let valid = true;
error.hidden = true;
// Rating required
if(!rating.value){
valid = false;
}
// Comments required + minlength 10
if(!comments.value || comments.value.trim().length < 10){
comments.setCustomValidity('Minimal 10 karakter');
valid = false;
} else {
comments.setCustomValidity('');
}
return valid;
}
// Optional: Google Forms webhook (configure by setting window.BKP_FORMS_ACTION)
// You can set this dynamically in Spaces Secrets and expose via an injected script if needed.
const FORMS_ACTION = window.BKP_FORMS_ACTION || '';
const BACKEND_URL = (window.BKP_BACKEND_URL || '').replace(/\/$/, '');
async function submitData(payload){
if(BACKEND_URL){
// Send to backend API
const res = await fetch(`${BACKEND_URL}/api/feedback`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if(!res.ok) throw new Error('Backend error');
return res;
} else if(FORMS_ACTION){
// Send as application/x-www-form-urlencoded for Google Forms compatibility
const body = new URLSearchParams(payload);
return fetch(FORMS_ACTION, {
method: 'POST',
mode: 'no-cors', // Google Forms usually requires no-cors
headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
body
});
} else {
// Fallback: store locally for demo
const key = `bkp-feedback:${Date.now()}`;
localStorage.setItem(key, JSON.stringify(payload));
return Promise.resolve({ ok:true, local:true });
}
}
function collectFeatures(){
return Array.from(document.querySelectorAll('input[name="features"]:checked')).map(c=>c.value);
}
form.addEventListener('submit', async (e)=>{
e.preventDefault();
if(!validateForm()){
error.hidden = false;
success.hidden = true;
return;
}
const payload = {
rating: rating.value,
features: collectFeatures().join(', '),
comments: comments.value.trim(),
ts: new Date().toISOString(),
ua: navigator.userAgent
};
try{
await submitData(payload);
success.hidden = false;
error.hidden = true;
form.reset();
rating.value = 3; // reset default
updateRatingLabel();
updateCounter();
}catch(err){
console.error('Submit error', err);
error.hidden = false;
success.hidden = true;
}
});
// Live updates
rating.addEventListener('input', updateRatingLabel);
comments.addEventListener('input', updateCounter);
// Init
updateRatingLabel();
updateCounter();
if(year) year.textContent = new Date().getFullYear();
})();