Commit da7a759a authored by Jogi Hofmüller's avatar Jogi Hofmüller

Fix javascript to add new geruecht

parent 5c3fb3cd
......@@ -7,9 +7,7 @@ var addurl = 'https://'+ hostname + ':8888/add';
function nextgeruecht() {
fetch(geturl, {
method: 'GET',
credentials: 'include',
mode: 'cors'
method: 'GET'
})
.then(function(response) {
if (response.ok) {
......@@ -27,12 +25,21 @@ function nextgeruecht() {
}
function addgeruecht() {
var payload = document.getElementById('newgeruecht').value;
var object = {}
object.type = 'geruecht';
object.payload = document.getElementById('newgeruecht').value;
if (object.payload.length < 20) {
input = document.getElementById('newgeruecht');
input.className = 'form-control is-invalid';
input.focus();
document.getElementById('newgeruechtHelp').className = 'form-text text-danger';
return false;
}
fetch(addurl, {
credentials: 'include',
method: 'PUT',
mode: 'cors',
body: JSON.stringify(payload),
body: JSON.stringify(object),
headers: {
'Content-Type': 'application/json; charset=utf8'
}
......@@ -44,13 +51,24 @@ function addgeruecht() {
throw new Error('Network error (response not ok)');
})
.then(function(data) {
document.getElementById('geruecht').innerHTML = data.message;
window.alert('Danke! Das Gerücht wird überprüft und bei Gefallen in die Sammlung aufgenommen.');
input = document.getElementById('newgeruecht');
input.value = "";
input.className = 'form-control';
input.focus();
document.getElementById('newgeruechtHelp').className = 'form-text';
})
.catch(function(error) {
document.getElementById('geruecht').innerHTML = 'API Fehler: ' + error;
console.log('API error: ' + error);
document.getElementById('geruecht').innerHTML = 'addgeruecht Fehler: ' + error;
console.log('addgeruecht error: ' + error);
})
}
window.onload = function() {
nextgeruecht();
var button = document.getElementById('addgeruecht');
button.addEventListener('click', function(event) {
addgeruecht();
event.preventDefault();
})
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment