Created
December 5, 2016 00:45
-
-
Save micha/511a30686a9545aa740ed841480016e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns app.ui.login | |
(:require [goog.net.cookies :as cks] | |
[app.rpc :as rpc] | |
[ui.form :as form] | |
[ui.button :as button] | |
[workflow.form :as workflow] | |
[ui.modal :as modal] | |
[ui.app :as app] | |
[cemerick.url :refer [url]])) | |
(defn organization [] | |
(.get goog.net.cookies "organization")) | |
(defn email [] | |
(if-let [e (.get goog.net.cookies "email")] | |
(js/decodeURIComponent e))) | |
;; parse query params, set cookie, refresh | |
(defn login-cookies | |
[email organization] | |
(let [cookies (->> [[:email email] [:network organization]] | |
(filter second))] | |
(and (not-empty cookies) cookies))) | |
;; if there are login query params, put in in cookies and reload page | |
(let [q (:query (url (.-href js/location))) | |
cookies (goog.net.Cookies. js/document) | |
kvs (->> ["email" "organization"] | |
(map (fn [k] [k (get q k)])) | |
(filter second))] | |
(when (not-empty kvs) | |
(doseq [[k v] kvs] | |
(.set cookies k v)) | |
(aset js/location "href" "/"))) | |
(def schema | |
{:network (organization) | |
:email (email) | |
:password nil}) | |
(defn logged-in | |
[current-login] | |
(fn [form-machine] | |
(.set goog.net.cookies "organization" @(:network (:data form-machine))) | |
(workflow/reset form-machine) | |
(reset! current-login (assoc schema :network (organization))))) | |
(defelem index | |
[attr kids] | |
((app/intercept | |
(let [current-login (cell {:network (organization) | |
:email (email) | |
:password nil}) | |
forgot (cell false) | |
!forgot (cell= (not forgot)) | |
titles {true "Recover Password" false "Log In"} | |
link-text (cell= (if forgot "Cancel" "I forgot my password"))] | |
(form/with-form [{:keys [data error state exception loading] :as form-machine} | |
(workflow/form-machine | |
:current current-login | |
:action #((if @forgot rpc/forgot rpc/login) %) | |
:schema schema | |
:success (logged-in current-login))] | |
(modal/modal-dialog | |
:size :md | |
(modal/modal-header | |
(modal/modal-title | |
(span :text (cell= (titles forgot))))) | |
(form | |
:submit #(workflow/submit form-machine) | |
(modal/modal-body | |
(form/with-field :network | |
(form/validation-field | |
(label "Organization") | |
(form/text-input | |
:focus-select !forgot) | |
(form/validation-message))) | |
(form/with-field :email | |
(form/validation-field | |
(label "Email Address") | |
(form/text-input :focus-select forgot) | |
(form/validation-message))) | |
(form/with-field :password | |
(form/validation-field | |
:toggle !forgot | |
(label "Password") | |
(form/password-input) | |
(form/validation-message)))) | |
(modal/modal-footer | |
:class "text-left" | |
(form/validation-alert) | |
(form/submit-primary "Submit") | |
(button/link | |
:click #(swap! forgot not) | |
:text (cell= (titles (not forgot)))))))))) | |
attr kids)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment