diff --git a/src/clj/chicken_master/css.clj b/src/clj/chicken_master/css.clj index 17badd5..cd3b364 100644 --- a/src/clj/chicken_master/css.clj +++ b/src/clj/chicken_master/css.clj @@ -1,10 +1,31 @@ (ns chicken-master.css (:require [garden.def :refer [defstyles]] - [garden.stylesheet :refer [at-media]])) + [garden.stylesheet :refer [at-media at-keyframes]])) (defstyles screen + (at-keyframes "spin" + [:0% {:transform "rotate(0deg)"}] + [:100% {:transform "rotate(360deg)"}]) + [:html {:height "100%"} [:body {:height "100%"} + [:.hidden {:display :none}] + [:.loader-container {:position :absolute + :width "100%" + :height "100%" + :z-index 1000 + :background-color "rgba(0,0,0,0.4)" + } + [:.loader {:margin :auto + :position :relative + :top "40%" + :border "5px solid #f3f3f3"; /* Light grey */ + :border-top "5px solid #3498db"; /* Blue */ + :border-radius "50%"; + :width "30px" + :height "30px" + :animation "spin 1s linear infinite"}]] + [:.full-height {:height "100%"}] [:.scroll-bar {:position :absolute diff --git a/src/clj/chicken_master/handler.clj b/src/clj/chicken_master/handler.clj index 0f9062d..a48b573 100644 --- a/src/clj/chicken_master/handler.clj +++ b/src/clj/chicken_master/handler.clj @@ -10,6 +10,7 @@ (defn get-customers [] {:body (mocks/fetch-customers {})}) (defn add-customer [request] {:body (some-> request :body :name mocks/add-customer)}) +(defn delete-customer [id] {:body (mocks/delete-customer (edn/read-string id))}) (defn get-products [_] (prn _){:body (mocks/get-all-products)}) (defn save-products [request] {:body (some-> request :body mocks/save-stocks)}) @@ -32,6 +33,7 @@ (GET "/stock" {params :query-params} (get-stock params)) (GET "/customers" [] (get-customers)) (POST "/customers" request (add-customer request)) + (DELETE "/customers/:id" [id] (delete-customer id)) (GET "/products" request (get-products request)) (POST "/products" request (save-products request)) diff --git a/src/clj/chicken_master/mocks.clj b/src/clj/chicken_master/mocks.clj index 7a53120..9cd9b19 100644 --- a/src/clj/chicken_master/mocks.clj +++ b/src/clj/chicken_master/mocks.clj @@ -62,6 +62,10 @@ :name customer-name}) (fetch-stock {})) +(defn delete-customer [id] + {:orders (swap! orders #(->> % (remove (comp #{id} :id :who second)) (into {}))) + :customers (swap! customers (partial remove (comp #{id} :id)))}) + (defn day-customers [day] [day (->> @orders vals (filter (comp #{day} :day)))]) (defn get-orders [params] @orders) diff --git a/src/cljs/chicken_master/backend_mocks.cljs b/src/cljs/chicken_master/backend_mocks.cljs index dca9e33..9c37cae 100644 --- a/src/cljs/chicken_master/backend_mocks.cljs +++ b/src/cljs/chicken_master/backend_mocks.cljs @@ -58,6 +58,11 @@ (prn @customers) (fetch-stock params)) +(defn delete-customer [id] + {:orders (swap! orders #(->> % (remove (comp #{id} :id :who second)) (into {}))) + :customers (swap! customers (partial remove (comp #{id} :id)))}) + + (defn- day-customers [day] [day (->> @orders vals (filter (comp #{day} :day)))]) (defn- days-between [from to] (time/days-range diff --git a/src/cljs/chicken_master/calendar.cljs b/src/cljs/chicken_master/calendar.cljs index fef7f41..cdba0e6 100644 --- a/src/cljs/chicken_master/calendar.cljs +++ b/src/cljs/chicken_master/calendar.cljs @@ -8,7 +8,18 @@ [chicken-master.time :as time])) (defn format-raw-order [{:strs [who who-id notes] :as raw-values}] - {:who {:name who :id (prod/num-or-nil who-id)} + (prn who who-id) + (prn (some->> @(re-frame/subscribe [::subs/available-customers]) + (filter (comp #{who} :name)) + first :id)) + {:who {:name who + :id (if (prod/num-or-nil who-id) + (prod/num-or-nil who-id) + ;; seeing as there's an autocomplete thingy, assume that if the name is the same, + ;; then so is the user + (some->> @(re-frame/subscribe [::subs/available-customers]) + (filter (comp #{who} :name)) + first :id))} :notes notes :products (prod/collect-products (remove (comp #{"who" "notes"} first) raw-values))}) @@ -16,9 +27,12 @@ (html/modal :order-edit [:div - (let [who @(re-frame/subscribe [::subs/order-edit-who])] + (let [who @(re-frame/subscribe [::subs/order-edit-who]) + customers @(re-frame/subscribe [::subs/available-customers])] [:div - (html/input :who "kto" {:required true :default (:name who)}) + (html/input :who "kto" {:required true :default (:name who) :list :customers}) + (into [:datalist {:id :customers}] + (for [cust customers] [:option {:value (:name cust) :id (:id cust)}])) [:input {:id :who-id :name :who-id :type :hidden :value (or (:id who) "")}]]) (html/input :notes "notka" {:default @(re-frame/subscribe [::subs/order-edit-notes])}) diff --git a/src/cljs/chicken_master/config.cljs b/src/cljs/chicken_master/config.cljs index 20676e4..4f8b570 100644 --- a/src/cljs/chicken_master/config.cljs +++ b/src/cljs/chicken_master/config.cljs @@ -11,7 +11,7 @@ :calendar-heading false ; show a header with the names of days :show-date true ; display the date for each day :show-day-name-with-date true ; add the day name to each date - :show-day-add-order false ; Show an add order button in each day + :show-day-add-order true ; Show an add order button in each day :show-order-time false ; display the time of each order :show-order-notes true ; display notes diff --git a/src/cljs/chicken_master/customers.cljs b/src/cljs/chicken_master/customers.cljs index 33d4ef6..d0d4c20 100644 --- a/src/cljs/chicken_master/customers.cljs +++ b/src/cljs/chicken_master/customers.cljs @@ -33,7 +33,10 @@ (group-by #(get-in % [:who :id])))] (for [{:keys [name id] :as who} @(re-frame/subscribe [::subs/available-customers])] [:details {:class "client" :key (gensym)} - [:summary name] + [:summary [:span name [:button {:on-click #(re-frame/dispatch + [::event/confirm-action + "na pewno usunąć?" + ::event/remove-customer id])} "-"]]] [order-adder {:who who}] (for [order (reverse (sort-by :day (client-orders id)))] [order-adder (assoc order :key (gensym))]) diff --git a/src/cljs/chicken_master/events.cljs b/src/cljs/chicken_master/events.cljs index 8d7f5c9..65487e9 100644 --- a/src/cljs/chicken_master/events.cljs +++ b/src/cljs/chicken_master/events.cljs @@ -36,10 +36,13 @@ (time/update-settings default-settings) {:db (assoc db/default-db :settings default-settings) :fx [[:dispatch [::show-from-date (time/iso-date (time/today))]] + [:dispatch [::start-loading]] [:dispatch [::fetch-stock]] [:dispatch [::fetch-orders]]]})) (re-frame/reg-event-db ::hide-modal (fn [db [_ modal]] (assoc-in db [modal :show] nil))) +(re-frame/reg-event-db ::start-loading (fn [db _] (assoc db :loading? true))) +(re-frame/reg-event-db ::stop-loading (fn [db _] (assoc db :loading? nil))) (re-frame/reg-event-fx ::confirm-action @@ -92,6 +95,7 @@ ::process-fetched-days (fn [db [_ days]] (-> db + (assoc :loading? nil) (update :current-days #(map (fn [[day orders]] [day (if (contains? days day) (days day) orders)]) %)) @@ -101,6 +105,7 @@ ::scroll-weeks (fn [{db :db} [_ offset]] {:fx [;[:dispatch [::fetch-stock]] + [:dispatch [::start-loading]] [:dispatch [::show-from-date (-> db :start-date time/parse-date @@ -120,7 +125,8 @@ (re-frame/reg-event-fx ::fetch-orders (fn [_ [_ from to]] - {(settings :http-dispatch) (http-get "orders" {} ::process-stock)})) + {:dispatch [::start-loading] + (settings :http-dispatch) (http-get "orders" {} ::process-stock)})) ;; Customers events (re-frame/reg-event-fx @@ -135,6 +141,11 @@ {(settings :http-dispatch) (http-request :post "customers" :body {:name customer-name} :on-success ::process-stock)})) +(re-frame/reg-event-fx + ::remove-customer + (fn [_ [_ id]] + {(settings :http-dispatch) (http-request :delete (str "customers/" id) + :on-success ::process-stock)})) ;;; Storage events @@ -147,7 +158,8 @@ (re-frame/reg-event-fx ::fetch-stock (fn [_ _] - {(settings :http-dispatch) (http-get "stock" {} ::process-stock)})) + {:dispatch [::start-loading] + (settings :http-dispatch) (http-get "stock" {} ::process-stock)})) (defn assoc-if [coll key val] (if val (assoc coll key val) coll)) (re-frame/reg-event-fx @@ -179,7 +191,8 @@ (re-frame/reg-event-fx ::save-stock (fn [_ [_ products]] - {:dispatch [::hide-modal :stock] + {:fx [[:dispatch [::hide-modal :stock]] + [:dispatch [::start-loading]]] (settings :http-dispatch) (http-request :post "products" :body products :on-sucess ::process-stock)})) ;; Settings @@ -221,9 +234,11 @@ (and (= method :post) (= uri "http://localhost:3000/products")) (re-frame/dispatch (conj on-success (mocks/save-stocks (cljs.reader/read-string body)))) - (= method :delete) + (and (= method :delete) (= (nth parts 3) "orders")) (re-frame/dispatch (conj on-success (mocks/delete-order (-> parts (nth 4) (js/parseInt))))) + (and (= method :delete) (= (nth parts 3) "customers")) + (re-frame/dispatch (conj on-success (mocks/delete-customer (-> parts (nth 4) (js/parseInt))))) (and (= method :post) (= uri "http://localhost:3000/customers")) (re-frame/dispatch (conj on-success (mocks/add-customer (cljs.reader/read-string body)))) diff --git a/src/cljs/chicken_master/subs.cljs b/src/cljs/chicken_master/subs.cljs index 0512951..c396f94 100644 --- a/src/cljs/chicken_master/subs.cljs +++ b/src/cljs/chicken_master/subs.cljs @@ -3,6 +3,7 @@ (re-frame/reg-sub ::name (fn [db] (:name db))) (re-frame/reg-sub ::settings (fn [db] (:settings db))) +(re-frame/reg-sub ::loading? (fn [db] (:loading? db))) (re-frame/reg-sub ::available-products (fn [db] (:products db))) (re-frame/reg-sub ::available-customers (fn [db] (:customers db))) diff --git a/src/cljs/chicken_master/views.cljs b/src/cljs/chicken_master/views.cljs index d477f7d..e0ca60a 100644 --- a/src/cljs/chicken_master/views.cljs +++ b/src/cljs/chicken_master/views.cljs @@ -14,6 +14,8 @@ (defn main-panel [] [:div {:class :full-height} + [:div {:class [:loader-container (if-not @(re-frame/subscribe [::subs/loading?]) :hidden)]} + [:div {:class :loader}]] (cond @(re-frame/subscribe [::subs/show-stock-modal]) (stock/show-available) @(re-frame/subscribe [::subs/show-settings-modal]) (show-settings)