Files
integreat/src/cljs/auto_ap/views/pages/admin/vendors.cljs
Bryce Covert 5e7add409f more specs.
2018-04-09 16:27:03 -07:00

243 lines
9.2 KiB
Clojure

(ns auto-ap.views.pages.admin.vendors
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [re-frame.core :as re-frame]
[reagent.core :as reagent]
[clojure.string :as str]
[auto-ap.subs :as subs]
[auto-ap.events.admin.vendors :as events]
[auto-ap.entities.vendors :as entity]
[clojure.spec.alpha :as s]
[auto-ap.views.utils :refer [login-url dispatch-value-change dispatch-event bind-field horizontal-field]]
[cljs.reader :as edn]
[auto-ap.routes :as routes]
[bidi.bidi :as bidi]))
(defn invalid-fields [spec v]
(set (mapcat :in (::s/problems (s/explain-data spec v)))))
(defn vendors-table []
(let [vendors (re-frame/subscribe [::subs/vendors])
editing-vendor (:editing @(re-frame/subscribe [::subs/admin]))]
[:table {:class "table", :style {:width "100%"}}
[:thead
[:tr
[:th "Name"]
[:th "Email"]
[:th "Invoice Reminders"]]]
[:tbody (for [v @vendors]
^{:key (str (::entity/id v))}
[:tr {:on-click (fn [] (re-frame/dispatch [::events/edit (::entity/id v)]))
:style {"cursor" "pointer"}}
[:td (::entity/name v)]
[:td (::entity/primary-email v)]
[:td (::entity/invoice-reminder-schedule v)]])]]))
(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))))
(defn edit-dialog []
(let [editing-vendor (:vendor @(re-frame/subscribe [::subs/admin]))]
[:div.modal.is-active
[:div.modal-background {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]
[:div.modal-card
[:header.modal-card-head
[:p.modal-card-title
(if (::entity/id editing-vendor)
(str "Edit " (or (::entity/name editing-vendor) "<vendor>"))
(str "Add " (or (::entity/name editing-vendor) "<new vendor>")))]
(when (:error editing-vendor)
[:span.icon.has-text-danger
[:i.fa.fa-exclamation-triangle]])
[:button.delete {:on-click (fn [] (re-frame/dispatch [::events/edit nil]))}]]
[:section.modal-card-body
[horizontal-field
[:label.label "Name"]
[:div.control
[bind-field
[:input.input {:type "text"
:field ::entity/name
:event ::events/change
:subscription editing-vendor}]]]]
[horizontal-field
[:label.label "Code"]
[:div.control
[bind-field
[:input.input.is-expanded {:type "text"
:field ::entity/code
:event ::events/change
:subscription editing-vendor}]]
[:p.help "The vendor code is used for invoice parsing. Only one vendor at a time can use a code"]]]
[:h2.subtitle "Address"]
[horizontal-field
nil
[:div.control
[:p.help "Address"]
[bind-field
[:input.input.is-expanded {:type "text"
:placeholder "1700 Pennsylvania Ave"
:field ::entity/address1
:event ::events/change
:subscription editing-vendor}]]]]
[horizontal-field
nil
[:div.control
[bind-field
[:input.input.is-expanded {:type "text"
:placeholder "Suite 400"
:field ::entity/address2
:event ::events/change
:subscription editing-vendor}]]]]
[horizontal-field
nil
[:div.control
[:p.help "City"]
[bind-field
[:input.input.is-expanded {:type "text"
:placeholder "Cupertino"
:field ::entity/city
:event ::events/change
:subscription editing-vendor}]]]
[:div.control
[:p.help "State"]
[bind-field
[:input.input {:type "text"
:placeholder "CA"
:field ::entity/state
:event ::events/change
:subscription editing-vendor}]]]
[:div.control
[:p.help "Zip"]
[bind-field
[:input.input {:type "text"
:field ::entity/zip
:event ::events/change
:subscription editing-vendor
:placeholder "95014"}]]]]
[: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
:event ::events/change
:subscription editing-vendor}]]
[:span.icon.is-small.is-left
[:i.fa.fa-user]]]
[:div.control.has-icons-left
[:span.icon.is-small.is-left
[:i.fa.fa-envelope]]
[bind-field
[:input.input {:type "email"
:field ::entity/primary-email
:event ::events/change
:subscription editing-vendor}]]]
[:div.control.has-icons-left
[bind-field
[:input.input {:type "phone"
:field ::entity/primary-phone
:event ::events/change
:subscription editing-vendor}]]
[:span.icon.is-small.is-left
[:i.fa.fa-phone]]]]
[horizontal-field
[:label.label "Secondary"]
[:div.control.has-icons-left
[bind-field
[:input.input.is-expanded {:type "text"
:field ::entity/secondary-contact
:event ::events/change
:subscription editing-vendor}]]
[:span.icon.is-small.is-left
[:i.fa.fa-user]]]
[:div.control.has-icons-left
[:span.icon.is-small.is-left
[:i.fa.fa-envelope]]
[bind-field
[:input.input {:type "email"
:field ::entity/secondary-email
:event ::events/change
:subscription editing-vendor}]]]
[:div.control.has-icons-left
[bind-field
[:input.input {:type "phone"
:field ::entity/secondary-phone
:event ::events/change
:subscription editing-vendor}]]
[:span.icon.is-small.is-left
[:i.fa.fa-phone]]]]
[horizontal-field
[:label.label "Invoice Reminders"]
[:div.control
[:label.radio
[bind-field
[:input {:type "radio"
:name "schedule"
:value "Weekly"
:field ::entity/invoice-reminder-schedule
:event ::events/change
:subscription editing-vendor}]]
" Send weekly"]
[:label.radio
[bind-field
[:input {:type "radio"
:name "schedule"
:value "Never"
:field ::entity/invoice-reminder-schedule
:event ::events/change
:subscription editing-vendor}]]
" Never"]]]
(when (:saving? editing-vendor) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])]
[:footer.modal-card-foot
[:a.button.is-primary {:on-click (fn [] (re-frame/dispatch [::events/save]))
:disabled (when (not (s/valid? ::entity/vendor editing-vendor ))
"disabled")}
[:span "Save"]
(when (:saving? editing-vendor)
[:span.icon
[:i.fa.fa-spin.fa-spinner]])]]]]))
(defn admin-vendors-page []
[(with-meta
(fn []
[:div {:class "inbox-messages"}
[:div.hero
[:div.hero-body
[:div.container
(let [vendors (re-frame/subscribe [::subs/vendors])
editing-vendor (:vendor @(re-frame/subscribe [::subs/admin]))]
[:div
[:h1.title "Vendors"]
[vendors-table]
[:a.button.is-primary.is-large {:on-click (dispatch-event [::events/new])} "New vendor"]
(when editing-vendor
[edit-dialog]
)])]]]])
{:component-did-mount (fn []
(re-frame/dispatch [::events/mounted]))})])