implemented saving of new events. next: updating

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2024-12-28 20:40:10 +01:00
parent fc17bb9168
commit 2e1f1c9697
7 changed files with 112 additions and 28 deletions
@@ -29,6 +29,7 @@ public class ApiHandler extends PathHandler {
private static final Logger LOG = getLogger(ApiHandler.class.getSimpleName());
private static final String ATTACHMENTS = "attachments";
private static final String LINKS = "links";
private static final String TAGS = "tags";
private final Database db;
public ApiHandler(Database db) {
@@ -102,6 +103,7 @@ public class ApiHandler extends PathHandler {
if (o instanceof JSONObject j) toLink(j).optional().ifPresent(event::addLinks);
});
}
if (json.has(TAGS)) json.getJSONArray(TAGS).forEach(o -> event.tags(o.toString()));
var res = db.add(event).map(ApiHandler::toJson);
return sendContent(ex, res);
}
@@ -35,7 +35,6 @@
</div>
<script>
document.addEventListener("DOMContentLoaded", function(event){
console.log("page loaded…");
loadCurrentEvents();
});
</script>
@@ -135,4 +135,20 @@ table#eventlist{
position: fixed;
top: 15px;
left: 50%;
}
.error {
position: absolute;
left: 0;
display: inline;
width: 100%;
text-align: center;
}
.error span {
color: red;
background: wheat;
padding: 5px;
border-radius: 5px;
}
.highlight{
background: wheat;
}
@@ -139,6 +139,25 @@ async function slug(start,location){
return btoa(String.fromCharCode(...new Uint8Array(hash)));
}
function showError(message){
var span = document.createElement('div');
span.setAttribute('class','error');
span.innerHTML = "<span>Error: "+message+"<span>";
element('taglist').appendChild(span);
}
async function handleSave(response){
if (response.ok){
var json = await response.json();
var url = window.location.href.replace('/static/edit','')
if (json.id) url+='?highlight='+json.id;
window.location.href = url; // TODO: add highlight on new event
} else {
var json = await response.json();
if (json.error) showError(json.error);
}
}
async function saveEvent(){
element('save').toggleAttribute("disabled");
@@ -163,5 +182,5 @@ async function saveEvent(){
headers: {
'Content-Type' : 'appication/json'
}
});
}).then(handleSave);
}
@@ -1,6 +1,7 @@
var start = null;
var end = null;
var tags = new Set();
var highlight = null;
function addCell(row,content,id){
var a = document.createElement('a');
@@ -14,6 +15,7 @@ function addCell(row,content,id){
function addRow(json){
var table = document.getElementById('eventlist');
var row = table.insertRow(1);
row.id = json.id;
if (json.tags){
var tagList = json.tags.join(' ');
row.setAttribute('class',tagList);
@@ -76,10 +78,17 @@ async function handleEvents(response){
var json = await response.json();
json.forEach(addRow);
updateTagVisibility();
if (highlight){
var row = element(highlight);
row.classList.add('highlight');
row.scrollIntoView({behavior: 'smooth',block: 'center'});
}
}
}
function loadCurrentEvents(){
let params = new URLSearchParams(location.search);
highlight = params.get('highlight');
if (start == null){
var now = new Date();
var year = now.getFullYear();