Просмотр исходного кода

enhance: make ui reactive when favorites updated

rcmerci 1 год назад
Родитель
Сommit
0b52f8ad58

+ 2 - 1
src/main/frontend/components/container.cljs

@@ -132,7 +132,8 @@
 
 (rum/defc favorites < rum/reactive
   [t]
-  (let [favorite-entities (page-handler/get-favorites)]
+  (let [_favorites-updated? (state/sub :favorites/updated?)
+        favorite-entities (page-handler/get-favorites)]
     (nav-content-item
      [:a.flex.items-center.text-sm.font-medium.rounded-md.wrap-th
       (ui/icon "star" {:size 16})

+ 24 - 19
src/main/frontend/handler/page.cljs

@@ -49,21 +49,25 @@
 
 (defn <unfavorite-page!
   [page-name]
-  (let [repo (state/get-current-repo)]
-    (if (config/db-based-graph? repo)
-      (let [db (conn/get-db)]
-        (when-let [page-block-uuid (:block/uuid (d/entity db [:block/name (common-util/page-name-sanity-lc page-name)]))]
-          (page-common-handler/<unfavorite-page!-v2 page-block-uuid)))
-      (page-common-handler/unfavorite-page! page-name))))
+  (p/do!
+   (let [repo (state/get-current-repo)]
+     (if (config/db-based-graph? repo)
+       (let [db (conn/get-db)]
+         (when-let [page-block-uuid (:block/uuid (d/entity db [:block/name (common-util/page-name-sanity-lc page-name)]))]
+           (page-common-handler/<unfavorite-page!-v2 page-block-uuid)))
+       (page-common-handler/unfavorite-page! page-name)))
+   (state/update-favorites-updated!)))
 
 (defn <favorite-page!
   [page-name]
-  (let [repo (state/get-current-repo)]
-    (if (config/db-based-graph? repo)
-      (let [db (conn/get-db)]
-        (when-let [page-block-uuid (:block/uuid (d/entity db [:block/name (common-util/page-name-sanity-lc page-name)]))]
-          (page-common-handler/<favorite-page!-v2 page-block-uuid)))
-      (page-common-handler/favorite-page! page-name))))
+  (p/do!
+   (let [repo (state/get-current-repo)]
+     (if (config/db-based-graph? repo)
+       (let [db (conn/get-db)]
+         (when-let [page-block-uuid (:block/uuid (d/entity db [:block/name (common-util/page-name-sanity-lc page-name)]))]
+           (page-common-handler/<favorite-page!-v2 page-block-uuid)))
+       (page-common-handler/favorite-page! page-name)))
+   (state/update-favorites-updated!)))
 
 (defn favorited?
   [page-name]
@@ -145,13 +149,14 @@
                   favorites)
             current-blocks (ldb/sort-by-left (ldb/get-page-blocks @conn page-common-handler/favorites-page-name {})
                                              favorites-page-entity)]
-        (prn :favorite-page-block-db-id-coll favorite-page-block-db-id-coll)
-        (ui-outliner-tx/transact!
-         {}
-         (doseq [[page-block-db-id block] (zipmap favorite-page-block-db-id-coll current-blocks)]
-           (when (not= page-block-db-id (:db/id (:block/link block)))
-             (outliner-core/save-block! repo conn (state/get-date-formatter)
-                                        (assoc block :block/link page-block-db-id)))))))))
+        (p/do!
+          (ui-outliner-tx/transact!
+              {}
+              (doseq [[page-block-db-id block] (zipmap favorite-page-block-db-id-coll current-blocks)]
+                (when (not= page-block-db-id (:db/id (:block/link block)))
+                  (outliner-core/save-block! repo conn (state/get-date-formatter)
+                                             (assoc block :block/link page-block-db-id)))))
+          (state/update-favorites-updated!))))))
 
 (defn has-more-journals?
   []

+ 7 - 1
src/main/frontend/state.cljs

@@ -307,7 +307,9 @@
       :history/tx->editor-cursor             (atom {})
       :system/info                           {}
       ;; Whether block is selected
-      :ui/select-query-cache                 (atom {})})))
+      :ui/select-query-cache                 (atom {})
+
+      :favorites/updated?                    (atom 0)})))
 
 ;; Block ast state
 ;; ===============
@@ -2369,3 +2371,7 @@ Similar to re-frame subscriptions"
     (when (and max-tx-id (nil? (:after (get @(:history/tx->editor-cursor @state) max-tx-id))))
       (update-state! :history/tx->editor-cursor
                      (fn [m] (assoc-in m [max-tx-id :after] editor-cursor))))))
+
+(defn update-favorites-updated!
+  []
+  (update-state! :favorites/updated? inc))