Giter VIP home page Giter VIP logo

Comments (3)

jacobobryant avatar jacobobryant commented on July 18, 2024

Sure! I just looked through it. I don't think it's in need of any major changes; it looks quite good. So here are some stylistic suggestions, some of which may be my own personal preference:

  • Use pull instead of :where when possible

For example, get-posts currently looks like this:

(defn get-posts 
  [db]
  (biff/q db 
    '{:find [id title body email time]
      :keys [post-id post-title post-body post-author post-time]
      :where [[id :post/title title]
              [id :post/body body]
              [id :post/created time]
              [id :post/user author-id]
              [author-id :user/email email]]
      :order-by [[time :desc]]}))

You can simplify it to this:

(defn get-posts 
  [db]
  (->> (biff/q db 
               '{:find (pull post [* {:post/user [*]}])
                 :where [[post :post/title]]})
       (sort-by :post/created #(compare %2 %1))))

You would need to also change render-post from this:

(defn render-post
  [{:keys [post-id post-title post-body post-author post-time]} email]
  [:article.post {:id (str "post-" post-id)}
   [:header
    [:div
     [:h1 post-title]
     [:div.about (str "by " (first (clojure.string/split post-author #"@")) " on " post-time)]]
    (if email 
      [:a.action {:hx-get (str "/app/update/" post-id)
                  :hx-target (str "#post-" post-id)
                  :hx-swap "outerHTML"} "Edit"])]
   [:p.body post-body]])

to this:

(defn render-post
  [{:post/keys [title body user created] :keys [xt/id]} email]
  [:article.post {:id (str "post-" id)}
   [:header
    [:div
     [:h1 title]
     [:div.about (str "by " (first (clojure.string/split (:user/email user) #"@")) " on " created)]]
    (if email 
      [:a.action {:hx-get (str "/app/update/" id)
                  :hx-target (str "#post-" id)
                  :hx-swap "outerHTML"} "Edit"])]
   [:p.body body]])

I would make a similar change to get-post. In that case, since you have a specific document ID already, you can use xtdb.api/pull instead of q.

Nitpicks:

  • Change (get-post id node) to (get-post id (xt/db node)). Then get-post can remain a pure function.
  • Instead of calling clojure.string/split, add [clojure.string :as str] to your list of requires at the top of the namespace and then call str/split.
  • No need to call biff/render. Biff comes with some middleware that will call biff/render for you if the handler returns a vector (in which case we assume it's a hiccup vector).

from biff-code-review.

soex201 avatar soex201 commented on July 18, 2024

Hi, wow thank you so much for detail review. It really helps a learner like me.

from biff-code-review.

jacobobryant avatar jacobobryant commented on July 18, 2024

Any time!

from biff-code-review.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.