Skip to content

Instantly share code, notes, and snippets.

@micha
Last active May 27, 2022 03:04
Show Gist options
  • Save micha/032367265649dee391b09f6cd456d3ea to your computer and use it in GitHub Desktop.
Save micha/032367265649dee391b09f6cd456d3ea to your computer and use it in GitHub Desktop.
(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]))
(def schema
{:network (.get goog.net.cookies "network")
:email nil
:password nil})
(defn logged-in
[current-login]
(fn [form-machine]
(.set goog.net.cookies "network" @(:network (:data form-machine)))
(workflow/reset form-machine)
(reset! current-login schema)))
(defelem index
[attr kids]
((app/intercept
(let [current-login (cell {:network (.get goog.net.cookies "network") :email nil :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