From 714ff39929dc4148a4ae1896ff8f091813a6d754 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sat, 20 Jan 2018 10:08:52 -0500 Subject: [PATCH] Implement viewing child comments --- src/looped_in/macros.clj | 5 +++- src/looped_in/sidebar.cljs | 57 ++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/looped_in/macros.clj b/src/looped_in/macros.clj index ffe2d6f..2a3bb16 100644 --- a/src/looped_in/macros.clj +++ b/src/looped_in/macros.clj @@ -1,3 +1,6 @@ (ns looped-in.macros) -(defmacro get-in-item [m ks] `(get-in ~m (vec (interpose :children ~ks)))) +(defmacro get-in-item [m ks] + `(if (seq ~ks) + (get-in ~m (cons :children (interpose :children ~ks))) + (get-in ~m ~ks))) diff --git a/src/looped_in/sidebar.cljs b/src/looped_in/sidebar.cljs index 75b5b40..e5e99bc 100644 --- a/src/looped_in/sidebar.cljs +++ b/src/looped_in/sidebar.cljs @@ -21,6 +21,14 @@ (.isArray js/Array v) (map obj->clj (array-seq v)) :default (js->clj v)))]))) +(defn sort-and-filter-item + [item] + (assoc item :children + (->> (:children item) + (filter #(contains? % :text)) + (sort-by #(count (:children % [])) #(compare %2 %1)) + (vec)))) + (defn fetch-item "Fetch the item with id `id`" [id] @@ -50,6 +58,7 @@ :got-hits (-> state (assoc :hits (:hits msg)) (assoc :loading false)) + :enq-depth (assoc state :depth (conj (:depth state) (:index msg))) :loading (assoc state :loading (:loading msg)) state)) @@ -59,7 +68,9 @@ (log/debug state) (cond (:loading state) (components/loader) - (:item state) (let [current-item (get-in-item (:item state) (:depth state))] + (:item state) (let [current-item (get-in-item + (sort-and-filter-item (:item state)) + (:depth state))] (cons (case (:type current-item) "story" (components/card @@ -67,14 +78,28 @@ (components/story-caption (:points current-item) (:author current-item) (* (:created_at_i current-item) 1000))) - "comment" ()) - (map (fn [child] + "comment" (components/card + (components/comment-caption (:author current-item) + (* (:created_at_i current-item) 1000)) + (components/comment-text (:text current-item)))) + (map-indexed (fn [index child] (-> (components/card (components/comment-caption (:author child) (* (:created_at_i child) 1000)) (components/comment-text (:text child)) (components/replies-indicator (count (:children child)))) - (components/with-classes "child"))) + (components/with-classes "child") + ((fn [card] + (if (> (count (:children child)) 0) + (-> card + (components/with-classes "clickable") + (components/with-listener + "click" + (fn [e] + (dispatch-message + {:type :enq-depth + :index index})))) + card))))) (->> (:children current-item) (filter #(contains? % :text)) (sort-by #(count (:children %)) #(compare %2 %1)))))) @@ -87,17 +112,19 @@ (components/comments-indicator (:num_comments hit))) ((fn [card] (if (> (:num_comments hit) 0) - (components/with-classes card "clickable") - card))) - (components/with-listener - "click" - (fn [e] - (dispatch-message {:type :loading :loading true}) - (go - (-> (fetch-item (:objectID hit)) - ( card + (components/with-classes "clickable") + (components/with-listener + "click" + (fn [e] + (dispatch-message {:type :loading :loading true}) + (go + (-> (fetch-item (:objectID hit)) + ( (count current-item) 1)