Spaces:
Sleeping
Sleeping
Download public/script.js from doksd/icadLAB-formFaceReg: direct link, hf CLI and curl.
- Browser
- Download file 3.48 kB
-
https://huggingface.co/spaces/doksd/icadLAB-formFaceReg/resolve/main/public/script.js
- Command line
-
hf download hf://spaces/doksd/icadLAB-formFaceReg/public/script.js
-
curl -L -o script.js https://huggingface.co/spaces/doksd/icadLAB-formFaceReg/resolve/main/public/script.js
3.48 kB
| // BKP Feedback Form - Real-time UX and backend 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; | |
| if(!rating.value){ | |
| valid = false; | |
| } | |
| if(!comments.value || comments.value.trim().length < 10){ | |
| comments.setCustomValidity('Minimal 10 karakter'); | |
| valid = false; | |
| } else { | |
| comments.setCustomValidity(''); | |
| } | |
| return valid; | |
| } | |
| const FORMS_ACTION = window.BKP_FORMS_ACTION || ''; | |
| const BACKEND_URL = (window.BKP_BACKEND_URL || '').replace(/\/$/, ''); | |
| async function submitData(payload){ | |
| // Prefer backend (same-origin by default, or custom BACKEND_URL if set) | |
| const api = BACKEND_URL ? `${BACKEND_URL}/api/feedback` : `/api/feedback`; | |
| try{ | |
| const res = await fetch(api, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(payload) | |
| }); | |
| if(res.ok) return res; | |
| // fallthrough to other modes | |
| }catch(_){ /* ignore and try fallbacks */ } | |
| if(FORMS_ACTION){ | |
| const body = new URLSearchParams(payload); | |
| return fetch(FORMS_ACTION, { | |
| method: 'POST', | |
| mode: 'no-cors', | |
| headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }, | |
| body | |
| }); | |
| } | |
| 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; | |
| updateRatingLabel(); | |
| updateCounter(); | |
| }catch(err){ | |
| console.error('Submit error', err); | |
| error.hidden = false; | |
| success.hidden = true; | |
| } | |
| }); | |
| rating.addEventListener('input', updateRatingLabel); | |
| comments.addEventListener('input', updateCounter); | |
| updateRatingLabel(); | |
| updateCounter(); | |
| if(year) year.textContent = new Date().getFullYear(); | |
| })(); | |