diff --git a/migrator/migrations/1523724426-DOWN-add-vendor-client-relationship.sql b/migrator/migrations/1523724426-DOWN-add-vendor-client-relationship.sql new file mode 100644 index 00000000..7ccc91ab --- /dev/null +++ b/migrator/migrations/1523724426-DOWN-add-vendor-client-relationship.sql @@ -0,0 +1,2 @@ +-- 1523724426 DOWN add-vendor-client-relationship +drop table vendors_companies; diff --git a/migrator/migrations/1523724426-UP-add-vendor-client-relationship.sql b/migrator/migrations/1523724426-UP-add-vendor-client-relationship.sql new file mode 100644 index 00000000..af97c9bb --- /dev/null +++ b/migrator/migrations/1523724426-UP-add-vendor-client-relationship.sql @@ -0,0 +1,8 @@ +-- 1523724426 UP add-vendor-client-relationship +CREATE TABLE vendors_companies ( + id serial primary key, + vendor_id int references vendors(id), + company_id int references companies(id), + account_id varchar(255), + data text +); diff --git a/src/cljs/auto_ap/events/admin/vendors.cljs b/src/cljs/auto_ap/events/admin/vendors.cljs index 656c317a..7c25f362 100644 --- a/src/cljs/auto_ap/events/admin/vendors.cljs +++ b/src/cljs/auto_ap/events/admin/vendors.cljs @@ -86,6 +86,21 @@ (assoc-in db (concat [:admin :vendor] path) value))) +(re-frame/reg-event-db + ::add-relationship + (fn [db [_ ]] + (-> db + (update-in [:admin :vendor :relationships] #(conj (or % []) + {:account-number (get-in db [:admin :vendor :new-relationship-account-number]) + :company-id (get-in db [:admin :vendor :new-relationship-company])})) + (update-in [:admin :vendor] dissoc :new-relationship-account-number :new-relationship-company)))) + +(re-frame/reg-event-db + ::remove-relationship + (fn [db [_ i]] + (-> db + (update-in [:admin :vendor :relationships] #(vec (concat (subvec % 0 i) (subvec % (min (count %) (inc i))))))))) + (re-frame/reg-event-fx ::mounted (fn [{:keys [db]} _] diff --git a/src/cljs/auto_ap/views/pages/admin/vendors.cljs b/src/cljs/auto_ap/views/pages/admin/vendors.cljs index 2377af47..bef7112a 100644 --- a/src/cljs/auto_ap/views/pages/admin/vendors.cljs +++ b/src/cljs/auto_ap/views/pages/admin/vendors.cljs @@ -230,6 +230,46 @@ :subscription editing-vendor}]] " Never"]]] + + [:h2.subtitle "Clients"] + + [horizontal-field + nil + [:div.control + [:div.select.is-expanded + [bind-field + [:select {:type "select" + :field :new-relationship-company + :event ::events/change + :subscription editing-vendor + } + (for [company @(re-frame/subscribe [::subs/companies])] + [:option {:value (:id company)} (:name company)])]]]] + [:div.control + [bind-field + [:input.input {:type "text" + :field :new-relationship-account-number + :subscription editing-vendor + :event ::events/change + :placeholder "Account number"}]]] + [:div.control + [:button.button.is-primary + {:on-click (dispatch-event [::events/add-relationship])} + [:span.icon + [:i.fa.fa-plus]]]]] + [horizontal-field + nil + [:ul + (for [[i r] (map vector (range) (:relationships editing-vendor))] + ^{:key i} + [:li (:account-number r) + (:company-id r) + [:a + {:on-click (dispatch-event [::events/remove-relationship i])} + [:span.icon + [:i.fa.fa-times]]]] + )]] + (when (:saving? editing-vendor) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])] diff --git a/src/cljs/auto_ap/views/utils.cljs b/src/cljs/auto_ap/views/utils.cljs index 9de6a354..8eb5f40c 100644 --- a/src/cljs/auto_ap/views/utils.cljs +++ b/src/cljs/auto_ap/views/utils.cljs @@ -37,6 +37,15 @@ (defmulti do-bind (fn [_ {:keys [type]}] type)) +(defmethod do-bind "select" [dom {:keys [field subscription event class value spec] :as keys} & rest] + (let [keys (assoc keys + :on-change (dispatch-value-change [event [field]]) + :class (str class + (when (and spec (not (s/valid? spec (field subscription)))) + " is-danger"))) + keys (dissoc keys :field :subscription :event :spec)] + (vec (concat [dom keys] rest)))) + (defmethod do-bind "radio" [dom {:keys [field subscription event class value spec] :as keys} & rest] (let [keys (assoc keys