Skip to content

Commit

Permalink
Navigate between organisations by url
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhanshuguptagit committed Sep 19, 2019
1 parent 7970b4c commit e04f237
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 37 deletions.
21 changes: 14 additions & 7 deletions resources/public/assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,10 @@ a:hover {
margin-bottom: 4px;
}

.sidebar-section {
padding-left: 10px;
}

.sidebar-section-head {
color: white;
font-weight: bolder;
margin-bottom: 5px;
margin-top: 25px;
margin: 25px 0px 5px 8px;
}

.sidebar-section-list {
Expand All @@ -185,9 +180,19 @@ a:hover {
.sidebar-section-list > .item {
display: block;
line-height: 35px;
padding: 0 15px 0 15px;
padding: 0 15px 0 20px;
width: 100%;
text-decoration: none;
border: 2px solid transparent;
}

.sidebar-section-list .item:hover {
background: #363c4e;
}

.sidebar .item.active {
border-left: 2px solid aliceblue;
background: #404040;
}

.sidebar-section-list .item-label {
Expand All @@ -211,6 +216,7 @@ a:hover {
.sidebar-link {
color: #d5e2f1 !important;
cursor: pointer;
display: block;
}

.sidebar-link:hover {
Expand Down Expand Up @@ -262,6 +268,7 @@ a:hover {
margin-bottom: 0;
background-color: #fff;
box-shadow: 0px 2px rgba(0, 0, 0, 0.05), 0 1px 0 rgba(0, 0, 0, 0.05);
padding: 10px 10px 0 10px;
}

/* End Dashboard*/
2 changes: 1 addition & 1 deletion src/cljs/villagebookUI/components/create_org.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
(do
(on-close-handler)
(fn [res]
(fetchers/fetch-orgs!)))
(fetchers/fetch-orgs! last)))
#(reset! error true)))}
[:div.inline-block
[utils/color-picker-patch
Expand Down
3 changes: 2 additions & 1 deletion src/cljs/villagebookUI/components/dashboard.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require [villagebookUI.components.sidebar :refer [sidebar]]
[villagebookUI.components.main-content :refer [main-content]]))

(defn dashboard []
(defn dashboard [on-mount-cb]
(on-mount-cb)
(fn []
[:div
[sidebar]
Expand Down
6 changes: 4 additions & 2 deletions src/cljs/villagebookUI/components/main_content.cljs
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))]]])
16 changes: 10 additions & 6 deletions src/cljs/villagebookUI/components/sidebar.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +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
(for [org (org-store/get-all)]
[:li.item {:key (:id org)}
[:a.sidebar-link {:href "#"}
[utils/patch (:color org)]
(:name org)]])
(doall
(for [org (org-store/get-all)
:let [id (:id org)]]
[:li.item {:key (:id org)
: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)]
Expand Down
12 changes: 6 additions & 6 deletions src/cljs/villagebookUI/core.cljs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
(ns villagebookUI.core
(:require [reagent.core :as r]
[reagent.session :as session]
[accountant.core :as accountant]
[bidi.bidi :as bidi]

[villagebookUI.routes :refer [routes]]
[villagebookUI.store.core :as store]))
[villagebookUI.store.core :as store]
[villagebookUI.store.session :as session]))

(accountant/configure-navigation!
{:nav-handler (fn [path]
(let [matched-route (bidi/match-route routes path)
current-page (:handler matched-route)
route-params (:route-params matched-route)]
(session/put! :route {:current-page current-page
:route-params route-params})))
(session/set! {:current-page current-page
:route-params route-params})))
:path-exists? (fn [path]
(boolean (bidi/match-route routes path)))})

(defn root []
"Root component holding all other components"
(let [component (:current-page (session/get :route))]
(let [component (session/current-page)]
[component]))

(defn main! []
(accountant/dispatch-current!)
(store/init!)
(accountant/dispatch-current!)
(r/render [root]
(.getElementById js/document "villagebook-app")))

Expand Down
8 changes: 6 additions & 2 deletions src/cljs/villagebookUI/fetchers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
(:require [villagebookUI.store.organisations :as org-store]
[villagebookUI.api.organisation :as org-api]))

(defn fetch-orgs! []
(defn fetch-orgs!
[& [selector-fn]]
(org-api/get-all
(fn [res]
(org-store/add-all! res))
(org-store/add-all! res)
(when selector-fn
(org-store/set-selected!
(selector-fn (org-store/get-all)))))
identity))
8 changes: 7 additions & 1 deletion src/cljs/villagebookUI/helpers.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns villagebookUI.helpers)
(ns villagebookUI.helpers
(:require [villagebookUI.store.session :as session]))

(defn random-color []
(str "hsl(" (->> (.random js/Math)
Expand All @@ -9,3 +10,8 @@
[el handler]
(if (= (.-key el) "Escape")
(handler)))

(defn org-id-from-url []
(-> (session/route-params)
:org-id
js/parseInt))
4 changes: 2 additions & 2 deletions src/cljs/villagebookUI/middleware.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
[villagebookUI.components.login :refer [login]]))

(defn require-login
[component]
[component & props]
(fn []
(if (user-store/fetched?)
(if (user-store/read)
[component]
[(apply component props)]
[login])
[utils/loading])))
15 changes: 6 additions & 9 deletions src/cljs/villagebookUI/routes.cljs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
(ns villagebookUI.routes
(:require [reagent.session :as session]
[villagebookUI.fetchers :as fetchers]
(:require [villagebookUI.fetchers :as fetchers]
[villagebookUI.middleware :refer [require-login]]
[villagebookUI.components.login :refer [login]]
[villagebookUI.components.signup :refer [signup]]
[villagebookUI.components.dashboard :refer [dashboard]]
[villagebookUI.components.notfound :refer [notfound]]))

(def routes
["" {"/" login
"/signup" signup
"/dashboard" (require-login
(fn []
(fetchers/fetch-orgs!)
dashboard))
true notfound}])
["" {"/" login
"/signup" signup
"/dashboard" (require-login dashboard #(fetchers/fetch-orgs! first))
["/orgs/" :org-id] (require-login dashboard #(fetchers/fetch-orgs!))
true notfound}])
2 changes: 2 additions & 0 deletions src/cljs/villagebookUI/store/core.cljs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
(ns villagebookUI.store.core
(:require [villagebookUI.store.user :as user-store]
[villagebookUI.store.session :as session]
[villagebookUI.store.organisations :as org-store]
[villagebookUI.api.user :as user-api]))

(defn init! []
(session/init!)
(user-store/init!)
(org-store/init!)
(user-api/get-data!))
14 changes: 14 additions & 0 deletions src/cljs/villagebookUI/store/organisations.cljs
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
(ns villagebookUI.store.organisations
(:require [reagent.core :as r]
[clojure.walk :refer [keywordize-keys]]
[villagebookUI.helpers :as helpers]
[villagebookUI.store.state :refer [state]]))

(def organisations
(r/cursor state [:organisations]))

(def selected-organisation
(r/cursor state [:selected-organisation]))

(defn get-all []
@organisations)

(defn get-by-id [id]
(some #(if (= (:id %) id) %) (get-all)))

(defn get-selected []
(or (get-by-id (helpers/org-id-from-url))
@selected-organisation))

(defn add-all! [orgs]
(reset! organisations (map #(keywordize-keys %) orgs)))

(defn add-one! [org]
(swap! organisations conj (keywordize-keys org)))

(defn set-selected! [org]
(reset! selected-organisation (keywordize-keys org)))

(defn init! []
(reset! organisations {}))
23 changes: 23 additions & 0 deletions src/cljs/villagebookUI/store/session.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns villagebookUI.store.session
(:require [reagent.core :as r]
[villagebookUI.store.state :refer [state]]))

(def session
(r/cursor state [:session]))

(defn get-session []
@session)

(defn set!
[page]
(reset! session page))

(defn current-page []
(:current-page @session))

(defn route-params []
(:route-params @session))

(defn init! []
(reset! session {:current-page nil
:route-params nil}))

0 comments on commit e04f237

Please sign in to comment.