parent
833347b198
commit
fe9c48f405
@ -0,0 +1,24 @@
|
||||
(ns wanijo.tag.forms
|
||||
(:require [clojure.spec.alpha :as spec]
|
||||
[clojure.string :refer [split trim]]))
|
||||
|
||||
(defn contains-whitespace? [s]
|
||||
(re-matches #".*\s.*" s))
|
||||
|
||||
(defn tag-names-from-input [s]
|
||||
(->> (split s #",")
|
||||
(map trim)))
|
||||
|
||||
(defn any-tag-name-contains-whitespace [s]
|
||||
(some contains-whitespace? (tag-names-from-input s)))
|
||||
|
||||
(spec/def ::new-names
|
||||
(spec/and string?
|
||||
(complement empty?)
|
||||
(complement any-tag-name-contains-whitespace)))
|
||||
|
||||
(def new-tag
|
||||
{:fields {:newnames {:label "Tag Name(s)"
|
||||
:required true
|
||||
:spec ::new-names
|
||||
:from-req tag-names-from-input}}})
|
@ -0,0 +1,32 @@
|
||||
(ns wanijo.tag.routes
|
||||
(:require [compojure.core :refer [defroutes POST]]
|
||||
[ring.util.response :as resp]
|
||||
[formulare.core :as form]
|
||||
[wanijo.framework.routing :refer [register! path]]
|
||||
[wanijo.schema.domain :as domain-schema]
|
||||
[wanijo.instance
|
||||
[view :as view-instance]
|
||||
[domain :as domain-instance]]
|
||||
[wanijo.tag
|
||||
[domain :as domain]
|
||||
[forms :as forms]]))
|
||||
|
||||
(defn create-tag! [instance-uuid req]
|
||||
(let [{new-names :newnames} (form/form-data forms/new-tag req)
|
||||
user-uuid (-> req :session :uuid)]
|
||||
(if (form/valid? forms/new-tag req)
|
||||
(do
|
||||
(domain/merge-tags new-names
|
||||
instance-uuid
|
||||
user-uuid)
|
||||
(resp/redirect (path :instance-show
|
||||
{:uuid instance-uuid})))
|
||||
(view-instance/show!
|
||||
(domain-instance/full-instance-by-uuid! instance-uuid)
|
||||
(domain-schema/accessible-schemas! user-uuid)
|
||||
req))))
|
||||
|
||||
(defroutes routes
|
||||
(POST (register! :tag-create "/tag/:instance-uuid")
|
||||
[instance-uuid :as req]
|
||||
(create-tag! instance-uuid req)))
|
@ -0,0 +1,21 @@
|
||||
(ns wanijo.tag.view
|
||||
(:require [hiccup.form :as hform]
|
||||
[hiccup.core :refer [h]]
|
||||
[ring.util.anti-forgery :refer [anti-forgery-field]]
|
||||
[formulare.core :as form]
|
||||
[wanijo.tag.forms :as forms]
|
||||
[wanijo.framework.routing :refer [path]]))
|
||||
|
||||
(defn tag-list [tags]
|
||||
[:ul.tag-list
|
||||
(for [tag tags]
|
||||
[:li
|
||||
[:code ":" (h (:name tag))]])])
|
||||
|
||||
(defn new-tag-form [{uuid :uuid}]
|
||||
(list
|
||||
(hform/form-to [:post (path :tag-create {:instance-uuid uuid})]
|
||||
(form/render-widgets forms/new-tag {} {})
|
||||
(hform/submit-button "Tag!"))
|
||||
[:small (str "Comma separate each tag. "
|
||||
"Tag names must not contain whitespace.")]))
|
Loading…
Reference in new issue