can import from excel now.

This commit is contained in:
Bryce Covert
2018-05-03 15:23:10 -07:00
parent 65e65302bf
commit ea1e43d981
9 changed files with 192 additions and 5 deletions

View File

@@ -0,0 +1,79 @@
(ns auto-ap.views.pages.admin.excel-import
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [re-frame.core :as re-frame]
[reagent.core :as reagent]
[auto-ap.subs :as subs]
[auto-ap.events.admin.companies :as events]
[auto-ap.entities.companies :as entity]
[auto-ap.views.utils :refer [login-url dispatch-value-change bind-field horizontal-field dispatch-event]]
[cljs.reader :as edn]
[auto-ap.routes :as routes]
[bidi.bidi :as bidi]))
(re-frame/reg-sub
::excel-import
(fn [db]
(::excel-import db)))
(re-frame/reg-event-db
::change
(fn [db [_ field v]]
(assoc-in db (into [::excel-import] field) v)))
(re-frame/reg-event-db
::edit
(fn [db [_ field v]]
(assoc db ::excel-import nil )
db))
(re-frame/reg-event-fx
::save
(fn [{:keys [db]}]
(let [excel-import (::excel-import db)]
{:http {:token (:user db)
:method :post
:body (pr-str excel-import)
:headers {"Content-Type" "application/edn"}
:uri (str "/api/invoices/upload-integreat")
:on-success [::save-complete]
:on-error [::save-error]}})))
(re-frame/reg-event-fx
::save-complete
(fn [{:keys [db]} [_ rows]]
{:dispatch [::edit nil]
:db (assoc-in db [::excel-import :rows] rows)}))
(re-frame/reg-event-fx
::save-error
(fn [{:keys [db]}]
{:dispatch [::change [:error] true]}))
(defn admin-excel-import-page []
[:div
(let [excel-import-data @(re-frame/subscribe [::excel-import])]
[:div
[:h1.title "Import Invoices from Integreat Excel"]
[bind-field
[:textarea.textarea {:rows "20"
:field :excel-rows
:type "text"
:event ::change
:subscription excel-import-data}]]
[:button.button.is-large.is-pulled-right.is-primary {:on-click (dispatch-event [::save])} "Import"]
[:div
[:table.table.is-fullwidth
[:thead
[:td "Date"]
[:td "Invoice #"]
[:td "Vendor"]
[:td "Company"]]
[:tbody
(for [{:keys [invoice-number date vendor-name company]} (:rows excel-import-data)]
[:tr
[:td date]
[:td invoice-number]
[:td vendor-name]
[:td company]])]]]])])