user editing stuff

integration-tests
Josha von Gizycki 6 years ago
parent bf0c9814b4
commit dda41eb8c5

@ -13,7 +13,7 @@
(and (= cookie "1")
(not= query-param "off")))))
(defn devbar []
(defn devbar [resp]
(hcore/html
[:section.devbar
[:ol
@ -23,7 +23,7 @@
(defn append-devbar [resp]
(let [body (:body resp)
new-body (clojure.string/replace
body "</body>" (str (devbar) "</body>"))]
body "</body>" (str (devbar resp) "</body>"))]
(assoc resp :body new-body)))
(defn wrap-devmode [handler]

@ -35,21 +35,30 @@
(spec/explain-data spec-key field-value)))))
(defn field-valid? [value spec-key req]
(or (empty? (:form-params req))
(or (empty? (:params req))
(spec/valid? spec-key value)))
(defn field [form-def field req]
{:pre [(spec/valid? ::form form-def)]}
(let [field-value (get-in req [:params field])
field-def (get-in form-def [:fields field])
{:keys [label required] spec-key :spec} field-def]
(list
(when-not (field-valid? field-value spec-key req)
(spec-to-errmsg label spec-key field-value))
(hform/label field label)
(hform/text-field {:required (when required "required")}
field
field-value))))
(defn field
([form-def field-id req]
(field form-def field-id req
(get-in req [:params field])))
([form-def field-id req field-value]
{:pre [(spec/valid? ::form form-def)]}
(let [field-def (get-in form-def [:fields field-id])
{:keys [label required] spec-key :spec} field-def]
(list (when-not (field-valid? field-value spec-key req)
(spec-to-errmsg label spec-key field-value))
(hform/label field-id label)
(hform/text-field {:required (when required "required")}
field-id
field-value)))))
(defn readonly
[form-def field-id field-value]
(list (hform/label field-id (get-in form-def [:fields field-id :label]))
(hform/text-field {:readonly "readonly"}
field-id
field-value)))
(defn drop-down
([form-def field req]

@ -33,7 +33,8 @@
[:h1.app-title "wan ijo"
(when authed?
[:small.app-title__hello
(str "O, " ident)])]
"O, "
[:a {:href (path :user-profile)} ident]])]
(when authed?
[:section.header-content
(btnlink (path :schema-overview)

@ -8,6 +8,7 @@
[ring.middleware.session.cookie :as session-cookie]
[wanijo.home.routes :as home-routes]
[wanijo.schema.routes :as schema-routes]
[wanijo.user.routes :as user-routes]
[wanijo.framework.auth :as auth]
[wanijo.framework.devmode :as devmode]
[wanijo.framework.routing :refer [path]]))
@ -22,7 +23,8 @@
(routes auth/routes)
(wrap-login-redirect
(routes home-routes/routes
schema-routes/routes))
schema-routes/routes
user-routes/routes))
(route/not-found "Not Found"))
(def app

@ -1,5 +1,14 @@
(ns wanijo.user.domain
(:require [wanijo.framework.neo4j :as neo4j]))
(:require [clojure.spec.alpha :as spec]
[wanijo.framework.neo4j :as neo4j]))
(spec/def ::ident
(spec/and string? not-empty))
(spec/def ::name
(spec/or :empty #(or (empty? %) (nil? %))
:given (spec/and string? some?)))
(spec/def ::password
(spec/and string? #(> 7 (count %))))
(neo4j/defquery
find-user

@ -0,0 +1,16 @@
(ns wanijo.user.routes
(:require [compojure.core :refer [defroutes GET POST]]
[ring.util.response :as resp]
[wanijo.framework.routing :refer [register]]
[wanijo.user.view :as view-user]
[wanijo.user.domain :as domain]))
(register :user-profile "/user/profile")
(register :user-edit "/user/edit")
(defn profile! [req]
(view-user/profile! req
(domain/find! (get-in req [:session :ident]))))
(defroutes routes
(GET "/user/profile" [] profile!))

@ -0,0 +1,29 @@
(ns wanijo.user.view
(:require [hiccup.form :as hform]
[ring.util.anti-forgery :refer [anti-forgery-field]]
[wanijo.framework.view :as view]
[wanijo.framework.form :as form]
[wanijo.framework.routing :refer [path]]
[wanijo.user.domain :as domain]))
(def edit-form
{:fields {:name {:label "Nimi"
:required false
:spec ::domain/name}
:ident {:label "Ident"
:required false
:spec ::domain/ident}
:password {:label "Toki Pimeja"
:required true
:spec ::domain/password}}})
(defn profile! [req user]
(view/layout!
:session (:session req)
:content
[[:h1 "O, " (get-in req [:session :ident])]
[:div (str user)]
(hform/form-to [:post (path :user-edit)]
(anti-forgery-field)
(form/readonly edit-form :ident (:ident user))
(form/field edit-form :name req (:name user)))]))
Loading…
Cancel
Save