made it possible to change layouts.

This commit is contained in:
Bryce Covert
2017-12-19 19:31:13 -08:00
parent 043e558a51
commit b1414ebe60
6 changed files with 120 additions and 54 deletions

View File

@@ -4,17 +4,11 @@
[auto-ap.events :as events]
[auto-ap.views :as views]
[auto-ap.config :as config]
[auto-ap.routes :as routes]
[auto-ap.effects :as effects]
[pushy.core :as pushy]
[auto-ap.pushy :as p]
[bidi.bidi :as bidi]))
(defn- parse-url [url]
(bidi/match-route routes/routes url))
(defn- dispatch-route [matched-route]
(re-frame/dispatch [::events/set-active-page (:handler matched-route)]))
(defn dev-setup []
(when true
(enable-console-print!)
@@ -22,16 +16,19 @@
(defn mount-root []
(re-frame/clear-subscription-cache!)
(reagent/render [views/main-panel]
(reagent/render [views/page]
(.getElementById js/document "app")))
(defn ^:export init []
(dev-setup)
(when-let [jwt (.get (js/URLSearchParams. (.-search (.-location js/window))) "jwt")]
(println "got jwt" jwt)
(.setItem js/localStorage "jwt" jwt))
(pushy/start! (pushy/pushy dispatch-route parse-url))
(re-frame/dispatch-sync [::events/initialize-db])
(pushy/start! p/history)
(if-let [jwt (.get (js/URLSearchParams. (.-search (.-location js/window))) "jwt")]
(do
(.setItem js/localStorage "jwt" jwt)
(re-frame/dispatch-sync [::events/initialize-db jwt]))
(do
(re-frame/dispatch-sync [::events/initialize-db nil])))
(mount-root))

View File

@@ -1,16 +1,23 @@
(ns auto-ap.effects
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [re-frame.core :as re-frame]
[reagent.core :as reagent]
[auto-ap.subs :as subs]
[auto-ap.events :as events]
[cljs.reader :as edn]
[cljsjs.dropzone :as dz]
[auto-ap.routes :as routes]
[bidi.bidi :as bidi]
[cljs-http.client :as http]
[cljs.core.async :refer [<!]]))
[cljs.core.async :refer [<!]]
[auto-ap.pushy :as p]
[pushy.core :as pushy]))
(re-frame/reg-fx
:redirect
(fn [uri]
(println uri)
(pushy/set-token! p/history uri)))
(re-frame/reg-fx
:set-local-storage
(fn [[name value]]
(if value
(.setItem js/localStorage name value)
(.removeItem js/localStorage name ))))
(re-frame/reg-fx
:http

View File

@@ -2,14 +2,21 @@
(:require [re-frame.core :as re-frame]
[auto-ap.db :as db]
[auto-ap.routes :as routes]
[auto-ap.effects :as effects]
[bidi.bidi :as bidi]))
(re-frame/reg-event-db
(re-frame/reg-event-fx
::initialize-db
(fn [_ _]
(assoc db/default-db
:active-page (:handler (bidi/match-route routes/routes (.. js/window -location -pathname))))))
(fn [{:keys [db]} [_ token]]
(let [handler (:handler (bidi/match-route routes/routes (.. js/window -location -pathname)))]
(if (and (not= :login handler) (not token))
{:redirect "/login"
:db (assoc db/default-db
:active-page :login
:user token)}
{:db (assoc db/default-db
:active-page handler
:user token)}))))
(re-frame/reg-event-db
::toggle-menu
@@ -26,10 +33,13 @@
(fn [db [_ company]]
(assoc db :company company)))
(re-frame/reg-event-db
(re-frame/reg-event-fx
::set-active-page
(fn [db [_ active-page]]
(assoc db :active-page active-page)))
(fn [{:keys [db]} [_ handler]]
(if (and (not= :login handler) (not (:user db)))
{:redirect "/login"
:db (assoc db :active-page :login)}
{:db (assoc db :active-page handler)})))
(re-frame/reg-event-db
::imported-invoices
@@ -84,11 +94,13 @@
(fn [db [_ invoice]]
(assoc db :new-invoice {})))
(re-frame/reg-event-db
(re-frame/reg-event-fx
::logout
(fn [db [_]]
(assoc db :user nil :menu {:company {:active? false}
:account {:active? false}})))
(fn [{:keys [db]} [_]]
{:db (assoc db :user nil :menu {:company {:active? false}
:account {:active? false}})
:redirect (bidi/path-for routes/routes :login)
:set-local-storage ["jwt" nil]}))
(re-frame/reg-event-fx
::submit-new-invoice

View File

@@ -0,0 +1,14 @@
(ns auto-ap.pushy
(:require [bidi.bidi :as bidi]
[pushy.core :as pushy]
[auto-ap.routes :as routes]
[re-frame.core :as re-frame]))
(defn- parse-url [url]
(bidi/match-route routes/routes url))
(defn- dispatch-route [matched-route]
(re-frame/dispatch [:auto-ap.events/set-active-page (:handler matched-route)]))
(def history (pushy/pushy dispatch-route parse-url))

View File

@@ -2,6 +2,7 @@
(:require [bidi.bidi :as bidi]))
(def routes ["/" {"" :index
"login/" :login
"invoices/" {"" :invoices
"import" :import-invoices
"unpaid" :unpaid-invoices

View File

@@ -10,6 +10,10 @@
[bidi.bidi :as bidi]
[cljs-http.client :as http]
[cljs.core.async :refer [<!]]))
(defn login-url []
(let [client-id "264081895820-0nndcfo3pbtqf30sro82vgq5r27h8736.apps.googleusercontent.com"
redirect-uri "http%3A%2F%2Flocalhost%3A3449%2Fapi%2Foauth"]
(str "https://accounts.google.com/o/oauth2/auth?access_type=online&client_id=" client-id "&redirect_uri=" redirect-uri "&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile")))
(defn active-when= [active-page candidate]
(when (= active-page candidate) " active"))
@@ -37,8 +41,18 @@
:previewsContainer "#dz-hidden"
:previewTemplate "<div class='dz-hidden-preview'></div>"})))})))
(defmulti active-page identity)
(defmethod active-page :login []
[:div {:class "inbox-messages"}
[:div.hero
[:div.hero-body
[:div.container
[:h1.title "Login"]
[:h2.subtitle "To get started, "
[:a {:href (login-url)} "Login with Google"]]]]]])
(defmethod active-page :index []
[:div {:class "inbox-messages"}
[:div.hero
@@ -217,28 +231,39 @@
[:i.fa.fa-spin.fa-spinner])
"Save"]]]]]))
(defn login-url []
(let [client-id "264081895820-0nndcfo3pbtqf30sro82vgq5r27h8736.apps.googleusercontent.com"
redirect-uri "http%3A%2F%2Flocalhost%3A3449%2Fapi%2Foauth"]
(str "https://accounts.google.com/o/oauth2/auth?access_type=online&client_id=" client-id "&redirect_uri=" redirect-uri "&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile")))
(defn login []
(let [user (re-frame/subscribe [::subs/user])
menu (re-frame/subscribe [::subs/menu])]
[:div {:id "navMenu", :class "navbar-menu"}
[:div {:class "navbar-end"}
[:div {:class (str "navbar-item has-dropdown " (when (get-in @menu [:account :active?]) "is-active"))}
(if @user
(if @user
[:div {:class (str "navbar-item has-dropdown " (when (get-in @menu [:account :active?]) "is-active"))}
[:a {:class "navbar-link login" :on-click (fn [e] (re-frame/dispatch [::events/toggle-menu :account]))} (:name @user)]
[:a {:class "navbar-link login" :href (login-url)} "Login"])
[:div {:class "navbar-dropdown"}
[:a {:class "navbar-item"} "My profile"]
[:hr {:class "navbar-divider"}]
[:a.navbar-item {:on-click (fn [e] (.preventDefault e) (re-frame/dispatch [::events/logout]))} "Logout"]]]]]))
[:div {:class "navbar-dropdown"}
[:a {:class "navbar-item"} "My profile"]
[:hr {:class "navbar-divider"}]
[:a.navbar-item {:on-click (fn [e] (.preventDefault e) (re-frame/dispatch [::events/logout]))} "Logout"]]]
[:a.navbar-item {:href (login-url)} "Login"])]]))
(defn main-panel []
{"" :index
"login/" :login
"invoices/" {"" :invoices
"import" :import-invoices
"unpaid" :unpaid-invoices
"paid" :paid-invoices
"new" :new-invoice}}
(defmulti main-layout (fn [x] ({:login :blank
:index :left-panel
:invoices :left-panel
:import-invoices :left-panel
:unpaid-invoices :left-panel
:paid-invoices :left-panel
:new-invoice :blank} x)))
(defmethod main-layout :left-panel [ap]
(let [company (re-frame/subscribe [::subs/company])
ap (re-frame/subscribe [::subs/active-page])
companies (re-frame/subscribe [::subs/companies])
menu (re-frame/subscribe [::subs/menu])]
[:div
@@ -273,27 +298,27 @@
[:div.main
[:p.menu-label "General"]
[:p.menu-item
[:a {:href (bidi/path-for routes/routes :index) , :class (str "item" (active-when= @ap :index))}
[:span {:class "icon"}
[:i {:class "fa fa-tachometer"}]]
[:span {:class "name"} "Dashboard"]]]
[:a {:href (bidi/path-for routes/routes :index) , :class (str "item" (active-when= ap :index))}
[:span {:class "icon"}
[:i {:class "fa fa-tachometer"}]]
[:span {:class "name"} "Dashboard"]]]
[:p.menu-label "Accounts Payable"]
[:ul.menu-list
[:li.menu-item
[:a {:href (bidi/path-for routes/routes :import-invoices) , :class (str "item" (active-when= @ap :import-invoices))}
[:a {:href (bidi/path-for routes/routes :import-invoices) , :class (str "item" (active-when= ap :import-invoices))}
[:span {:class "icon"}
[:i {:class "fa fa-star-o"}]]
[:span {:class "name"} "Upload Invoices"]]]
[:li.menu-item
[:a {:href (bidi/path-for routes/routes :unpaid-invoices), :class (str "item" (active-when= @ap :unpaid-invoices))}
[:a {:href (bidi/path-for routes/routes :unpaid-invoices), :class (str "item" (active-when= ap :unpaid-invoices))}
[:span {:class "icon"}
[:i {:class "fa fa-envelope-o"}]]
[:span {:class "name"} "Unpaid Invoices"]]
]
[:li.menu-item
[:a {:href (bidi/path-for routes/routes :paid-invoices), :class (str "item" (active-when= @ap :paid-invoices))}
[:a {:href (bidi/path-for routes/routes :paid-invoices), :class (str "item" (active-when= ap :paid-invoices))}
[:span {:class "icon"}
[:i {:class "fa fa-envelope-o"}]]
[:span {:class "name"} "Paid Invoices"]]
@@ -305,7 +330,7 @@
[:a {:class "button is-danger is-block is-bold" :href (bidi/path-for routes/routes :new-invoice)}
[:span {:class "compose"} "New Invoice"]]]]]
[:div {:class "column messages hero is-fullheight", :id "message-feed"}
^{:key (str "active-page-" (:name @company))} [active-page @ap]]]
^{:key (str "active-page-" (:name @company))} [active-page ap]]]
[:footer {:class "footer"}
[:div {:class "container"}
[:div {:class "content has-text-centered"}
@@ -316,3 +341,13 @@
[:a {:class "icon", :href "https://github.com/dansup/bulma-templates"}
[:i {:class "fa fa-github"}]]]]]]
[:div#dz-hidden]]))
(defmethod main-layout :blank [ap]
[:div
[active-page ap]])
(defn page []
(let [ap (re-frame/subscribe [::subs/active-page])]
[:div
[main-layout @ap]]))