user editing stuff

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

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

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

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

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

@ -1,5 +1,14 @@
(ns wanijo.user.domain (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 (neo4j/defquery
find-user 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