Repos / pytaku / 3557c2479c
commit 3557c2479ca9abfe91c9b0c0a959591e1a5ffa04
Author: Bùi Thành Nhân <hi@imnhan.com>
Date:   Sun Aug 23 19:59:42 2020 +0700

    toggle following

diff --git a/src/pytaku/main.py b/src/pytaku/main.py
index c155e13..14e9630 100644
--- a/src/pytaku/main.py
+++ b/src/pytaku/main.py
@@ -514,3 +514,18 @@ def api_search(query):
                 "proxy_view", b64_url=_encode_proxy_url(title["thumbnail"])
             )
     return results
+
+
+@app.route("/api/follow", methods=["POST"])
+@process_token(required=True)
+def api_follow():
+    should_follow = request.json["follow"]
+    site = request.json["site"]
+    title_id = request.json["title_id"]
+
+    if should_follow:
+        follow(request.user_id, site, title_id)
+    else:
+        unfollow(request.user_id, site, title_id)
+
+    return jsonify({"follow": should_follow})
diff --git a/src/pytaku/static/js/routes/title.js b/src/pytaku/static/js/routes/title.js
index 186ae17..529bceb 100644
--- a/src/pytaku/static/js/routes/title.js
+++ b/src/pytaku/static/js/routes/title.js
@@ -3,6 +3,7 @@ import { LoadingMessage, Button, fullChapterName, Chapter } from "../utils.js";
 
 function Title(initialVNode) {
   let isLoading = false;
+  let isTogglingFollow = false;
   let title = {};
 
   return {
@@ -37,9 +38,36 @@ function Title(initialVNode) {
               m("div.title--details", [
                 Auth.isLoggedIn()
                   ? m(Button, {
-                      text: "Follow",
                       icon: "bookmark",
-                      color: "green",
+                      disabled: isTogglingFollow ? "disabled" : null,
+                      text: isTogglingFollow
+                        ? "submitting..."
+                        : title.is_following
+                        ? "following"
+                        : "follow",
+                      color: title.is_following ? "red" : "green",
+                      title: title.is_following
+                        ? "Click to unfollow"
+                        : "Click to follow",
+                      onclick: (ev) => {
+                        isTogglingFollow = true;
+                        m.redraw();
+                        Auth.request({
+                          method: "POST",
+                          url: "/api/follow",
+                          body: {
+                            site: title.site,
+                            title_id: title.id,
+                            follow: !title.is_following,
+                          },
+                        })
+                          .then((resp) => {
+                            title.is_following = resp.follow;
+                          })
+                          .finally(() => {
+                            isTogglingFollow = false;
+                          });
+                      },
                     })
                   : null,
                 " ",
diff --git a/src/pytaku/static/lookandfeel.css b/src/pytaku/static/lookandfeel.css
index b874b45..3e3fd64 100644
--- a/src/pytaku/static/lookandfeel.css
+++ b/src/pytaku/static/lookandfeel.css
@@ -114,6 +114,7 @@ button[disabled]:active {
   filter: none;
   cursor: default;
   opacity: 0.5;
+  color: #222;
 }
 a.touch-friendly {
   background-color: white;