File size: 3,838 Bytes
e17de98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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();
})();