From 9bd2cceb7d56fab878b1f9f22a5b19897ff64a46 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Sat, 16 Apr 2022 23:45:17 +0200 Subject: [PATCH] pre-filling subscription form for logged-in users --- .../de/srsoftware/widerhall/data/ListMember.java | 2 +- .../java/de/srsoftware/widerhall/web/Web.java | 16 ++++++++++------ static/templates/js.st | 7 ++++--- static/templates/listlist.st | 2 +- static/templates/subscribe.st | 4 ++-- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/srsoftware/widerhall/data/ListMember.java b/src/main/java/de/srsoftware/widerhall/data/ListMember.java index 478ca4e..7e1105a 100644 --- a/src/main/java/de/srsoftware/widerhall/data/ListMember.java +++ b/src/main/java/de/srsoftware/widerhall/data/ListMember.java @@ -77,7 +77,7 @@ public class ListMember { public static String stateText(int state) { var words = new ArrayList(); - if ((state & STATE_OWNER) > 0) words.add("owener"); + if ((state & STATE_OWNER) > 0) words.add("owner"); if ((state & STATE_SUBSCRIBER) > 0) words.add("subscriber"); return String.join(", ",words); } diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index 382d50c..e3d9311 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -313,7 +313,7 @@ public class Web extends HttpServlet { var email = req.getParameter(EMAIL); var pass = req.getParameter(PASSWORD); var list = req.getParameter(LIST); - var data = new HashMap(); + var data = new HashMap(); data.put(NAME,name); data.put(EMAIL,email); data.put(LIST,list); @@ -329,12 +329,12 @@ public class Web extends HttpServlet { if (pass != null && pass.isBlank()) pass = null; try { - User.create(email,name,pass); + data.put(USER,User.create(email,name,pass).safeMap()); } catch (SQLException sqle) { var cause = getCausingException(sqle); int code = cause.getErrorCode(); if (code == PRIMARY_KEY_CONSTRAINT) try {// user already exists - User.load(email,pass); + data.put(USER,User.load(email,pass).safeMap()); // success → subscribe } catch (InvalidKeyException | SQLException e) { // invalid credentials @@ -346,9 +346,13 @@ public class Web extends HttpServlet { ListMember.create(list,email,ListMember.STATE_SUBSCRIBER); data.put(NOTES,t("Successfully subscribed '{}' to '{}'.",email,list)); return loadTemplate(INDEX,data,resp); - } catch (SQLException e) { - LOG.debug("List subscription failed: ",e); - data.put(ERROR,t("Subscription failed: {}",e.getMessage())); + } catch (SQLException sqle) { + LOG.debug("List subscription failed: ",sqle); + var cause = getCausingException(sqle); + int code = cause.getErrorCode(); + if (code == PRIMARY_KEY_CONSTRAINT) {// user already exists + data.put(ERROR,t("You already are member of this list!",sqle.getMessage())); + } else data.put(ERROR,t("Subscription failed: {}",sqle.getMessage())); return loadTemplate(SUBSCRIBE,data,resp); } } diff --git a/static/templates/js.st b/static/templates/js.st index 1e8ccc7..9de6fdc 100644 --- a/static/templates/js.st +++ b/static/templates/js.st @@ -81,14 +81,15 @@ function showListList(data){ for (let i in data.lists){ let list = data.lists[i]; let row = $(''); - let subBtn = $('