From dda41eb8c504fbc108e94c7347867ef6a5eb4627 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Thu, 26 Jul 2018 14:57:19 +0200 Subject: [PATCH] user editing stuff --- src/wanijo/framework/devmode.clj | 4 ++-- src/wanijo/framework/form.clj | 35 ++++++++++++++++++++------------ src/wanijo/framework/view.clj | 3 ++- src/wanijo/handler.clj | 4 +++- src/wanijo/user/domain.clj | 11 +++++++++- src/wanijo/user/routes.clj | 16 +++++++++++++++ src/wanijo/user/view.clj | 29 ++++++++++++++++++++++++++ 7 files changed, 84 insertions(+), 18 deletions(-) create mode 100644 src/wanijo/user/routes.clj create mode 100644 src/wanijo/user/view.clj diff --git a/src/wanijo/framework/devmode.clj b/src/wanijo/framework/devmode.clj index 7a1b558..6f19865 100644 --- a/src/wanijo/framework/devmode.clj +++ b/src/wanijo/framework/devmode.clj @@ -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 "" (str (devbar) ""))] + body "" (str (devbar resp) ""))] (assoc resp :body new-body))) (defn wrap-devmode [handler] diff --git a/src/wanijo/framework/form.clj b/src/wanijo/framework/form.clj index 2a07ec1..ddc116f 100644 --- a/src/wanijo/framework/form.clj +++ b/src/wanijo/framework/form.clj @@ -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] diff --git a/src/wanijo/framework/view.clj b/src/wanijo/framework/view.clj index 55ba465..4939e8a 100644 --- a/src/wanijo/framework/view.clj +++ b/src/wanijo/framework/view.clj @@ -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) diff --git a/src/wanijo/handler.clj b/src/wanijo/handler.clj index f4306af..dd381c5 100644 --- a/src/wanijo/handler.clj +++ b/src/wanijo/handler.clj @@ -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 diff --git a/src/wanijo/user/domain.clj b/src/wanijo/user/domain.clj index 0572fdd..3787d3a 100644 --- a/src/wanijo/user/domain.clj +++ b/src/wanijo/user/domain.clj @@ -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 diff --git a/src/wanijo/user/routes.clj b/src/wanijo/user/routes.clj new file mode 100644 index 0000000..b9b4cd9 --- /dev/null +++ b/src/wanijo/user/routes.clj @@ -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!)) diff --git a/src/wanijo/user/view.clj b/src/wanijo/user/view.clj new file mode 100644 index 0000000..8b0fa88 --- /dev/null +++ b/src/wanijo/user/view.clj @@ -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)))]))