Update user address

This commit is contained in:
kagel 2016-08-23 02:27:49 +03:00
parent 8cd7f7705c
commit 601465ad39
8 changed files with 125 additions and 48 deletions

View File

@ -4,6 +4,30 @@ html, body {
padding-top: 0px;
}
.app {
width: 100%;
padding: 0;
margin: 0;
max-width: 100%;
}
.container {
padding-top: 20px;
}
.profile-settings {
padding-top: 20px;
.form-group {
padding-top: 20px;
}
button {
margin-top: 20px;
}
}
.profile-link {
display: inline-block;
padding-top: .425rem;

View File

@ -13,7 +13,7 @@ WHERE id = :id;
-- :name update-user-address! :! :n
UPDATE users
SET address = :address
WHERE id = :id;
WHERE login = :login;
-- :name get-user :? :1
-- :doc retrieve a user given the login.

View File

@ -24,3 +24,8 @@
[login]
(jdbc/with-db-connection [con-db *db*]
(some? (db/get-user con-db {:login login}))))
(defn update-user-address
[login address]
(jdbc/with-db-connection [con-db *db*]
(db/update-user-address! con-db {:login login :address address})))

View File

@ -4,7 +4,8 @@
[schema.core :as s]
[compojure.api.meta :refer [restructure-param]]
[buddy.auth.accessrules :refer [restrict]]
[buddy.auth :refer [authenticated?]]))
[buddy.auth :refer [authenticated?]]
[commiteth.db.users :as users]))
(defn access-error [_ _]
(unauthorized {:error "unauthorized"}))
@ -24,43 +25,21 @@
(defapi service-routes
{:swagger {:ui "/swagger-ui"
:spec "/swagger.json"
:data {:info {:version "1.0.0"
:title "Sample API"
:description "Sample Services"}}}}
:data {:info {:version "0.1"
:title "commitETH API"
:description "commitETH API"}}}}
(GET "/authenticated" []
:auth-rules authenticated?
:current-user user
(ok {:user user}))
(context "/api" []
:tags ["thingie"]
(GET "/plus" []
:return Long
:query-params [x :- Long, {y :- Long 1}]
:summary "x+y with query-parameters. y defaults to 1."
(ok (+ x y)))
(POST "/minus" []
:return Long
:body-params [x :- Long, y :- Long]
:summary "x-y with body-parameters."
(ok (- x y)))
(GET "/times/:x/:y" []
:return Long
:path-params [x :- Long, y :- Long]
:summary "x*y with path-parameters"
(ok (* x y)))
(POST "/divide" []
:return Double
:form-params [x :- Long, y :- Long]
:summary "x/y with form-parameters"
(ok (/ x y)))
(GET "/power" []
:return Long
:header-params [x :- Long, y :- Long]
:summary "x^y with header-parameters"
(ok (long (Math/pow x y))))))
(POST "/user/address" []
:auth-rules authenticated?
:body-params [user :- String, address :- String]
:summary "Update user address"
(let [result (users/update-user-address user address)]
(if (= 1 result)
(ok)
(internal-server-error))))))

View File

@ -8,7 +8,8 @@
[ajax.core :refer [GET POST]]
[commiteth.ajax :refer [load-interceptors!]]
[commiteth.handlers]
[commiteth.subscriptions])
[commiteth.subscriptions]
[reagent.core :as reagent])
(:import goog.History))
(defn nav-link [uri title page collapsed?]
@ -21,8 +22,9 @@
(defn login-link []
(let [user (rf/subscribe [:user])]
(if-let [login (:login @user)]
[:li.pull-right
[:span.profile-link (str "Logged in as " login)]
[:li.pull-right.p
[:span.profile-link "Logged in as "
[:a {:href "/#/profile"} login]]
[:a.btn.btn-primary.btn-sm {:href "/logout"} "Logout"]]
[:li.pull-right
[:a.btn.btn-social.btn-github
@ -43,17 +45,53 @@
[login-link]]]]))
(defn home-page []
[:div.container
[:div.jumbotron
[:h3 "Welcome to commitETH"]]])
[:div
[:h3 "Welcome to commitETH"]])
(defn input [{:keys [value-path]}]
(let [val (reagent/atom nil)
save #(let [v (-> @val str clojure.string/trim)]
(when (seq v)
(rf/dispatch [:assoc-in value-path v])))]
(fn [props]
[:input.form-control
(merge props {:type "text"
:value @val
:auto-focus true
:on-blur save
:on-change #(reset! val (-> % .-target .-value))})])))
(defn save-address
[login address]
(fn [_]
(rf/dispatch [:save-user-address login address])))
(defn address-settings []
(let [user (rf/subscribe [:user])
login (:login @user)
address (rf/subscribe [:address])]
(fn []
[:div.form-group
[:label "Address"]
[input {:placeholder "Address"
:value-path [:address]}]
[:button.btn.btn-primary.btn-lg
{:on-click (save-address login @address)}
"Save"]])))
(defn profile-page []
[:div.profile-settings
[:h1 "Profile"]
[address-settings]])
(def pages
{:home #'home-page})
{:home #'home-page
:profile #'profile-page})
(defn page []
[:div
[:div.app
[navbar]
[(pages @(rf/subscribe [:page]))]])
[:div.container [(pages @(rf/subscribe [:page]))]]])
;; -------------------------
;; Routes
@ -61,6 +99,8 @@
(secretary/defroute "/" []
(rf/dispatch [:set-active-page :home]))
(secretary/defroute "/profile" []
(rf/dispatch [:set-active-page :profile]))
;; -------------------------
;; History

View File

@ -1,5 +1,6 @@
(ns commiteth.db)
(def default-db
{:page :home
:user nil})
{:page :home
:user nil
:address nil})

View File

@ -1,12 +1,18 @@
(ns commiteth.handlers
(:require [commiteth.db :as db]
[re-frame.core :refer [dispatch reg-event-db]]))
[re-frame.core :refer [dispatch reg-event-db]]
[ajax.core :refer [GET POST]]))
(reg-event-db
:initialize-db
(fn [_ _]
db/default-db))
(reg-event-db
:assoc-in
(fn [db [_ path value]]
(assoc-in db path value)))
(reg-event-db
:set-active-page
(fn [db [_ page]]
@ -16,3 +22,20 @@
:set-active-user
(fn [db [_ user]]
(assoc db :user user)))
(reg-event-db
:set-user-address
(fn [db [_ address]]
(assoc db :address address)))
(defn save-user-address [params]
(POST "/api/user/address"
{:headers {"Accept" "application/transit+json"}
:params params
:handler #(println %)}))
(reg-event-db
:save-user-address
(fn [db [_ user address]]
(save-user-address {:user user :address address})
db))

View File

@ -10,3 +10,8 @@
:user
(fn [db _]
(:user db)))
(reg-sub
:address
(fn [db _]
(:address db)))