Wiki: Provide feedback after editing.

Change-Id: I6530be803f5f4218008c07d00ad3c6f739a1eb7a
diff --git a/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiResource.java b/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiResource.java
index 30cae98..0f7aa3f 100644
--- a/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiResource.java
+++ b/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiResource.java
@@ -1,5 +1,6 @@
 package eu.mulk.mulkcms2.benki.wiki;
 
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
 import static javax.ws.rs.core.MediaType.TEXT_HTML;
 
 import eu.mulk.mulkcms2.benki.users.User;
@@ -16,6 +17,8 @@
 import java.time.temporal.TemporalAccessor;
 import java.util.Optional;
 import javax.inject.Inject;
+import javax.json.JsonObject;
+import javax.json.spi.JsonProvider;
 import javax.transaction.Transactional;
 import javax.ws.rs.BadRequestException;
 import javax.ws.rs.FormParam;
@@ -39,6 +42,8 @@
   private static DateTimeFormatter humanDateFormatter =
       DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT);
 
+  private static JsonProvider jsonProvider = JsonProvider.provider();
+
   @ResourcePath("benki/wiki/wikiPage.html")
   @Inject
   Template wikiPage;
@@ -71,14 +76,15 @@
   @Path("/{pageName}")
   @Authenticated
   @Transactional
-  public void updatePage(
+  @Produces(APPLICATION_JSON)
+  public JsonObject updatePage(
       @PathParam("pageName") String pageName,
       @FormParam("wiki-title") String title,
       @FormParam("wiki-content") String content) {
 
     if (title == null && content == null) {
       // No changes, nothing to do.
-      return;
+      return jsonProvider.createObjectBuilder().add("status", "ok").build();
     }
 
     if (title != null) {
@@ -112,6 +118,12 @@
             User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult());
 
     pageRevision.persistAndFlush();
+
+    return jsonProvider
+        .createObjectBuilder()
+        .add("status", "ok")
+        .add("content", pageRevision.enrichedContent())
+        .build();
   }
 
   @GET
diff --git a/src/main/resources/templates/benki/wiki/wikiPage.html b/src/main/resources/templates/benki/wiki/wikiPage.html
index 901b300..dbfc9c7 100644
--- a/src/main/resources/templates/benki/wiki/wikiPage.html
+++ b/src/main/resources/templates/benki/wiki/wikiPage.html
@@ -8,9 +8,11 @@
 <link rel="stylesheet" type="text/css" href="/web_modules/ContentTools/build/content-tools.min.css" />
 <script type="module">
   import {ContentTools} from "/web_modules/ContentTools.js";
+
   window.addEventListener('DOMContentLoaded', function() {
     let editor = ContentTools.EditorApp.get();
     editor.init('*[data-editable]', 'data-name');
+
     editor.addEventListener('saved', async function (ev) {
       let regions = ev.detail().regions;
       if (Object.getOwnPropertyNames(regions).length === 0) {
@@ -30,6 +32,15 @@
         body: requestParams
       });
 
+      let status = await response.json();
+      if (status.status === "ok") {
+        if (status.hasOwnProperty("content")) {
+          document.getElementById("wiki-content").innerHTML = status.content;
+        }
+      } else {
+        alert("Failed to save page: " + JSON.stringify(status));
+      }
+
       this.busy(false);
     });
   });
@@ -45,7 +56,7 @@
   </header>
 
   <main>
-    <div data-editable data-name="wiki-content">
+    <div data-editable data-name="wiki-content" id="wiki-content">
       {#with page}{enrichedContent.raw}{/}
     </div>
   </main>