another step in cleaning up modals.

This commit is contained in:
Bryce Covert
2020-08-22 09:16:16 -07:00
parent b508d8d8b8
commit 86a117f739
4 changed files with 99 additions and 75 deletions

View File

@@ -0,0 +1,76 @@
(ns auto-ap.views.pages.admin.vendors.merge-dialog
(:require [auto-ap.forms :as forms]
[auto-ap.status :as status]
[auto-ap.subs :as subs]
[auto-ap.views.components.modal :as modal]
[auto-ap.views.components.typeahead :refer [typeahead-entity]]
[auto-ap.views.utils :refer [dispatch-event]]
[re-frame.core :as re-frame]))
(re-frame/reg-sub
::can-submit
:<- [::forms/form ::form]
(fn [{:keys [data]}]
(println data)
(and (:from data)
(:to data))))
(def merge-form (forms/vertical-form {:submit-event [::save]
:change-event [::forms/change ::form]
:can-submit [::can-submit]
:id ::form}))
(defn form []
(let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
{:keys [form-inline horizontal-field field raw-field error-notification submit-button]} merge-form]
(form-inline {}
[:<>
(field "Form Vendor (will be deleted)"
[typeahead-entity {:matches @(re-frame/subscribe [::subs/vendors])
:type "typeahead-entity"
:auto-focus true
:match->text :name
:field [:from]}])
(field "To Vendor"
[typeahead-entity {:matches @(re-frame/subscribe [::subs/vendors])
:type "typeahead-entity"
:match->text :name
:field [:to]}])])))
(re-frame/reg-event-fx
::show
(fn [{:keys [db]} _]
{:dispatch [::modal/modal-requested {:title "Merge Vendors"
:body [form]
:confirm {:value "Merge"
:status-from [::status/single ::form]
:class "is-primary"
:on-click (dispatch-event [::save])
:can-submit [::can-submit]
:close-event [::status/completed ::form]}}]
:db (forms/start-form db ::form {})}
))
(re-frame/reg-event-fx
::complete
(fn [{:keys [db]} _]
{:db (forms/stop-form db ::form)
:dispatch [::modal/modal-closed ]}))
(re-frame/reg-event-fx
::save
[(forms/in-form ::form)]
(fn [{{{:keys [from to]} :data :as merge-vendors-form} :db :as g} _]
(let [user @(re-frame/subscribe [::subs/token])]
{:graphql
{:token user
:owns-state {:single ::form}
:query-obj {:venia/operation {:operation/type :mutation
:operation/name "MergeVendors"}
:venia/queries [{:query/data [:merge-vendors
{:from (:id from) :to (:id to)} []]}]}
:on-success [::complete]}})))