Display feedback when filtering or requesting new posts
This commit is contained in:
parent
2e48899420
commit
d430d62c26
1 changed files with 36 additions and 26 deletions
|
|
@ -234,32 +234,36 @@
|
||||||
on-response on-response
|
on-response on-response
|
||||||
on-error on-error}}]
|
on-error on-error}}]
|
||||||
((fn fetch-favorites' [url]
|
((fn fetch-favorites' [url]
|
||||||
(println :calling url)
|
(let [req-id (js/Date.now)]
|
||||||
(swap! state assoc :api/state :loading)
|
(println :calling url)
|
||||||
(-> (mastodon-request! {:url url :bearer-token bearer-token})
|
(swap! state assoc-in [:section/posts :loading] req-id)
|
||||||
(.then (fn [response]
|
(-> (mastodon-request! {:url url :bearer-token bearer-token})
|
||||||
(on-response response)
|
(.then (fn [response]
|
||||||
(if (continue? response)
|
(on-response response)
|
||||||
(js/setTimeout #(fetch-favorites' (link-header "next" (:raw response))) 500)
|
(if (continue? response)
|
||||||
(swap! state dissoc :api/state :loading))))
|
(js/setTimeout #(fetch-favorites' (link-header "next" (:raw response))) 500)
|
||||||
(.catch (fn [response]
|
(when (= req-id (-> @state :section/posts :loading))
|
||||||
(swap! state dissoc :api/state)
|
(swap! state update :section/posts dissoc :loading))))
|
||||||
(on-error response)))))
|
(.catch (fn [response]
|
||||||
(favorites-url {:instance-url instance-url :max-id max-id})))
|
(when (= req-id (-> @state :section/posts :loading))
|
||||||
|
(swap! state update :section/posts dissoc :loading))
|
||||||
|
(on-error response))))))
|
||||||
|
(favorites-url {:instance-url instance-url :max-id max-id}))))
|
||||||
|
|
||||||
;;; views
|
;;; views
|
||||||
|
|
||||||
(defn debounce [ms f]
|
(defn debounce [ms f]
|
||||||
(let [timeout (volatile! nil)]
|
(let [prev (volatile! (js/Date.now))
|
||||||
|
trail (volatile! (js/setTimeout (fn dummy []) ms))]
|
||||||
(fn debounced-fn [& args]
|
(fn debounced-fn [& args]
|
||||||
(when @timeout
|
(let [now (js/Date.now)]
|
||||||
(js/clearTimeout @timeout))
|
(if (> (- now @prev) ms)
|
||||||
(js/console.log "debounced-fn called" (js/Date.now))
|
(do (vreset! prev now)
|
||||||
(vreset! timeout
|
(apply f args))
|
||||||
(js/setTimeout (fn scheduled-fn []
|
(do (js/clearTimeout @trail)
|
||||||
(js/console.log "scheduled-fn called" (js/Date.now))
|
(vreset! trail (js/setTimeout (fn []
|
||||||
(vreset! timeout nil)
|
(vreset! prev (js/Date.now))
|
||||||
(apply f args)) ms)))))
|
(apply f args))))))))))
|
||||||
|
|
||||||
(defn search []
|
(defn search []
|
||||||
[:input {:placeholder "Start typing to search…"
|
[:input {:placeholder "Start typing to search…"
|
||||||
|
|
@ -341,15 +345,20 @@
|
||||||
(matches? (j/get-in post [:account :acct])) ; search for url + username of poster
|
(matches? (j/get-in post [:account :acct])) ; search for url + username of poster
|
||||||
(some #(matches? (j/get % :username)) (j/get post :mentions))))) ; search only for username of mentions
|
(some #(matches? (j/get % :username)) (j/get post :mentions))))) ; search only for username of mentions
|
||||||
(take per-page)
|
(take per-page)
|
||||||
(map #(js->clj % :keywordize-keys true)))]
|
(map #(js->clj % :keywordize-keys true)))
|
||||||
|
refresh-id (js/Date.now)]
|
||||||
|
(swap! state assoc-in [:section/posts :loading] refresh-id)
|
||||||
(-> (js/Promise.all #js [(db/count! ::db/posts)
|
(-> (js/Promise.all #js [(db/count! ::db/posts)
|
||||||
(-> (db/open-cursor! ::db/posts db/all "prev")
|
(-> (db/open-cursor! ::db/posts db/all "prev")
|
||||||
(db/transduce-cursor xform))])
|
(db/transduce-cursor xform))])
|
||||||
(.then (fn [[total displayed-posts]]
|
(.then (fn [[total displayed-posts]]
|
||||||
(swap! state update :section/posts #(-> (assoc % :total total)
|
(swap! state update :section/posts #(let [v (-> (assoc % :total total)
|
||||||
(assoc :displayed-posts displayed-posts))))))))
|
(assoc :displayed-posts displayed-posts))]
|
||||||
|
(if (= refresh-id (:loading %))
|
||||||
|
(dissoc v :loading)
|
||||||
|
v))))))))
|
||||||
|
|
||||||
(def debounced-refresh! (debounce 100 (partial refresh-displayed-posts!)))
|
(def debounced-refresh! (debounce 20 (partial refresh-displayed-posts!)))
|
||||||
|
|
||||||
(defn- fetch-posts! [opts]
|
(defn- fetch-posts! [opts]
|
||||||
(let [defaults {:max-id nil
|
(let [defaults {:max-id nil
|
||||||
|
|
@ -360,7 +369,7 @@
|
||||||
(fetch-favorites! (merge defaults opts))))
|
(fetch-favorites! (merge defaults opts))))
|
||||||
|
|
||||||
(defn posts-section [{:keys [posts]}]
|
(defn posts-section [{:keys [posts]}]
|
||||||
(let [{:keys [per-page query total displayed-posts]} posts]
|
(let [{:keys [per-page query total displayed-posts loading]} posts]
|
||||||
[:section.favorites
|
[:section.favorites
|
||||||
[:h2 "Favorites"]
|
[:h2 "Favorites"]
|
||||||
[:header.controls
|
[:header.controls
|
||||||
|
|
@ -370,6 +379,7 @@
|
||||||
(str ", displaying " (count displayed-posts) (when query " matches"))))]
|
(str ", displaying " (count displayed-posts) (when query " matches"))))]
|
||||||
[:div.search-form
|
[:div.search-form
|
||||||
[search]
|
[search]
|
||||||
|
(when loading " …")
|
||||||
#_(cond (= api-state :loading) " …"
|
#_(cond (= api-state :loading) " …"
|
||||||
(= api-state :error) " API Error!")]]
|
(= api-state :error) " API Error!")]]
|
||||||
[:ul.results (map-indexed (fn [idx favorite]
|
[:ul.results (map-indexed (fn [idx favorite]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue