diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index 9d42d615..2358bc17 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -34,15 +34,19 @@ :company_id {:type 'Int} :vendor {:type :vendor - :resolve :get-vendor} + :resolve :get-vendor-for-invoice} :company {:type :company - :resolve :get-company}}}} + :resolve :get-company-for-invoice}}}} :queries {:invoice {:type '(list :invoice) :args {:imported {:type 'Boolean} :company_id {:type 'Int}} - :resolve :get-invoice}}}) + :resolve :get-invoice} + :company {:type '(list :company) + :resolve :get-company} + :vendor {:type '(list :vendor) + :resolve :get-vendor}}}) (defn by [x kf] (reduce @@ -99,23 +103,33 @@ ->graphql (invoices/get-graphql (<-graphql args))) extra-context))) -(defn get-vendor [context args value] +(defn get-vendor-for-invoice [context args value] (->graphql (if-let [vendor-cache (:vendor-cache context)] (vendor-cache (:vendor_id value)) (vendors/get-by-id (:vendor_id value))))) -(defn get-company [context args value] +(defn get-company-for-invoice [context args value] (->graphql (if-let [company-cache (:company-cache context)] (company-cache (:company_id value)) (companies/get-by-id (:company_id value))))) +(defn get-company [context args value] + (->graphql + (companies/get-all))) + +(defn get-vendor [context args value] + (->graphql + (vendors/get-all))) + (def schema (-> integreat-schema (attach-resolvers {:get-invoice get-invoice - :get-vendor get-vendor - :get-company get-company}) + :get-vendor-for-invoice get-vendor-for-invoice + :get-company-for-invoice get-company-for-invoice + :get-company get-company + :get-vendor get-vendor}) schema/compile)) diff --git a/src/cljs/auto_ap/events.cljs b/src/cljs/auto_ap/events.cljs index 92879150..8662e5e2 100644 --- a/src/cljs/auto_ap/events.cljs +++ b/src/cljs/auto_ap/events.cljs @@ -19,10 +19,11 @@ {:db (assoc db/default-db :active-page handler :user token) - :http {:method :get - :token token - :uri (str "/api/companies") - :on-success [::received-companies]}})))) + :graphql {:token token + :query-obj {:venia/queries [[:company + [:id :name]]]} + + :on-success [::received-companies]}})))) (re-frame/reg-event-db ::toggle-menu @@ -32,15 +33,16 @@ (re-frame/reg-event-fx ::logged-in (fn [{:keys [db]} [_ token user]] - {:http {:method :get - :token token - :uri (str "/api/companies") - :on-success [::received-companies]} + {:graphql {:token token + :query-obj {:venia/queries [[:company + [:id :name]]]} + + :on-success [::received-companies]} :db (assoc db :user (assoc user :token token))})) (re-frame/reg-event-db ::received-companies - (fn [db [_ companies]] + (fn [db [_ {companies :company}]] (assoc db :companies (reduce (fn [companies company] (assoc companies (:id company) company)) @@ -50,6 +52,7 @@ (re-frame/reg-event-db ::swap-company (fn [db [_ company]] + (println company) (assoc db :company (:id company)))) (re-frame/reg-event-fx diff --git a/src/cljs/auto_ap/views/main.cljs b/src/cljs/auto_ap/views/main.cljs index 5a5debda..87d4cbc7 100644 --- a/src/cljs/auto_ap/views/main.cljs +++ b/src/cljs/auto_ap/views/main.cljs @@ -32,7 +32,8 @@ [:a {:class "navbar-link login" :on-click (fn [e] (re-frame/dispatch [::events/toggle-menu :account]))} (:name @user)] [:div {:class "navbar-dropdown"} [:a {:class "navbar-item"} "My profile"] - [:a {:class "navbar-item" :href (bidi/path-for routes/routes :admin)} "Administration"] + [:a {:class "navbar-item" :href (bidi/path-for routes/routes :admin) + :on-click (fn [e] (re-frame/dispatch [::events/toggle-menu :account]))} "Administration"] [:hr {:class "navbar-divider"}] [:a.navbar-item {:on-click (fn [e] (.preventDefault e) (re-frame/dispatch [::events/logout]))} "Logout"]]] [:a.navbar-item {:href login-url} "Login"])]])) @@ -126,15 +127,15 @@ :on-click (fn [] (re-frame/dispatch [::events/toggle-menu :company]))} [:div.navbar-start [:div { :class (str "navbar-item has-dropdown " (when (get-in @menu [:company :active?]) "is-active"))} - [:a {:class "navbar-link login"} "Company: " (if @company (::company/name @company) + [:a {:class "navbar-link login"} "Company: " (if @company (:name @company) "All")] [:div {:class "navbar-dropdown"} [:a {:class "navbar-item" :on-click (fn [] (re-frame/dispatch [::events/swap-company nil])) } "All"] [:hr {:class "navbar-divider"}] - (for [{:keys [::company/name] :as company} @companies] - ^{:key name } + (for [{:keys [name id] :as company} @companies] + ^{:key id } [:a {:class "navbar-item" :on-click (fn [] (re-frame/dispatch [::events/swap-company company])) } name])]]]] diff --git a/src/cljs/auto_ap/views/pages/import_invoices.cljs b/src/cljs/auto_ap/views/pages/import_invoices.cljs index a8f9e907..67519ca7 100644 --- a/src/cljs/auto_ap/views/pages/import_invoices.cljs +++ b/src/cljs/auto_ap/views/pages/import_invoices.cljs @@ -23,7 +23,7 @@ (js/Dropzone. (reagent/dom-node this) (clj->js {:init (fn [] (.on (js-this) "success" (fn [_ files] - (re-frame/dispatch [::events/received-invoices :pending (edn/read-string files)])))) + (re-frame/dispatch [::events/view-pending-invoices])))) :paramName "file" :headers {"Authorization" (str "Token " @token)} :url (str "/api/invoices/upload"