really improved vendor dialog code.
This commit is contained in:
@@ -10,7 +10,9 @@
|
||||
[auto-ap.views.utils :refer [active-when active-when= login-url dispatch-event appearing css-transition-group]]
|
||||
[auto-ap.views.components.vendor-dialog :refer [vendor-dialog]]
|
||||
|
||||
[auto-ap.views.components.vendor-dialog :refer [vendor-dialog]]))
|
||||
[auto-ap.views.components.vendor-dialog :refer [vendor-dialog]]
|
||||
[auto-ap.entities.vendors :as vendor]
|
||||
[auto-ap.views.components.vendor-dialog :as vendor-dialog]))
|
||||
|
||||
|
||||
(defn navbar-drop-down-contents [{:keys [id]} children ]
|
||||
@@ -85,7 +87,7 @@
|
||||
[:div.navbar-end
|
||||
[:div.navbar-item
|
||||
[:a.button.is-primary.is-outlined
|
||||
{:on-click (dispatch-event [::events/modal-status :auto-ap.views.main/user-editing-vendor {:visible? true}])}
|
||||
{:on-click (dispatch-event [::vendor-dialog/started {}])}
|
||||
[:span.icon [:i.fa.fa-plus] ] [:span "Vendor"]]]
|
||||
|
||||
|
||||
@@ -140,9 +142,7 @@
|
||||
]
|
||||
#_[footer]
|
||||
[:div
|
||||
[vendor-dialog {:vendor @(re-frame/subscribe [::subs/user-editing-vendor])
|
||||
:save-event [::events/save-vendor]
|
||||
:change-event [::events/change-nested-form-state [:user-editing-vendor]] :id :auto-ap.views.main/user-editing-vendor}]
|
||||
[vendor-dialog {}]
|
||||
bottom]
|
||||
[:div#dz-hidden]]))
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
|
||||
(defn action-modal [{:keys [title warning action-text id save-event can-submit? status-from] :or {can-submit? true}} & rest]
|
||||
(let [{:keys [visible? saving? error-message]} @(re-frame/subscribe [::subs/modal-state id status-from])]
|
||||
(println id visible?)
|
||||
(when visible?
|
||||
[:form {:id id
|
||||
:on-submit (fn [e]
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
(ns auto-ap.views.components.vendor-dialog
|
||||
(:require [re-frame.core :as re-frame]
|
||||
[auto-ap.views.utils :refer [dispatch-event horizontal-field bind-field]]
|
||||
[auto-ap.views.components.modal :refer [action-modal]]
|
||||
[auto-ap.views.components.address :refer [address-field]]
|
||||
[auto-ap.views.components.typeahead :refer [typeahead]]
|
||||
[clj-fuzzy.metrics :refer [jaccard jaro-winkler]]
|
||||
[clojure.spec.alpha :as s]
|
||||
[clojure.string :as str]
|
||||
[auto-ap.entities.vendors :as entity]
|
||||
[auto-ap.entities.contact :as contact]
|
||||
[auto-ap.subs :as subs]))
|
||||
[auto-ap.views.utils :refer [dispatch-event horizontal-field bind-field with-user]]
|
||||
[auto-ap.views.components.modal :refer [action-modal]]
|
||||
[auto-ap.views.components.address :refer [address-field]]
|
||||
[auto-ap.views.components.typeahead :refer [typeahead-entity]]
|
||||
[auto-ap.events :as events]
|
||||
[clj-fuzzy.metrics :refer [jaccard jaro-winkler]]
|
||||
[clojure.spec.alpha :as s]
|
||||
[clojure.string :as str]
|
||||
[auto-ap.entities.vendors :as entity]
|
||||
[auto-ap.entities.contact :as contact]
|
||||
[auto-ap.subs :as subs]
|
||||
[auto-ap.forms :as forms]))
|
||||
(defn ngrams [text len]
|
||||
(mapv #(.toLowerCase (str/join "" %))
|
||||
(partition len 1 text)))
|
||||
@@ -24,29 +26,70 @@
|
||||
(fn [v] (mapv
|
||||
(fn [n] [v (jaro-winkler text n ) n text])
|
||||
(ngrams v (count text)))))
|
||||
#_(sort-by second)
|
||||
(filter #(> (second %) 0.9))
|
||||
#_(map (fn [x] (doto x println)))
|
||||
(map first)
|
||||
first))))
|
||||
|
||||
(defn vendor-dialog [{:keys [vendor save-event change-event id] {:keys [name]} :vendor}]
|
||||
(re-frame/reg-event-fx
|
||||
::started
|
||||
(fn [{:keys [db]} [_ vendor]]
|
||||
{:db (-> db (forms/start-form ::vendor-form (-> vendor
|
||||
(update :hidden #(if (nil? %)
|
||||
false
|
||||
%))
|
||||
(update :default-account
|
||||
(fn [da]
|
||||
@(re-frame/subscribe [::subs/account (:id da)]))))))
|
||||
|
||||
:dispatch [::events/modal-status ::dialog {:visible? true}]}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::save-complete
|
||||
[(forms/triggers-stop ::vendor-form)]
|
||||
(fn [{:keys [db]} [_ {vendor :upsert-vendor} ]]
|
||||
{:dispatch [::events/modal-completed ::dialog ]
|
||||
:db (-> db
|
||||
(assoc-in [:vendors (:id vendor)] vendor))}))
|
||||
|
||||
(re-frame/reg-event-fx
|
||||
::save
|
||||
[with-user (forms/triggers-loading ::vendor-form) (forms/in-form ::vendor-form)]
|
||||
(fn [{:keys [user] {:keys [data]} :db} _]
|
||||
(when (s/valid? ::entity/vendor data)
|
||||
{ :graphql
|
||||
{:token user
|
||||
:query-obj {:venia/operation {:operation/type :mutation
|
||||
:operation/name "UpsertVendor"}
|
||||
:venia/queries [{:query/data [:upsert-vendor
|
||||
{:vendor (-> data
|
||||
(assoc :default-account-id (:id (:default-account data)))
|
||||
(dissoc :default-account))}
|
||||
events/vendor-query]}]}
|
||||
:on-success [::save-complete]
|
||||
:on-error [::forms/save-error ::vendor-form]}})))
|
||||
|
||||
(defn vendor-dialog [{:keys [save-event] {:keys [name]} :vendor}]
|
||||
|
||||
(let [clients-by-id @(re-frame/subscribe [::subs/clients-by-id])
|
||||
all-vendors @(re-frame/subscribe [::subs/vendors])
|
||||
chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts])]
|
||||
[action-modal {:id id
|
||||
:title [:span (if (:id vendor)
|
||||
(str "Edit " (or name "<vendor>"))
|
||||
(str "Add " (or name "<new vendor>")))
|
||||
(when (:error vendor)
|
||||
chooseable-expense-accounts @(re-frame/subscribe [::subs/chooseable-expense-accounts])
|
||||
{:keys [data error ] :as f} @(re-frame/subscribe [::forms/form ::vendor-form])
|
||||
change-event [::forms/change ::vendor-form]]
|
||||
[action-modal {:id ::dialog
|
||||
:title [:span (if (:id data)
|
||||
(str "Edit " (or (:name data) "<vendor>"))
|
||||
(str "Add " (or (:name data) "<new vendor>")))
|
||||
(when error
|
||||
[:span.icon.has-text-danger
|
||||
[:i.fa.fa-exclamation-triangle]])]
|
||||
:warning (when-let [vendor (partial-matches-vendor? vendor all-vendors)]
|
||||
(str "Are you sure you don't mean " vendor "?"))
|
||||
:status-from ::vendor-form
|
||||
:warning
|
||||
(when-let [vendor (partial-matches-vendor? data all-vendors)]
|
||||
(str "Are you sure you don't mean " vendor "?"))
|
||||
:action-text "Save"
|
||||
:save-event save-event
|
||||
:can-submit? (s/valid? ::entity/vendor vendor)}
|
||||
:save-event [::save]
|
||||
:can-submit? (s/valid? ::entity/vendor data)}
|
||||
|
||||
[horizontal-field
|
||||
[:label.label "Name"]
|
||||
@@ -57,7 +100,7 @@
|
||||
:field :name
|
||||
:spec ::entity/name
|
||||
:event change-event
|
||||
:subscription vendor}]]]]
|
||||
:subscription data}]]]]
|
||||
|
||||
[horizontal-field
|
||||
[:label.label "Print Checks As"]
|
||||
@@ -67,7 +110,18 @@
|
||||
:field :print-as
|
||||
:spec ::entity/print-as
|
||||
:event change-event
|
||||
:subscription vendor}]]]]
|
||||
:subscription data}]]]]
|
||||
|
||||
[horizontal-field
|
||||
[:label.label "Terms"]
|
||||
[:div.control
|
||||
[bind-field
|
||||
[:input.input {:type "number"
|
||||
:step "1"
|
||||
:field :terms
|
||||
:spec ::entity/terms
|
||||
:event change-event
|
||||
:subscription data}]]]]
|
||||
|
||||
[horizontal-field
|
||||
[:label.label "Hidden"]
|
||||
@@ -77,26 +131,24 @@
|
||||
:field :hidden
|
||||
:spec ::entity/hidden
|
||||
:event change-event
|
||||
:subscription vendor}]]]]
|
||||
:subscription data}]]]]
|
||||
|
||||
[:h2.subtitle "Expense Accounts"]
|
||||
[horizontal-field
|
||||
[:label.label "Default"]
|
||||
|
||||
[bind-field
|
||||
[typeahead {:matches (map (fn [x] [(:id x) (str (:numeric-code x) " - " (:name x))]) chooseable-expense-accounts)
|
||||
:type "typeahead"
|
||||
:field [:default-account-id]
|
||||
:event change-event
|
||||
:subscription vendor}]]]
|
||||
|
||||
[typeahead-entity {:matches chooseable-expense-accounts
|
||||
:match->text (fn [x ] (str (:numeric-code x) " - " (:name x)))
|
||||
:type "typeahead-entity"
|
||||
:field [:default-account]
|
||||
:event change-event
|
||||
:subscription data}]]]
|
||||
|
||||
[:h2.subtitle "Address"]
|
||||
[address-field {:field [:address]
|
||||
:event change-event
|
||||
:subscription vendor}]
|
||||
|
||||
|
||||
|
||||
:subscription data}]
|
||||
[:h2.subtitle "Contact"]
|
||||
[horizontal-field
|
||||
[:label.label "Primary"]
|
||||
@@ -106,7 +158,7 @@
|
||||
:field [:primary-contact :name]
|
||||
:spec ::contact/name
|
||||
:event change-event
|
||||
:subscription vendor}]]
|
||||
:subscription data}]]
|
||||
[:span.icon.is-small.is-left
|
||||
[:i.fa.fa-user]]]
|
||||
|
||||
@@ -118,7 +170,7 @@
|
||||
:field [:primary-contact :email]
|
||||
:spec ::contact/email
|
||||
:event change-event
|
||||
:subscription vendor}]]]
|
||||
:subscription data}]]]
|
||||
|
||||
[:div.control.has-icons-left
|
||||
[bind-field
|
||||
@@ -126,7 +178,7 @@
|
||||
:field [:primary-contact :phone]
|
||||
:spec ::contact/phone
|
||||
:event change-event
|
||||
:subscription vendor}]]
|
||||
:subscription data}]]
|
||||
[:span.icon.is-small.is-left
|
||||
[:i.fa.fa-phone]]]]
|
||||
[horizontal-field
|
||||
@@ -137,7 +189,7 @@
|
||||
:field [:secondary-contact :name]
|
||||
:spec ::contact/name
|
||||
:event change-event
|
||||
:subscription vendor}]]
|
||||
:subscription data}]]
|
||||
[:span.icon.is-small.is-left
|
||||
[:i.fa.fa-user]]]
|
||||
[:div.control.has-icons-left
|
||||
@@ -148,14 +200,14 @@
|
||||
:field [:secondary-contact :email]
|
||||
:spec ::contact/email
|
||||
:event change-event
|
||||
:subscription vendor}]]]
|
||||
:subscription data}]]]
|
||||
[:div.control.has-icons-left
|
||||
[bind-field
|
||||
[:input.input {:type "phone"
|
||||
:field [:secondary-contact :phone]
|
||||
:spec ::contact/phone
|
||||
:event change-event
|
||||
:subscription vendor}]]
|
||||
:subscription data}]]
|
||||
[:span.icon.is-small.is-left
|
||||
[:i.fa.fa-phone]]]]
|
||||
[horizontal-field
|
||||
@@ -169,7 +221,7 @@
|
||||
:field :invoice-reminder-schedule
|
||||
:spec ::entity/invoice-reminder-schedule
|
||||
:event change-event
|
||||
:subscription vendor}]]
|
||||
:subscription data}]]
|
||||
" Send weekly"]
|
||||
|
||||
[:label.radio
|
||||
@@ -180,6 +232,6 @@
|
||||
:field :invoice-reminder-schedule
|
||||
:spec ::entity/invoice-reminder-schedule
|
||||
:event change-event
|
||||
:subscription vendor}]]
|
||||
:subscription data}]]
|
||||
" Never"]]]
|
||||
(when (:saving? vendor) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])]))
|
||||
(when (:saving? data) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])]))
|
||||
|
||||
Reference in New Issue
Block a user