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

fix: reactive queries

Each block should have its own :query-result atom
Tienson Qin 2 лет назад
Родитель
Сommit
8ab497cb8a

+ 13 - 15
src/main/frontend/components/block.cljs

@@ -2734,9 +2734,7 @@
         blocks-container-id (:blocks-container-id config)
         config (update config :block merge block)
         ;; Each block might have multiple queries, but we store only the first query's result
-        config (if (nil? (:query-result config))
-                 (assoc config :query-result (atom nil))
-                 config)
+        config (assoc config :query-result (atom nil))
         config (if ref? (block-handler/attach-order-list-state config block) config)
         heading? (:heading properties)
         *control-show? (get state ::control-show?)
@@ -2771,18 +2769,18 @@
                     (state/sub-block-selected? blocks-container-id uuid))]
     [:div.ls-block
      (cond->
-       {:id block-id
-        :data-refs data-refs
-        :data-refs-self data-refs-self
-        :data-collapsed (and collapsed? has-child?)
-        :class (str uuid
-                    (when pre-block? " pre-block")
-                    (when (and card? (not review-cards?)) " shadow-md")
-                    (when selected? " selected noselect")
-                    (when order-list? " is-order-list")
-                    (when (string/blank? content) " is-blank"))
-        :blockid (str uuid)
-        :haschild (str (boolean has-child?))}
+      {:id block-id
+       :data-refs data-refs
+       :data-refs-self data-refs-self
+       :data-collapsed (and collapsed? has-child?)
+       :class (str uuid
+                   (when pre-block? " pre-block")
+                   (when (and card? (not review-cards?)) " shadow-md")
+                   (when selected? " selected noselect")
+                   (when order-list? " is-order-list")
+                   (when (string/blank? content) " is-blank"))
+       :blockid (str uuid)
+       :haschild (str (boolean has-child?))}
 
        level
        (assoc :level level)

+ 5 - 7
src/main/frontend/components/query.cljs

@@ -141,9 +141,7 @@
              (when-not (or built-in? dsl-query?)
                (when collapsed?
                  (editor-handler/collapse-block! current-block-uuid))))
-           state)}
-  (rum/local nil ::query-result)
-  {:init (fn [state] (assoc state :query-error (atom nil)))}
+           (assoc state :query-error (atom nil)))}
   [state config {:keys [title builder query view collapsed? table-view?] :as q} *query-triggered?]
   (let [*query-error (:query-error state)
         built-in? (built-in-custom-query? title)
@@ -208,13 +206,13 @@
                  (if table?
                    [:a.flex.ml-1.fade-link {:title "Switch to list view"
                                             :on-click (fn [] (editor-property/set-block-property! current-block-uuid
-                                                                                                 "query-table"
-                                                                                                 false))}
+                                                                                                  "query-table"
+                                                                                                  false))}
                     (ui/icon "list" {:style {:font-size 20}})]
                    [:a.flex.ml-1.fade-link {:title "Switch to table view"
                                             :on-click (fn [] (editor-property/set-block-property! current-block-uuid
-                                                                                                 "query-table"
-                                                                                                 true))}
+                                                                                                  "query-table"
+                                                                                                  true))}
                     (ui/icon "table" {:style {:font-size 20}})]))
 
                [:a.flex.ml-1.fade-link

+ 21 - 22
src/main/frontend/components/query/result.cljs

@@ -61,26 +61,25 @@
 
 (defn get-query-result
   [state config *query-error *query-triggered? current-block-uuid q options]
-  (or (when-let [*result (:query-result config)] @*result)
-      (let [query-atom (trigger-custom-query! state *query-error *query-triggered?)
-            query-result (and query-atom (rum/react query-atom))
+  (let [query-atom (trigger-custom-query! state *query-error *query-triggered?)
+        query-result (and query-atom (rum/react query-atom))
             ;; exclude the current one, otherwise it'll loop forever
-            remove-blocks (if current-block-uuid [current-block-uuid] nil)
-            transformed-query-result (when query-result
-                                       (let [result (db/custom-query-result-transform query-result remove-blocks q)]
-                                         (if (and query-result (coll? result) (:block/uuid (first result)))
-                                           (cond-> result
-                                            (get q :remove-block-children? true)
-                                            tree/filter-top-level-blocks)
-                                           result)))
-            group-by-page? (get-group-by-page q options)
-            result (if (and group-by-page? (:block/uuid (first transformed-query-result)))
-                     (let [result (db-utils/group-by-page transformed-query-result)]
-                       (if (map? result)
-                         (dissoc result nil)
-                         result))
-                     transformed-query-result)]
-        (when-let [query-result (:query-result config)]
-          (reset! query-result result))
-        (when query-atom
-          (util/safe-with-meta result (meta @query-atom))))))
+        remove-blocks (if current-block-uuid [current-block-uuid] nil)
+        transformed-query-result (when query-result
+                                   (let [result (db/custom-query-result-transform query-result remove-blocks q)]
+                                     (if (and query-result (coll? result) (:block/uuid (first result)))
+                                       (cond-> result
+                                         (get q :remove-block-children? true)
+                                         tree/filter-top-level-blocks)
+                                       result)))
+        group-by-page? (get-group-by-page q options)
+        result (if (and group-by-page? (:block/uuid (first transformed-query-result)))
+                 (let [result (db-utils/group-by-page transformed-query-result)]
+                   (if (map? result)
+                     (dissoc result nil)
+                     result))
+                 transformed-query-result)]
+    (when-let [query-result (:query-result config)]
+      (reset! query-result result))
+    (when query-atom
+      (util/safe-with-meta result (meta @query-atom)))))