Advertisement
Guest User

Untitled

a guest
Jan 31st, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns article.core
  2.   (:require
  3.    [goog.string :as gstring]
  4.    [reagent.core :as reagent :refer [atom]]
  5.    [reagent.dom :as rdom]))
  6.  
  7. (defn log-clj! [obj]
  8.   (.log js/console (.stringify js/JSON (clj->js obj))))
  9.  
  10. (defn log! [obj]
  11.   (.log js/console (.stringify js/JSON obj)))
  12.  
  13. (def items [
  14.             {
  15.              :id "Article 1"
  16.              :title "My article 1"
  17.              :summary "Summary of article 1"
  18.              :display "none"
  19.              }
  20.             {
  21.              :id "Article 2"
  22.              :title "My article 2"
  23.              :summary "Summary of article 2"
  24.              :display "none"
  25.              }
  26.             {
  27.              :id "Article 3"
  28.              :title "My article 3"
  29.              :summary "Summary of article 3"
  30.              :display "none"
  31.              }
  32.             {
  33.              :id "Article 4"
  34.              :title "My article 4"
  35.              :summary "Summary of article 4"
  36.              :display "none"
  37.              }
  38.             ])
  39.  
  40. (defn article-item [{:keys [article onClickToggle onClickRemove]}]
  41.   [:li
  42.    [:a {
  43.         :href (str "#" (:id article))
  44.         :title "Toggle Summary"
  45.         :on-click #(onClickToggle (:id article))
  46.         }
  47.     (:title article)
  48.     ]
  49.    (gstring/unescapeEntities " ")
  50.    [:a {
  51.         :href (str "#" (:id article))
  52.         :title "Toggle Summary"
  53.         :on-click #(onClickRemove (:id article))
  54.         }
  55.     \u2717
  56.     ]
  57.    [:p {:style {:display (:display article)}} (:summary article)]])
  58.  
  59. (defn article-list [{:keys [articles onClickToggle onClickRemove]}]
  60.   [:ul
  61.    (for [article articles]
  62.      ^{:key (:id article)} [article-item {
  63.                                            :article article
  64.                                            :onClickToggle onClickToggle
  65.                                            :onClickRemove onClickRemove
  66.                                           }])
  67.    ]
  68.   )
  69.  
  70. (defn add-article [{:keys [name
  71.                            title
  72.                            summary
  73.                            onChangeTitle
  74.                            onChangeSummary
  75.                            onClickAdd]}]
  76.   [:section
  77.    [:h1 name]
  78.    [:input {
  79.             :placehold "Title"
  80.             :default-value title
  81.             :on-change onChangeTitle
  82.             }]
  83.    [:input {
  84.             :placehold "Summary"
  85.             :default-value summary
  86.             :on-change onChangeSummary
  87.             }]
  88.    [:button {:on-click onClickAdd} "Add"]])
  89.  
  90.  
  91. (defn my-feature []
  92.   (let [articles (reagent/atom items)
  93.         title (reagent/atom "")
  94.         summary (reagent/atom "")
  95.         ids (reagent/atom (count items))
  96.         onClickAdd (fn []
  97.                      (swap! ids inc)                    
  98.                      (swap! articles #(conj % {:id (str "Article " @ids)
  99.                                                :title @title
  100.                                                :summary @summary
  101.                                                :display "none"}))
  102.                      )
  103.         onChangeSummary #(reset! summary (-> % .-target .-value))
  104.         onClickRemove   (fn [id]
  105.                           (log! id)
  106.                           (swap! articles (fn [a] (filter #(not= id (:id %)) a)))
  107.                           )
  108.         onChangeTitle   #(reset! title (-> % .-target .-value))]        
  109.         (fn []
  110.           [:section
  111.            [add-article  {
  112.                           :title @title
  113.                           :summary @summary
  114.                           :name "Articles"
  115.                           :onClickAdd onClickAdd
  116.                           :onChangeSummary onChangeSummary
  117.                           :onChangeTitle onChangeTitle
  118.                           }]
  119.            [article-list {:articles @articles :onClickRemove onClickRemove}]])))    
  120.  
  121.   (defn app []
  122.     [:div
  123.      [my-feature {:articles items}]])
  124.  
  125.   (defn mount-root []
  126.     (rdom/render [app] (.getElementById js/document "app")))
  127.  
  128.   (defn init! []
  129.     (mount-root))
  130.  
  131.   (defn ^:dev/after-load reload! []
  132.     (mount-root))
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement