This commit is contained in:
codl 2017-08-29 18:38:54 +02:00
parent 9a86b45268
commit 6495135124
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
2 changed files with 41 additions and 25 deletions

17
.eslintrc.yml Normal file
View File

@ -0,0 +1,17 @@
env:
browser: true
es6: true
extends: 'eslint:recommended'
rules:
indent:
- error
- 4
linebreak-style:
- error
- unix
quotes:
- error
- single
semi:
- error
- always

View File

@ -7,14 +7,14 @@
let settings_section = document.querySelector('#settings-section'); let settings_section = document.querySelector('#settings-section');
let form = document.forms.settings; let form = document.forms.settings;
let backoff_level = 0 let backoff_level = 0;
function hide_status(){ function hide_status(){
status_display.classList.remove('error', 'success', 'saving'); status_display.classList.remove('error', 'success', 'saving');
status_display.classList.add('hidden'); status_display.classList.add('hidden');
status_display.innerHTML=''; status_display.innerHTML='';
} }
function show_error(e){ function show_error(){
hide_status(); hide_status();
status_display.textContent='Could not save. Retrying...'; status_display.textContent='Could not save. Retrying...';
status_display.classList.add('error'); status_display.classList.add('error');
@ -26,6 +26,9 @@
status_display.classList.add('success'); status_display.classList.add('success');
status_display.classList.remove('hidden'); status_display.classList.remove('hidden');
} }
function show_still_saving(){
status_display.textContent='Still saving...';
}
function show_saving(){ function show_saving(){
hide_status(); hide_status();
status_display.textContent='Saving...'; status_display.textContent='Saving...';
@ -33,30 +36,26 @@
status_display.classList.remove('hidden'); status_display.classList.remove('hidden');
status_timeout = setTimeout(show_still_saving, 5000); status_timeout = setTimeout(show_still_saving, 5000);
} }
function show_still_saving(){
status_display.textContent='Still saving...';
}
function save(){ function save(){
hide_status(); hide_status();
clearTimeout(status_timeout); clearTimeout(status_timeout);
status_timeout = setTimeout(show_saving, 70); status_timeout = setTimeout(show_saving, 70);
promise = send_settings(get_all_inputs()) let promise = send_settings(get_all_inputs())
.then(data => { .then(() => {
show_success(); show_success();
clearTimeout(status_timeout); clearTimeout(status_timeout);
status_timeout = setTimeout(hide_status, 3000); status_timeout = setTimeout(hide_status, 3000);
backoff_level = 0; backoff_level = 0;
}); });
promise.catch(e => { promise.catch(() => {
console.error('Fetch rejected:', e); show_error();
show_error(); clearTimeout(status_timeout);
clearTimeout(status_timeout); status_timeout = setTimeout(save, Math.pow(2, backoff_level)*1000);
status_timeout = setTimeout(save, Math.pow(2, backoff_level)*1000); backoff_level += 1;
backoff_level += 1; backoff_level = Math.min(backoff_level, 5);
backoff_level = Math.min(backoff_level, 5); });
});
promise.then(fetch_viewer).then(update_viewer); promise.then(fetch_viewer).then(update_viewer);
// remove server-rendered banner // remove server-rendered banner
@ -67,13 +66,13 @@
} }
function get_all_inputs(){ function get_all_inputs(){
let o = Object() let o = Object();
for(input of form.elements){ for(let input of form.elements){
if(input.type != 'radio' || input.checked){ if(input.type != 'radio' || input.checked){
o[input.name] = input.value; o[input.name] = input.value;
} }
} }
return o return o;
} }
function send_settings(body){ function send_settings(body){
@ -85,15 +84,15 @@
}, },
body: JSON.stringify(body) body: JSON.stringify(body)
}) })
.then(resp => { if(!resp.ok){ return Promise.reject(resp) }; return resp; }) .then(resp => { if(!resp.ok){ return Promise.reject(resp); } return resp; })
.then(resp => resp.json()) .then(resp => resp.json())
.then(data => { .then(data => {
if(data.status == 'error'){ return Promise.reject(data) } if(data.status == 'error'){ return Promise.reject(data); }
return data return data;
}); });
} }
for(input of form.elements){ for(let input of form.elements){
input.addEventListener('change', save); input.addEventListener('change', save);
} }
@ -116,14 +115,14 @@
return fetch('/api/viewer', { return fetch('/api/viewer', {
credentials: 'same-origin', credentials: 'same-origin',
}) })
.then(resp => { if(!resp.ok){ return Promise.reject(resp) }; return resp; }) .then(resp => { if(!resp.ok){ return Promise.reject(resp); } return resp; })
.then(resp => resp.json()); .then(resp => resp.json());
} }
let last_viewer = {} let last_viewer = {};
function update_viewer(viewer){ function update_viewer(viewer){
if(JSON.stringify(last_viewer) == JSON.stringify(viewer)){ if(JSON.stringify(last_viewer) == JSON.stringify(viewer)){
return return;
} }
document.querySelector('#post-count').textContent = viewer.post_count; document.querySelector('#post-count').textContent = viewer.post_count;