you can add vendors while creating.

This commit is contained in:
BC
2018-06-14 21:17:07 -07:00
parent d110755939
commit a17b6b9688
11 changed files with 179 additions and 151 deletions

View File

@@ -74,8 +74,9 @@
(re-frame/reg-event-db
::change-form
(fn [db [_ location field value]]
(println field value)
(assoc-in db (into location field) value)))
(if value
(assoc-in db (into location field) value)
(update-in db location dissoc field))))
(re-frame/reg-event-db
::modal-status

View File

@@ -2,31 +2,38 @@
(:require [reagent.core :as r]
[clojure.string :as str]))
(defn typeahead [{:keys [matches on-change field value class]}]
(defn typeahead [{:keys [matches on-change field text-field value class not-found-description
not-found-value]}]
(let [text (r/atom (or (second (first (filter #(= (first %) value) matches))) ""))
highlighted (r/atom 0)
selected (r/atom (first (first (filter #(= (first %) value) matches))))
select (fn [[id t]]
(reset! selected id)
(reset! text t)
(println [id t])
select (fn [[id text-description text-value]]
(reset! selected id)
(reset! text text-description)
(println (= :not-found id))
(when on-change
(on-change id)))]
(fn [{:keys [matches on-change field value class]}]
(if (= :not-found id)
(on-change nil text-description text-value)
(on-change id text-description (or text-value text-description)))))]
(fn [{:keys [matches on-change field text-field value class not-found-description]}]
(let [valid-matches (take 5 (for [[[id t :as match] i] (map vector matches (range))
:when (str/includes? (.toLowerCase t) (.toLowerCase @text))]
match))]
match))
valid-matches (if (and not-found-description @text)
(concat valid-matches [[:not-found (not-found-description @text) (not-found-value @text)]])
valid-matches)]
[:div.typeahead
(if @selected
[:div.input {:class class}
[:div.control
[:div.tags.has-addons
[:span.tag @text]
[:a.tag.is-delete {:on-click (fn [] (select [nil ""]))}]]]]
[:a.tag.is-delete {:on-click (fn [] (select [nil "" nil]))}]]]]
[:input.input {:type "text"
:class class
:field [:vendor]
:field field
:value @text
:on-blur (fn [e]
(cond @selected
@@ -52,13 +59,19 @@
(reset! highlighted (ffirst valid-matches))
(select [nil (.. e -target -value)]))}
])
(when (and (seq @text)
(not @selected)
(seq valid-matches))
(cond
(and (seq @text)
(not @selected)
(seq valid-matches))
[:div.typeahead-menu
[:ul
(for [[id t :as match] valid-matches]
[:li.typeahead-suggestion {:class (if (= id @highlighted)
"typeahead-highlighted")
:on-mouse-down #(do (println "MATCH" match) (select match))} t])]])]))))
:on-mouse-down #(do (select match))} t])]]
:else
nil)]))))

View File

@@ -26,7 +26,6 @@
(re-frame/reg-sub
::invoice
(fn [db id]
(println id)
(get (by :id (-> db ::invoice-page :invoices )) id)))
(re-frame/reg-sub
@@ -207,18 +206,20 @@
(re-frame/reg-event-fx
::create-invoice
(fn [{:keys [db]} _]
{:graphql
{:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "AddInvoice"}
:venia/queries [{:query/data [:add-invoice
{:invoice @(re-frame/subscribe [::new-invoice])}
[:id :total :outstanding-balance :date :invoice-number
[:company [:id :name]]
[:vendor [:id :name]]
]]}]}
:on-success [::invoice-created]}}))
(let [new-invoice @(re-frame/subscribe [::new-invoice])]
(println new-invoice)
{:graphql
{:token (-> db :user)
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "AddInvoice"}
:venia/queries [{:query/data [:add-invoice
{:invoice new-invoice}
[:id :total :outstanding-balance :date :invoice-number
[:company [:id :name]]
[:vendor [:id :name]]
]]}]}
:on-success [::invoice-created]}})))
(re-frame/reg-event-fx
::invoice-created
@@ -270,7 +271,7 @@
(update-in [::invoice-page :invoices]
(fn [is]
(println "UPDATE" updated is)
(replace-if #(= (:id %1) (:id %2)) updated is)))
(dissoc ::change-expense-accounts))})))
@@ -336,7 +337,7 @@
:field [:invoice :expense-accounts index :amount]
:event change-event
:subscription data
:value (doto (get-in data [:invoice :expense-accounts index :amount]) println)
:value (get-in data [:invoice :expense-accounts index :amount])
:max (:total data)
:step "0.01"}]]]]]]
@@ -421,8 +422,11 @@
[typeahead {:matches (map (fn [x] [(:id x) (:name x)]) @(re-frame/subscribe [::subs/vendors]))
:type "typeahead"
:field [:vendor-id]
:text-field [:vendor-name]
:not-found-description #(str "Create vendor '" % "'")
:not-found-value #(str %)
:event change-event
:spec ::invoice/vendor-id
:spec (s/nilable ::invoice/vendor-id)
:subscription data}]]]
[horizontal-field
[:label.label "Date"]

View File

@@ -68,12 +68,15 @@
keys (dissoc keys :field :subscription :event :spec)]
(into [dom keys] (with-keys rest))))
(defmethod do-bind "typeahead" [dom {:keys [field event subscription class spec] :as 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]
(re-frame/dispatch (conj (conj event field) selected)))
:on-change (fn [selected text-description text-value]
(println "HERE " 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))))