141 lines
5.4 KiB
Clojure
141 lines
5.4 KiB
Clojure
(ns auto-ap.views.utils
|
|
(:require [re-frame.core :as re-frame]
|
|
[clojure.spec.alpha :as s]
|
|
[cljs-time.coerce :as c]
|
|
[cljs-time.core :as time]
|
|
[auto-ap.events :as events]
|
|
[auto-ap.subs :as subs]
|
|
[cljs-time.format :as format]))
|
|
|
|
(defn active-when= [active-page candidate]
|
|
(when (= active-page candidate) " active"))
|
|
|
|
(def login-url
|
|
(let [client-id "264081895820-0nndcfo3pbtqf30sro82vgq5r27h8736.apps.googleusercontent.com"
|
|
redirect-uri (js/encodeURI (str (.-origin (.-location js/window)) "/api/oauth"))]
|
|
(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 dispatch-value-change [event]
|
|
(fn [e]
|
|
(.preventDefault e)
|
|
(re-frame/dispatch (conj event (.. e -target -value)))))
|
|
|
|
(defn delayed-dispatch [e]
|
|
(fn [x]
|
|
(js/setTimeout #(re-frame/dispatch e) 150)
|
|
false))
|
|
|
|
(defn dispatch-event [event]
|
|
(fn [e]
|
|
(when (.-stopPropagation e)
|
|
(.stopPropagation e)
|
|
(.preventDefault e))
|
|
(re-frame/dispatch-sync event)))
|
|
|
|
(def pretty-long (format/formatter "MM/dd/yyyy HH:mm:ss"))
|
|
(def pretty (format/formatter "MM/dd/yyyy"))
|
|
(def standard (format/formatter "yyyy-MM-MM"))
|
|
|
|
(defn date->str
|
|
([d] (date->str d pretty))
|
|
([d format]
|
|
(when d
|
|
(format/unparse format d))))
|
|
|
|
(defn date-time->str [d]
|
|
(when d
|
|
(format/unparse pretty-long d)))
|
|
|
|
(defn str->date [d f]
|
|
(when d
|
|
(format/parse f d)))
|
|
|
|
(defmulti do-bind (fn [a {:keys [type] :as x}]
|
|
type))
|
|
|
|
(defn with-keys [children]
|
|
(map-indexed (fn [i c] ^{:key i} c) children))
|
|
|
|
(defmethod do-bind "select" [dom {:keys [field subscription event class value spec] :as keys} & rest]
|
|
(let [field (if (keyword? field) [field] field)
|
|
event (if (keyword? event) [event] event)
|
|
keys (assoc keys
|
|
:on-change (dispatch-value-change (conj event field))
|
|
|
|
:value (get-in subscription field)
|
|
:class (str class
|
|
(when (and spec (not (s/valid? spec (get-in subscription field))))
|
|
" is-danger")))
|
|
keys (dissoc keys :field :subscription :event :spec)]
|
|
(into [dom keys] (with-keys rest))))
|
|
|
|
|
|
(defmethod do-bind "radio" [dom {:keys [field subscription event class value spec] :as keys} & rest]
|
|
(let [field (if (keyword? field) [field] field)
|
|
event (if (keyword? event) [event] event)
|
|
keys (assoc keys
|
|
:on-change (dispatch-value-change (conj event field))
|
|
:checked (= (get-in subscription field) value)
|
|
:class (str class
|
|
(when (and spec (not (s/valid? spec (get-in subscription field ))))
|
|
" is-danger")))
|
|
keys (dissoc keys :field :subscription :event :spec)]
|
|
(into [dom keys] (with-keys rest))))
|
|
|
|
(defmethod do-bind "typeahead" [dom {:keys [field text-field event subscription class spec] :as keys} & rest]
|
|
(let [field (if (keyword? field) [field] field)
|
|
event (if (keyword? event) [event] event)
|
|
keys (assoc keys
|
|
:on-change (fn [selected text-description text-value]
|
|
(re-frame/dispatch (conj (conj event field) selected))
|
|
(when text-field
|
|
(re-frame/dispatch (conj (conj event text-field) text-value))))
|
|
:value (get-in subscription field)
|
|
:class (str class
|
|
(when (and spec (not (s/valid? spec (get-in subscription field))))
|
|
" is-danger")))
|
|
keys (dissoc keys :field :subscription :event :spec)]
|
|
(into [dom keys] (with-keys rest))))
|
|
|
|
(defmethod do-bind "date" [dom {:keys [field event subscription class spec] :as keys} & rest]
|
|
(let [field (if (keyword? field) [field] field)
|
|
event (if (keyword? event) [event] event)
|
|
keys (assoc keys
|
|
:on-change (dispatch-value-change (conj event field))
|
|
:value (doto (get-in subscription field) println)
|
|
:class (str class
|
|
(when (and spec (not (s/valid? spec (get-in subscription field))))
|
|
" is-danger")))
|
|
keys (dissoc keys :field :subscription :event :spec)]
|
|
(into [dom keys] (with-keys rest))))
|
|
|
|
(defmethod do-bind :default [dom {:keys [field event subscription class spec] :as keys} & rest]
|
|
(let [field (if (keyword? field) [field] field)
|
|
event (if (keyword? event) [event] event)
|
|
keys (assoc keys
|
|
:on-change (dispatch-value-change (conj event field))
|
|
:value (get-in subscription field)
|
|
:class (str class
|
|
(when (and spec (not (s/valid? spec (get-in subscription field))))
|
|
" is-danger")))
|
|
keys (dissoc keys :field :subscription :event :spec)]
|
|
(into [dom keys] (with-keys rest))))
|
|
|
|
(defn bind-field [all]
|
|
(apply do-bind all))
|
|
|
|
|
|
|
|
(defn horizontal-field [label & controls]
|
|
[:div.field.is-horizontal
|
|
[:div.field-label
|
|
label
|
|
]
|
|
(into
|
|
[:div.field-body]
|
|
|
|
(with-keys (map (fn [x] [:div.field x]) controls)))])
|
|
|
|
|
|
|