-
Notifications
You must be signed in to change notification settings - Fork 6
/
guestbook.cljs
105 lines (97 loc) · 3.25 KB
/
guestbook.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
;---
; Excerpted from "Web Development with Clojure, Third Edition",
; published by The Pragmatic Bookshelf.
; Copyrights apply to this code. It may not be used to create training material,
; courses, books, articles, and the like. Contact us if you are in doubt.
; We make no guarantees that this code is fit for any purpose.
; Visit http://www.pragmaticprogrammer.com/titles/dswdcloj3 for more book information.
;---
; zip archive retrieved from the "Resources" section of
; https://pragprog.com/titles/dswdcloj3/web-development-with-clojure-third-edition/
; file: guestbook-reagent/src/cljs/guestbook/core.cljs
(ns guestbook.core
(:require [reagent.core :as r]
[reagent.dom :as dom]
[ajax.core :refer [GET POST]]
[clojure.string :as string]
#_[cljs.pprint :refer [pprint]]))
;
(defn get-messages [messages]
(GET "/messages"
{:headers {"Accept" "application/transit+json"}
:handler #(reset! messages (:messages %))}))
;
;
(defn message-list [messages]
(println messages)
[:ul.messages
(for [{:keys [timestamp message name]} @messages]
^{:key timestamp}
[:li
[:time (.toLocaleString timestamp)]
[:p message]
[:p " - " name]])])
;
;
(defn send-message! [fields errors messages]
(POST "/message"
{;; :format :json
:headers
{"Accept" "application/transit+json"
;;"x-csrf-token" (.-value (.getElementById js/document "token"))
}
:params @fields
:handler (fn [_]
(swap! messages conj (assoc @fields
:timestamp (js/Date.)))
(reset! fields nil)
(reset! errors nil))
:error-handler (fn [e]
(.log js/console (str e))
(reset! errors (-> e :response :errors)))}))
;
;
(defn errors-component [errors id]
(when-let [error (id @errors)]
[:div.notification.is-danger (string/join error)]))
;
;
(defn message-form [messages]
(let [fields (r/atom {})
errors (r/atom nil)]
(fn []
[:div
[errors-component errors :server-error]
[:div.field
[:label.label {:for :name} "Name"]
[errors-component errors :name]
[:input.input
{:type :text
:name :name
:on-change #(swap! fields
assoc :name (-> % .-target .-value))
:value (:name @fields)}]]
[:div.field
[:label.label {:for :message} "Message"]
[errors-component errors :message]
[:textarea.textarea
{:name :message
:value (:message @fields)
:on-change #(swap! fields
assoc :message (-> % .-target .-value))}]]
[:input.button.is-primary
{:type :submit
:on-click #(send-message! fields errors messages)
:value "comment"}]])))
(defn home []
(let [messages (r/atom nil)]
(get-messages 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]]])))
;
(dom/render [home] (.getElementById js/document "content"))