Branchwell

Paywell / website · paywell.company / assets / rate.js

Raw 3.3 KB on 080f5e6639651f7c8ea5bac67bba0a504a0eba6e
1(function(){
2var $=function(id){return document.getElementById(id)};
3var token=null;
4function show(id){['r-start','r-form-wrap','r-done'].forEach(function(s){$(s).hidden=(s!==id)});window.scrollTo(0,0);}
5function err(msg){var e=$('r-err');e.textContent=msg;e.hidden=!msg;$('r-loading').hidden=true;}
6function post(path,body){return fetch('/api/rate/'+path,{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify(body)}).then(function(r){return r.json().then(function(j){j._ok=r.ok;return j})});}
7function tokenFromUrl(){
8 var m=location.pathname.match(/^\/r\/([A-Za-z0-9_.-]+)/); if(m) return m[1];
9 var q=new URLSearchParams(location.search); if(q.get('t')) return q.get('t');
10 return null;
11}
12function load(t){
13 err('');$('r-loading').hidden=false;
14 post('check',{token:t}).then(function(j){
15 $('r-loading').hidden=true;
16 if(!j._ok){err(j.error||'Something went wrong. Please try again.');show('r-start');return;}
17 token=t;var v=j.visit;
18 $('r-test').hidden=!j.test;
19 $('r-where').textContent=[v.brand,v.service,v.date?new Date(v.date+'T12:00:00').toLocaleDateString(undefined,{month:'long',day:'numeric'}):''].filter(Boolean).join(' · ');
20 $('r-q').textContent='How was your visit with '+v.person+'?';
21 show('r-form-wrap');
22 }).catch(function(){err('We couldn’t reach Paywell. Check your connection and try again.');});
23}
24$('r-code').addEventListener('submit',function(e){
25 e.preventDefault();var c=$('r-code-in').value.trim();
26 if(!c){err('Please enter the code from your receipt.');return;}
27 var m=c.match(/\/r\/([A-Za-z0-9_.-]+)/); if(m){load(m[1]);return;}
28 if(c.indexOf('.')>0&&c.length>40){load(c);return;}
29 err('');post('code',{code:c}).then(function(j){if(j._ok&&j.token)load(j.token);else err(j.error||'We couldn’t find that code.');}).catch(function(){err('We couldn’t reach Paywell. Try again in a minute.');});
30});
31$('r-sample').addEventListener('click',function(){
32 fetch('/api/rate/sample').then(function(r){return r.json()}).then(function(j){if(j.token)load(j.token);else err('The sample isn’t available right now.');}).catch(function(){err('We couldn’t reach Paywell. Try again in a minute.');});
33});
34$('r-form').addEventListener('submit',function(e){
35 e.preventDefault();var f=e.target;var s=f.querySelector('input[name=stars]:checked');
36 if(!s){$('r-ferr').textContent='Please tap a rating first.';return;}
37 var b=f.querySelector('.r-send');b.disabled=true;$('r-ferr').textContent='';
38 post('submit',{token:token,stars:+s.value,note:f.note.value,website:f.website.value}).then(function(j){
39 b.disabled=false;
40 if(!j._ok){$('r-ferr').textContent=j.error||'That didn’t go through. Please try again.';return;}
41 var p=j.person||'them';
42 var msg={bonus:'Your rating raises '+p+'’s bonus today. The company pays it, not you.',none:'Thanks for letting us know. An okay rating doesn’t change anyone’s pay.',review:'Thanks for telling us. A low rating is checked before it counts, and '+p+' gets a chance to respond. It never touches their wage or earned pay.'}[j.effect];
43 $('r-done-h').textContent=j.effect==='review'?'Thanks for your honesty.':'Thank you!';
44 $('r-done-p').textContent=msg;$('r-done-test').hidden=!j.test;show('r-done');
45 }).catch(function(){b.disabled=false;$('r-ferr').textContent='We couldn’t reach Paywell. Please try again.';});
46});
47var t=tokenFromUrl(); if(t) load(t);
48})();