attribute updates, some styling, forms fixing

integration-tests
Josha von Gizycki 6 years ago
parent 95565a0aaf
commit 59c7df7ef3

@ -66,6 +66,15 @@ h2 {
.schema-title__name { .schema-title__name {
font-weight: normal; font-weight: normal;
} }
.schema-attributes {
list-style-type: none;
padding: 0;
li {
margin-bottom: 3rem;
}
}
} }
aside { aside {

@ -11,8 +11,7 @@
#(some (partial = %) ["date" "file", "string" "text"]))) #(some (partial = %) ["date" "file", "string" "text"])))
(spec/def ::required (spec/def ::required
(spec/or :bool-val boolean? #(some (partial = %) ["on" nil 0 1 "0" "1"]))
:nil nil?))
(neo4j/defquery (neo4j/defquery
findy-by-schema findy-by-schema
@ -37,13 +36,38 @@
CREATE (a)-[:of]->(s) CREATE (a)-[:of]->(s)
SET a.type = {type} SET a.type = {type}
SET a.name = {name} SET a.name = {name}
SET a.uuid = {attribute_uuid}") SET a.uuid = {attribute_uuid}
SET a.required = {required}")
(defn create-new! [name type schema-uuid user-uuid] (defn create-new! [attr schema-uuid user-uuid]
(neo4j/exec-query! (neo4j/exec-query!
create-new create-new
{:user_uuid user-uuid (-> attr
:schema_uuid schema-uuid (assoc
:attribute_uuid (neo4j/uuid) :user_uuid user-uuid
:type type :schema_uuid schema-uuid
:name name})) :attribute_uuid (neo4j/uuid))
(update :required #(if (some? %) 1 0)))))
(neo4j/defquery edit
"MATCH (a:attribute)
WHERE a.uuid = {uuid}
SET a.type = {type}
SET a.name = {name}
SET a.required = {required}")
(defn edit! [attr]
(neo4j/exec-query!
edit
(update attr
:required #(if (some? %) 1 0))))
(neo4j/defquery delete-by-uuid
"MATCH (a:attribute)-[c]-()
WHERE a.uuid = {uuid}
DELETE c, a")
(defn delete-by-uuid! [uuid]
(neo4j/exec-query!
delete-by-uuid
{:uuid uuid}))

@ -1,5 +1,5 @@
(ns wanijo.attribute.routes (ns wanijo.attribute.routes
(:require [compojure.core :refer [defroutes GET POST]] (:require [compojure.core :refer [defroutes GET POST DELETE]]
[ring.util.response :as resp] [ring.util.response :as resp]
[wanijo.framework.form :as form] [wanijo.framework.form :as form]
[wanijo.framework.routing :refer [register! path]] [wanijo.framework.routing :refer [register! path]]
@ -12,8 +12,7 @@
(if (form/valid? view-schema/attr-form req) (if (form/valid? view-schema/attr-form req)
(do (do
(domain/create-new! (domain/create-new!
(get-in req [:params :name]) (:params req)
(get-in req [:params :type])
schema-uuid schema-uuid
(get-in req [:session :uuid])) (get-in req [:session :uuid]))
(resp/redirect (path :schema-show {:uuid schema-uuid}))) (resp/redirect (path :schema-show {:uuid schema-uuid})))
@ -22,7 +21,27 @@
(domain/find-by-schema! schema-uuid) (domain/find-by-schema! schema-uuid)
req)))) req))))
(defn edit! [schema-uuid req]
(if (form/valid? view-schema/attr-form req)
(do
(domain/edit! (:params req))
(resp/redirect (path :schema-show {:uuid schema-uuid})))
(view-schema/show-schema!
(domain-schema/find-by-uuid! schema-uuid)
(domain/find-by-schema! schema-uuid)
req)))
(defn delete! [uuid req]
(domain/delete-by-uuid! uuid)
(resp/redirect (path :schema-show {:uuid (get-in req [:params :schema])})))
(defroutes routes (defroutes routes
(POST (register! :attribute-new "/attribute/new") (POST (register! :attribute-new "/attribute/new")
[] []
new!)) new!)
(POST (register! :attribute-edit "/attribute/edit/:schema")
[schema :as req]
(edit! schema req))
(DELETE (register! :attribute-delete "/attribute/:uuid/delete")
[uuid :as req]
(delete! uuid req)))

@ -39,9 +39,7 @@
(spec/explain-data spec-key field-value))))) (spec/explain-data spec-key field-value)))))
(defn field-valid?[value spec-key req] (defn field-valid?[value spec-key req]
(or (or (and (contains? req :form-params) (or (empty? (:form-params req))
(empty? (:form-params req)))
(empty? req))
(spec/valid? spec-key value))) (spec/valid? spec-key value)))
(defn field (defn field
@ -109,38 +107,40 @@
true true
(:params req))) (:params req)))
(defn render-input [id {:keys [label required spec]} values] (defn render-input [id {:keys [label required spec]} value req-value validate?]
(let [value (id values)] (list
(list (when-not (field-valid? value spec values) (when (and validate? (not (spec/valid? spec req-value)))
(spec-to-errmsg label spec value)) (spec-to-errmsg label spec req-value))
(hform/label id label) (hform/label id label)
(hform/text-field {:required (when required "required")} (hform/text-field {:required (when required "required")}
id id
value)))) (if validate? req-value value))))
(defn render-checkbox [id {:keys [label spec]} values] (defn render-checkbox [id {:keys [label spec]} value req-value validate?]
(let [value (id values)] (list
(list (when (and validate? (not (spec/valid? spec req-value)))
(when-not (field-valid? value spec values) (spec-to-errmsg label spec value))
(spec-to-errmsg label spec value)) (hform/label id label)
(hform/label id label) (hform/check-box id
(hform/check-box id (some? value) value)))) (and (some? value) (not= 0 value))
(if validate? req-value value))))
(defn render-textarea [id {:keys [label spec]} values] (defn render-textarea [id {:keys [label spec]} value req-value validate?]
(let [value (id values)] (list
(list (when (and validate? (not (spec/valid? spec req-value)))
(when-not (field-valid? value spec values) (spec-to-errmsg label spec value))
(spec-to-errmsg label spec value)) (hform/label id label)
(hform/label id label) (hform/text-area id
(hform/text-area id value)))) (if validate? req-value value))))
(defn render-select [id {:keys [label spec options]} values] (defn render-select [id {:keys [label spec options]} value req-value validate?]
(let [value (id values)] (list
(list (when (and validate? (not (spec/valid? spec req-value)))
(when-not (field-valid? value spec values) (spec-to-errmsg label spec value))
(spec-to-errmsg label spec value)) (hform/label id label)
(hform/label id label) (hform/drop-down id
(hform/drop-down id options value)))) options
(if validate? req-value value))))
(def render-mapping (def render-mapping
{:input render-input {:input render-input
@ -148,29 +148,25 @@
:textarea render-textarea :textarea render-textarea
:select render-select}) :select render-select})
(defn cleaned-values (defn render-form [form-def values req]
"Removes keys from value map that are not configured in the form"
[form-def values]
(reduce-kv
(fn [agg value-key value]
(if (contains? (:fields form-def) value-key)
(assoc agg value-key value)
agg))
{}
values))
(defn render-form [form-def values]
{:pre [(spec/valid? ::form form-def)]} {:pre [(spec/valid? ::form form-def)]}
(let [cleaned-values (cleaned-values form-def values)] (let [form-hash (str (hash [form-def values]))
submitted-hash (get-in req [:params :__form-hash])
validate? (= form-hash submitted-hash)]
(conj (map (conj (map
(fn [[field-id field-def]] (fn [[field-id field-def]]
(let [{:keys [options widget]} field-def (let [{:keys [options widget]} field-def
value (field-id values)
req-value (get-in req [:params field-id])
widget (cond (some? widget) widget widget (cond (some? widget) widget
(some? options) :select (some? options) :select
:else :input) :else :input)
widget-renderer (widget render-mapping)] widget-renderer (widget render-mapping)]
(widget-renderer field-id (widget-renderer field-id
field-def field-def
cleaned-values))) value
req-value
validate?)))
(:fields form-def)) (:fields form-def))
(hform/hidden-field "__form-hash" form-hash)
(anti-forgery-field)))) (anti-forgery-field))))

@ -64,17 +64,20 @@
[:span.schema-title__name (:name schema)]] [:span.schema-title__name (:name schema)]]
(hform/form-to [:post "/schema/edit"]) (hform/form-to [:post "/schema/edit"])
[:h2 "Attributes"] [:h2 "Attributes"]
[:ul [:ul.schema-attributes
(for [attr attrs] (for [attr attrs]
[:li [:li
(hform/form-to [:post ""] (hform/form-to [:post (path :attribute-edit {:schema (:uuid schema)})]
(form/render-form attr-form attr) (form/render-form attr-form attr req)
(hform/hidden-field "uuid" (:uuid attr))
(hform/submit-button "Save")) (hform/submit-button "Save"))
(hform/form-to [:delete ""] (hform/form-to [:delete (path :attribute-delete attr)]
(anti-forgery-field)
(hform/hidden-field "schema" (:uuid schema))
(hform/submit-button "Delete!"))])] (hform/submit-button "Delete!"))])]
[:h3 "New attribute"] [:h3 "New attribute"]
(hform/form-to [:post (path :attribute-new)] (hform/form-to [:post (path :attribute-new)]
(form/render-form attr-form (:params req)) (form/render-form attr-form {} req)
(hform/hidden-field "schema" (:uuid schema)) (hform/hidden-field "schema" (:uuid schema))
(hform/submit-button "Save")) (hform/submit-button "Save"))
[:h2 "Actions"] [:h2 "Actions"]

Loading…
Cancel
Save