a lot of de-duplicating.
This commit is contained in:
@@ -1,20 +1,26 @@
|
|||||||
(ns auto-ap.entities.vendors
|
(ns auto-ap.entities.vendors
|
||||||
(:require [clojure.spec.alpha :as s]))
|
(:require [clojure.spec.alpha :as s]
|
||||||
|
[clojure.string :as str]))
|
||||||
|
|
||||||
(def email-regex #"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$")
|
(def email-regex #"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$")
|
||||||
(s/def ::id int)
|
(s/def ::id int)
|
||||||
(s/def ::name (s/nilable string?))
|
(s/def ::identifier (s/nilable string?))
|
||||||
|
(s/def ::required-identifier (s/and string?
|
||||||
|
#(not (str/blank? %))))
|
||||||
|
|
||||||
|
(s/def ::name ::required-identifier)
|
||||||
|
|
||||||
(s/def ::email (s/nilable (s/and string? (s/or :is-email #(re-matches email-regex %)
|
(s/def ::email (s/nilable (s/and string? (s/or :is-email #(re-matches email-regex %)
|
||||||
:is-empty #(= % "")))))
|
:is-empty #(= % "")))))
|
||||||
(s/def ::phone (s/nilable string?))
|
(s/def ::phone (s/nilable string?))
|
||||||
|
|
||||||
(s/def ::data (s/nilable string?))
|
(s/def ::data (s/nilable string?))
|
||||||
(s/def ::invoice-reminder-schedule (s/nilable string?))
|
(s/def ::invoice-reminder-schedule (s/nilable #{"Weekly" "Never" nil}))
|
||||||
(s/def ::primary-contact ::name)
|
(s/def ::primary-contact ::identifier)
|
||||||
(s/def ::primary-email ::email)
|
(s/def ::primary-email ::email)
|
||||||
(s/def ::primary-phone ::phone)
|
(s/def ::primary-phone ::phone)
|
||||||
|
|
||||||
(s/def ::secondary-contact ::name)
|
(s/def ::secondary-contact ::identifier)
|
||||||
(s/def ::secondary-email ::email)
|
(s/def ::secondary-email ::email)
|
||||||
(s/def ::secondary-phone ::phone)
|
(s/def ::secondary-phone ::phone)
|
||||||
|
|
||||||
|
|||||||
@@ -33,9 +33,54 @@
|
|||||||
[:td (::entity/primary-email v)]
|
[:td (::entity/primary-email v)]
|
||||||
[:td (::entity/invoice-reminder-schedule v)]])]]))
|
[:td (::entity/invoice-reminder-schedule v)]])]]))
|
||||||
|
|
||||||
|
(defmulti do-bind (fn [_ {:keys [type]}]
|
||||||
|
|
||||||
|
type))
|
||||||
|
|
||||||
|
(defn danger-for [[dom {:keys [field subscription class] :as keys} & rest]]
|
||||||
|
(let [keys (assoc keys :class (str class
|
||||||
|
(when (not (s/valid? field (field subscription)))
|
||||||
|
" is-danger")))
|
||||||
|
keys (dissoc keys :field :subscription)]
|
||||||
|
(vec (concat [dom keys] rest))))
|
||||||
|
|
||||||
|
|
||||||
|
(defmethod do-bind "radio" [dom {:keys [field subscription class value] :as keys} & rest]
|
||||||
|
(let [keys (assoc keys
|
||||||
|
:on-change (dispatch-value-change [::events/change [field]])
|
||||||
|
:checked (= (field subscription) value)
|
||||||
|
:class (str class
|
||||||
|
(when (not (s/valid? field (field subscription)))
|
||||||
|
" is-danger")))
|
||||||
|
keys (dissoc keys :field :subscription)]
|
||||||
|
(vec (concat [dom keys] rest))))
|
||||||
|
|
||||||
|
|
||||||
|
(defmethod do-bind :default [dom {:keys [field subscription class] :as keys} & rest]
|
||||||
|
(let [keys (assoc keys
|
||||||
|
:on-change (dispatch-value-change [::events/change [field]])
|
||||||
|
:value (field subscription)
|
||||||
|
:class (str class
|
||||||
|
(when (not (s/valid? field (field subscription)))
|
||||||
|
" is-danger")))
|
||||||
|
keys (dissoc keys :field :subscription)]
|
||||||
|
(vec (concat [dom 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
|
||||||
|
]
|
||||||
|
(map (fn [c] [:div.field c]) controls))])
|
||||||
|
|
||||||
(defn edit-dialog []
|
(defn edit-dialog []
|
||||||
(let [editing-vendor (:vendor @(re-frame/subscribe [::subs/admin]))]
|
(let [editing-vendor (:vendor @(re-frame/subscribe [::subs/admin]))]
|
||||||
(println editing-vendor)
|
|
||||||
[:div.modal.is-active
|
[:div.modal.is-active
|
||||||
[:div.modal-background {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]
|
[:div.modal-background {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]
|
||||||
|
|
||||||
@@ -47,160 +92,140 @@
|
|||||||
(str "Add " (or (::entity/name editing-vendor) "<new vendor>")))]
|
(str "Add " (or (::entity/name editing-vendor) "<new vendor>")))]
|
||||||
[:button.delete {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]]
|
[:button.delete {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]]
|
||||||
[:section.modal-card-body
|
[:section.modal-card-body
|
||||||
[:div.field.is-horizontal
|
[horizontal-field
|
||||||
[:div.field-label
|
[:label.label "Name"]
|
||||||
[:label.label "Name"]]
|
[:div.control
|
||||||
[:div.field-body
|
[bind-field
|
||||||
[:div.field
|
[:input.input {:type "text"
|
||||||
[:div.control
|
:field ::entity/name
|
||||||
[:input.input {:type "text" :value (::entity/name editing-vendor)
|
:subscription editing-vendor}]]]]
|
||||||
:class (str/join " " (when (not (s/valid? ::entity/name (::entity/name editing-vendor)))
|
|
||||||
"is-danger"))
|
[horizontal-field
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/name]])}]]]]]
|
[:label.label "Code"]
|
||||||
[:div.field.is-horizontal
|
[:div.control
|
||||||
[:div.field-label
|
|
||||||
[:label.label "Code"]]
|
[bind-field
|
||||||
[:div.field-body
|
[:input.input.is-expanded {:type "text"
|
||||||
[:div.field
|
:field ::entity/code
|
||||||
[:div.control
|
:subscription editing-vendor}]]
|
||||||
[:input.input.is-expanded {:type "text" :value (::entity/code editing-vendor)
|
[:p.help "The vendor code is used for invoice parsing. Only one vendor at a time can use a code"]]]
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/code]])}]
|
|
||||||
[:p.help "The vendor code is used for invoice parsing. Only one vendor at a time can use a code"]]]]
|
|
||||||
]
|
|
||||||
|
|
||||||
[:h2.subtitle "Address"]
|
[:h2.subtitle "Address"]
|
||||||
[:div.field.is-horizontal
|
[horizontal-field
|
||||||
[:div.field-label]
|
nil
|
||||||
[:div.field-body
|
[:div.control
|
||||||
[:div.field
|
|
||||||
[:div.control
|
|
||||||
[:p.help "Address"]
|
[:p.help "Address"]
|
||||||
[:input.input.is-expanded {:type "text"
|
[bind-field
|
||||||
:placeholder "1700 Pennsylvania Ave"
|
[:input.input.is-expanded {:type "text"
|
||||||
:value (::entity/address1 editing-vendor)
|
:placeholder "1700 Pennsylvania Ave"
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/address1]])}]]]]]
|
:field ::entity/address1
|
||||||
[:div.field.is-horizontal
|
:subscription editing-vendor}]]]]
|
||||||
[:div.field-label]
|
|
||||||
[:div.field-body
|
[horizontal-field
|
||||||
[:div.field
|
nil
|
||||||
[:div.control
|
[:div.control
|
||||||
|
[bind-field
|
||||||
[:input.input.is-expanded {:type "text"
|
[:input.input.is-expanded {:type "text"
|
||||||
:placeholder "Suite 400"
|
:placeholder "Suite 400"
|
||||||
:value (::entity/address2 editing-vendor)
|
:field ::entity/address2
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/address2]])}]]]]]
|
:subscription editing-vendor}]]]]
|
||||||
[:div.field.is-horizontal
|
|
||||||
[:div.field-label]
|
[horizontal-field
|
||||||
[:div.field-body
|
nil
|
||||||
[:div.field
|
[:div.control
|
||||||
[:p.help "City"]
|
[:p.help "City"]
|
||||||
[:div.control
|
[bind-field
|
||||||
[:input.input.is-expanded {:type "text"
|
[:input.input.is-expanded {:type "text"
|
||||||
:value (::entity/city editing-vendor)
|
:placeholder "Cupertino"
|
||||||
:placeholder "Cupertino"
|
:field ::entity/city
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/city]])}]]]
|
:subscription editing-vendor}]]]
|
||||||
[:div.field
|
[:div.control
|
||||||
[:div.control
|
[:p.help "State"]
|
||||||
|
[bind-field
|
||||||
[:p.help "State"]
|
[:input.input {:type "text"
|
||||||
[:input.input {:type "email"
|
:placeholder "CA"
|
||||||
:value (::entity/state editing-vendor)
|
:field ::entity/state
|
||||||
:placeholder "CA"
|
:subscription editing-vendor}]]]
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/state]])}]
|
[:div.control
|
||||||
]]
|
[:p.help "Zip"]
|
||||||
|
[bind-field
|
||||||
[:div.field
|
[:input.input {:type "text"
|
||||||
[:div.control
|
:field ::entity/zip
|
||||||
[:p.help "Zip"]
|
:subscription editing-vendor
|
||||||
[:input.input {:type "phone"
|
:placeholder "95014"}]]]]
|
||||||
:value (::entity/zip editing-vendor)
|
|
||||||
:placeholder "95014"
|
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/zip]])}]
|
|
||||||
]]]]
|
|
||||||
[:h2.subtitle "Contact"]
|
[:h2.subtitle "Contact"]
|
||||||
|
[horizontal-field
|
||||||
|
[:label.label "Primary"]
|
||||||
|
[:div.control.has-icons-left
|
||||||
|
[bind-field
|
||||||
|
[:input.input.is-expanded {:type "text"
|
||||||
|
:field ::entity/primary-contact
|
||||||
|
:subscription editing-vendor}]]
|
||||||
|
[:span.icon.is-small.is-left
|
||||||
|
[:i.fa.fa-user]]]
|
||||||
|
|
||||||
[:div.field.is-horizontal
|
[:div.control.has-icons-left
|
||||||
[:div.field-label
|
|
||||||
[:label.label "Primary"]]
|
|
||||||
[:div.field-body
|
|
||||||
[:div.field
|
|
||||||
[:div.control.has-icons-left
|
|
||||||
[:input.input.is-expanded {:type "text"
|
|
||||||
:value (::entity/primary-contact editing-vendor)
|
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/primary-contact]])}]
|
|
||||||
[:span.icon.is-small.is-left
|
|
||||||
[:i.fa.fa-user]]]]
|
|
||||||
[:div.field
|
|
||||||
[:div.control.has-icons-left
|
|
||||||
[:span.icon.is-small.is-left
|
[:span.icon.is-small.is-left
|
||||||
[:i.fa.fa-envelope]]
|
[:i.fa.fa-envelope]]
|
||||||
[:input.input {:type "email"
|
[bind-field
|
||||||
:value (::entity/primary-email editing-vendor)
|
[:input.input {:type "email"
|
||||||
:class (str/join " " [(when (::entity/primary-email (invalid-fields ::entity/vendor editing-vendor ))
|
:field ::entity/primary-email
|
||||||
"is-danger")])
|
:subscription editing-vendor}]]]
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/primary-email]])}]]]
|
|
||||||
|
|
||||||
[:div.field
|
[:div.control.has-icons-left
|
||||||
[:div.control.has-icons-left
|
[bind-field
|
||||||
[:input.input {:type "phone"
|
[:input.input {:type "phone"
|
||||||
:value (::entity/primary-phone editing-vendor)
|
:field ::entity/primary-phone
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/primary-phone]])}]
|
:subscription editing-vendor}]]
|
||||||
[:span.icon.is-small.is-left
|
[:span.icon.is-small.is-left
|
||||||
[:i.fa.fa-phone]]]]]]
|
[:i.fa.fa-phone]]]]
|
||||||
[:div.field.is-horizontal
|
|
||||||
[:div.field-label
|
[horizontal-field
|
||||||
[:label.label "Secondary"]]
|
[:label.label "Secondary"]
|
||||||
[:div.field-body
|
[:div.control.has-icons-left
|
||||||
[:div.field
|
[bind-field
|
||||||
[:div.control.has-icons-left
|
[:input.input.is-expanded {:type "text"
|
||||||
[:input.input.is-expanded {:type "text"
|
:field ::entity/secondary-contact
|
||||||
:value (::entity/secondary-contact editing-vendor)
|
:subscription editing-vendor}]]
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/secondary-contact]])}]
|
|
||||||
[:span.icon.is-small.is-left
|
[:span.icon.is-small.is-left
|
||||||
[:i.fa.fa-user]]]]
|
[:i.fa.fa-user]]]
|
||||||
[:div.field
|
[:div.control.has-icons-left
|
||||||
[:div.control.has-icons-left
|
|
||||||
[:span.icon.is-small.is-left
|
[:span.icon.is-small.is-left
|
||||||
[:i.fa.fa-envelope]]
|
[:i.fa.fa-envelope]]
|
||||||
[:input.input {:type "email"
|
[bind-field
|
||||||
:value (::entity/secondary-email editing-vendor)
|
[:input.input {:type "email"
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/secondary-email]])}]]]
|
:field ::entity/secondary-email
|
||||||
|
:subscription editing-vendor}]]]
|
||||||
[:div.field
|
[:div.control.has-icons-left
|
||||||
[:div.control.has-icons-left
|
[bind-field
|
||||||
[:input.input {:type "phone"
|
[:input.input {:type "phone"
|
||||||
:value (::entity/secondary-phone editing-vendor)
|
:field ::entity/secondary-phone
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/secondary-phone]])}]
|
:subscription editing-vendor}]]
|
||||||
[:span.icon.is-small.is-left
|
[:span.icon.is-small.is-left
|
||||||
[:i.fa.fa-phone]]]]]]
|
[:i.fa.fa-phone]]]]
|
||||||
|
|
||||||
[:div.field.is-horizontal
|
[horizontal-field
|
||||||
[:div.field-label
|
[:label.label "Invoice Reminders"]
|
||||||
[:label.label "Invoice Reminders"]]
|
[:div.control
|
||||||
[:div.field-body
|
|
||||||
[:div.field
|
|
||||||
[:div.control
|
|
||||||
[:label.radio
|
[:label.radio
|
||||||
[:input {:type "radio"
|
[bind-field
|
||||||
:name "schedule"
|
[:input {:type "radio"
|
||||||
:value "Weekly"
|
:name "schedule"
|
||||||
:checked (if (= "Weekly" (::entity/invoice-reminder-schedule editing-vendor))
|
:value "Weekly"
|
||||||
"checked"
|
:field ::entity/invoice-reminder-schedule
|
||||||
"")
|
:subscription editing-vendor}]]
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/invoice-reminder-schedule]])}]
|
|
||||||
|
|
||||||
" Send weekly"]
|
" Send weekly"]
|
||||||
[:label.radio
|
[:label.radio
|
||||||
[:input {:type "radio"
|
[bind-field
|
||||||
:name "schedule"
|
[:input {:type "radio"
|
||||||
:value "Never"
|
:name "schedule"
|
||||||
:checked (if (= "Never" (::entity/invoice-reminder-schedule editing-vendor))
|
:value "Never"
|
||||||
"checked"
|
:field ::entity/invoice-reminder-schedule
|
||||||
"")
|
:subscription editing-vendor}]]
|
||||||
:on-change (dispatch-value-change [::events/change [::entity/invoice-reminder-schedule]])}]
|
" Never"]]]
|
||||||
" Never"]]]]
|
|
||||||
]
|
|
||||||
|
|
||||||
(when (:saving? editing-vendor) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])]
|
(when (:saving? editing-vendor) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user