(require '[bb-web :refer [state]]
'[goog.object :as go]
'[reagent.core :as r])
;
(defn message-list [messages]
(println messages)
[:ul.messages
(for [{:keys [timestamp message name]} @messages]
^{:key timestamp}
[:li
[:time timestamp]
[:p message]
[:p " - " name]])])
;
;
(defn send-message! [fields messages]
(do
(swap! messages conj (assoc @fields :timestamp (bb-web/timestamp)))
(reset! fields nil)))
;
;
(defn message-form [messages]
(let [fields (r/cursor state [:fields])]
(fn [messages]
[:div
[:div.field
[:label.label {:for :name} "Name"]
[:input.input
{:type :text
:name :name
:on-change (fn [e] (swap! fields assoc :name
(-> e (go/get "target") (go/get "value"))))
:value (:name @fields)}]]
[:div.field
[:label.label {:for :message} "Message"]
[:textarea.textarea
{:name :message
:value (:message @fields)
:on-change (fn [e] (swap! fields assoc :message
(-> e (go/get "target") (go/get "value"))))}]]
[:input.button.is-primary
{:type :submit
:on-click (fn [_] (send-message! fields messages))
:value "comment"}]])))
(defn home []
(let [messages (r/cursor state [:messages])]
(fn []
[:div.content>div.columns.is-centered>div.column.is-two-thirds
[:div.columns>div.column
[:h3 "Messages"]
[message-list messages]]
[:div.columns>div.column
[message-form messages]]])))
;
[home]