Browse Source

Handle redirects in link creation

Getty Ritter 1 year ago
parent
commit
1ae0cb94ef
3 changed files with 8 additions and 3 deletions
  1. 4 1
      lc/web.py
  2. 3 1
      static/lc.js
  3. 1 1
      templates/add_link.mustache

+ 4 - 1
lc/web.py

@@ -66,7 +66,10 @@ class Endpoint:
 
     SHOULD_REDIRECT = set(("application/x-www-form-urlencoded", "multipart/form-data",))
 
-    def api_ok(self, redirect: str, data: dict = {"status": "ok"}) -> ApiOK:
+    def api_ok(self, redirect: str, data: Optional[dict] = None) -> ApiOK:
+        if data is None:
+            data = {"status": "ok"}
+        data["redirect"] = redirect
         content_type = flask.request.content_type or ""
         content_type = content_type.split(";")[0]
         if content_type in Endpoint.SHOULD_REDIRECT:

+ 3 - 1
static/lc.js

@@ -46,7 +46,9 @@ $(document).ready(() => {
                 method: 'POST',
                 headers: {'Content-Type': 'application/json'},
                 body: JSON.stringify(body),
-            }).then(_ => window.location.href = url);
+            }).then(response => response.json())
+                .then(body => window.location.href = body['redirect'] || url)
+                .catch(err => window.location.href = url);
         });
     }
 });

+ 1 - 1
templates/add_link.mustache

@@ -1,5 +1,5 @@
 <div class="loginform">
-  <form name="login" method="POST" action="{{post_url}}">
+  <form name="edit_link" method="POST" action="{{post_url}}">
     <div class="url">
       <label for="url">URL</label>
       <input name="url" type="text" value="{{url}}" />