-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Display and navigate b/w organisations in the sidebar #12
Changes from 9 commits
040d444
87a7bee
47fd7e4
e9bbd98
cb980bc
025d4df
5fc0a46
7970b4c
e04f237
9f45a2d
dd86386
d79f2b5
58de60b
63b7e8c
72a7fea
8827bbc
da0140e
84e8c77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,8 +9,9 @@ | |||||
[villagebook.config :as config])) | ||||||
|
||||||
(def apiroutes | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{"organisations" {["/" :id] {:get (with-auth org/retrieve)} | ||||||
:post (with-auth org/create!)} | ||||||
{"organisations" {:get (with-auth org/retrieve-by-user) | ||||||
:post (with-auth org/create!) | ||||||
["/" :id] {:get (with-auth org/retrieve)}} | ||||||
"user" {:get (with-auth user/retrieve)}}) | ||||||
|
||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
(ns villagebookUI.api.organisation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Capitals in the ns name seems odd. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes🤷♂ but decided to stick with it, changing would be too much effort. |
||
[:require [ajax.core :refer [POST]]]) | ||
[:require [ajax.core :refer [GET POST]]]) | ||
|
||
(def ^:private create-org-api "/api/organisations") | ||
(def ^:private get-all-orgs-api "/api/organisations") | ||
|
||
(defn create-org [org-data handler-fn error-handler-fn] | ||
(defn create! [org-data handler-fn error-handler-fn] | ||
(POST create-org-api | ||
{:params org-data | ||
:format :raw | ||
:handler handler-fn | ||
:error-handler error-handler-fn})) | ||
|
||
(defn get-all [handler-fn error-handler-fn] | ||
(GET get-all-orgs-api | ||
{:handler handler-fn | ||
:response-format :json | ||
:error-handler error-handler-fn})) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
(ns villagebookUI.api.user | ||
[:require [ajax.core :refer [GET]]]) | ||
(:require [ajax.core :refer [GET]] | ||
[villagebookUI.store.user :as store])) | ||
|
||
(def ^:private get-user-data-api "/api/user") | ||
|
||
(defn get-user-data | ||
[handler-fn error-handler-fn] | ||
(defn get-data [handler-fn error-handler-fn finally-fn] | ||
(GET get-user-data-api | ||
{:handler handler-fn | ||
:error-handler error-handler-fn})) | ||
{:handler handler-fn | ||
:error-handler error-handler-fn | ||
:finally finally-fn})) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation seems off here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd also just call them |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
(ns villagebookUI.components.create-org | ||
(:require [reagent.core :as r] | ||
[villagebookUI.helpers :refer [random-color handle-esc]] | ||
[villagebookUI.fetchers :as fetchers] | ||
[villagebookUI.components.utils :as utils] | ||
[villagebookUI.api.organisation :as org])) | ||
[villagebookUI.api.organisation :as org-api])) | ||
|
||
(defn- create-org [form success-handler error-handler] | ||
(when (:name form) | ||
(org/create-org form | ||
success-handler | ||
error-handler))) | ||
(org-api/create! form | ||
success-handler | ||
error-handler))) | ||
|
||
(defn org-creation-form [on-close-handler] | ||
(let [color (random-color) | ||
|
@@ -19,7 +20,10 @@ | |
[:form.inline-block {:on-submit (fn [e] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract this handler function out. Don't write more than a line of code inside the view, it isn't as tangible, and is not testable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
(.preventDefault e) | ||
(create-org @form | ||
on-close-handler | ||
(do | ||
(on-close-handler) | ||
(fn [res] | ||
(fetchers/fetch-orgs! last))) | ||
#(reset! error true)))} | ||
[:div.inline-block | ||
[utils/color-picker-patch | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,9 @@ | |
(:require [villagebookUI.components.sidebar :refer [sidebar]] | ||
[villagebookUI.components.main-content :refer [main-content]])) | ||
|
||
(defn dashboard [] | ||
(let [] | ||
(fn [] | ||
[:div | ||
[sidebar] | ||
[main-content]]))) | ||
(defn dashboard [on-mount-cb] | ||
(on-mount-cb) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When in the component lifecycle is this called? Not sure that this gives you good enough control. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is called once per component (when it's rendered the first time). Reagent provides this construct for setup or initialising local state. Works just like componentDidMount. Relevant doc |
||
(fn [] | ||
[:div | ||
[sidebar] | ||
[main-content]])) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,9 @@ | |
(:require [reagent.core :as r] | ||
[accountant.core :as accountant] | ||
|
||
[villagebookUI.fetchers :as fetchers] | ||
[villagebookUI.api.auth :as auth] | ||
[villagebookUI.store :as store])) | ||
[villagebookUI.store.user :as user-store])) | ||
|
||
(defn validate! | ||
[elementID formdata] | ||
|
@@ -27,7 +28,7 @@ | |
(let [formdata (r/atom {}) | ||
error (r/atom {})] | ||
(fn [] | ||
(if @store/user | ||
(if (user-store/read) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Structure this as a guard clause instead? Also, extract the else part into another function, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've refactored this here in a separate WIP PR. |
||
(do | ||
(accountant/navigate! "/dashboard") | ||
[:div]) | ||
|
@@ -46,6 +47,7 @@ | |
(:user @formdata) | ||
(fn [res] | ||
(swap! error assoc :message "") | ||
(fetchers/fetch-user!) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract out the on-click function here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will refactor this bit in #14 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merged #14 into this branch. |
||
(accountant/navigate! "/dashboard")) | ||
(fn [res] | ||
(swap! error assoc :message (:response res)))))} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
(ns villagebookUI.components.main-content) | ||
(ns villagebookUI.components.main-content | ||
(:require [villagebookUI.store.organisations :as org-store])) | ||
|
||
(defn main-content [] | ||
[:div.main-content | ||
[:div.navbar]]) | ||
[:div.navbar | ||
[:h5 (:name (org-store/get-selected))]]]) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ | |||||
(:require [reagent.core :as r] | ||||||
[villagebookUI.components.utils :as utils] | ||||||
[villagebookUI.components.create-org :refer [org-creation-form]] | ||||||
[villagebookUI.api.organisation :as org])) | ||||||
[villagebookUI.store.organisations :as org-store])) | ||||||
|
||||||
(defn sidebar [] | ||||||
(let [creating-org (r/atom false)] | ||||||
|
@@ -13,12 +13,17 @@ | |||||
[:p.sidebar-logo "villagebook"] | ||||||
[:div.divider]] | ||||||
[:div.sidebar-section | ||||||
[:p.sidebar-section-head "Organisations"] | ||||||
[:p.sidebar-section-head "Your organisations"] | ||||||
[:ul.sidebar-section-list | ||||||
[:li.item | ||||||
[:a.sidebar-link [utils/patch "#5fcc5f"] "Org 1"]] | ||||||
[:li.item | ||||||
[:a.sidebar-link [utils/patch "#ff8383"] "Org 2"]] | ||||||
(doall | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needed since |
||||||
(for [org (org-store/get-all) | ||||||
:let [id (:id org)]] | ||||||
[:li.item {:key (:id org) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
:class (if (= id (:id (org-store/get-selected))) | ||||||
[:active])} | ||||||
[:a.sidebar-link {:href (str "/orgs/" id)} | ||||||
[utils/patch (:color org)] | ||||||
(:name org)]])) | ||||||
[:li.item | ||||||
(if @creating-org | ||||||
[org-creation-form #(reset! creating-org false)] | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
(ns villagebookUI.fetchers | ||
(:require [villagebookUI.store.user :as user-store] | ||
[villagebookUI.api.user :as user-api] | ||
[villagebookUI.store.organisations :as org-store] | ||
[villagebookUI.api.organisation :as org-api])) | ||
|
||
|
||
(defn fetch-user! [] | ||
(user-api/get-data | ||
(fn [res] | ||
(user-store/add! res)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
(fn [res] | ||
(user-store/add! nil)) | ||
#(user-store/fetched!))) | ||
|
||
(defn fetch-orgs! | ||
[& [selector-fn]] | ||
(org-api/get-all | ||
(fn [res] | ||
(org-store/add-all! res) | ||
(when selector-fn | ||
(org-store/set-selected! | ||
(selector-fn (org-store/get-all))))) | ||
identity)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
(ns villagebookUI.middleware | ||
(:require [villagebookUI.store :as store] | ||
(:require [villagebookUI.store.user :as user-store] | ||
[villagebookUI.components.utils :as utils] | ||
[villagebookUI.components.login :refer [login]])) | ||
|
||
(defn require-login | ||
[component] | ||
[component & props] | ||
(fn [] | ||
(if (store/fetched?) | ||
(if @store/user | ||
[component] | ||
(if (user-store/fetched?) | ||
(if (user-store/read) | ||
[(apply component props)] | ||
[login]) | ||
[utils/loading]))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.