drag n drop

This commit is contained in:
Daniel O'Connell 2021-02-06 15:22:19 +01:00
parent 95f0af672f
commit d5562736f1
3 changed files with 43 additions and 16 deletions

View File

@ -11,12 +11,12 @@
;;; Orders ;;; Orders
(def id-counter (atom -1)) (def id-counter (atom -1))
(def days (atom ;; (def days (atom
(->> (time/date-offset (new js/Date) -50) ;; (->> (time/date-offset (new js/Date) -50)
(time/days-range 90) ;; (time/days-range 90)
(map (fn [date] ;; (map (fn [date]
[(time/iso-date date) (repeatedly (rand-int 6) #(swap! id-counter inc))])) ;; [(time/iso-date date) (repeatedly (rand-int 6) #(swap! id-counter inc))]))
(into {})))) ;; (into {}))))
(def notes ["bezglutenowy" (def notes ["bezglutenowy"
"tylko z robakami" "tylko z robakami"
"przyjdzie wieczorem" "przyjdzie wieczorem"
@ -28,7 +28,10 @@
{:id 3 :name "johnny"}])) {:id 3 :name "johnny"}]))
(def orders (def orders
(atom (atom
(->> @days (->> (time/date-offset (new js/Date) -50)
(time/days-range 90)
(map (fn [date]
[(time/iso-date date) (repeatedly (rand-int 6) #(swap! id-counter inc))]))
(map (fn [[day ids]] (map (fn [[day ids]]
(map (fn [i] (map (fn [i]
{:id i :day day {:id i :day day
@ -58,7 +61,7 @@
:name customer-name}) :name customer-name})
(fetch-stock params)) (fetch-stock params))
(defn- day-customers [day] [day (->> day (get @days) (map (partial get @orders)))]) (defn- day-customers [day] [day (->> @orders vals (filter (comp #{day} :day)))])
(defn- days-between [from to] (defn- days-between [from to]
(time/days-range (time/days-range
(int (/ (- (time/parse-date to) (time/parse-date from)) (* 24 3600000))) (int (/ (- (time/parse-date to) (time/parse-date from)) (* 24 3600000)))
@ -70,17 +73,26 @@
(map day-customers) (map day-customers)
(into {}))) (into {})))
(defn- replace-order [order] (defn- replace-order [{start-from :start-from :as order}]
(println "replacing order" order) (println "replacing order" order)
(let [order (update order :id #(or % (swap! id-counter inc)))] (let [order (-> order
(swap! days (fn [ds] (update ds (:day order) #(distinct (conj % (:id order)))))) (dissoc :start-from)
(swap! orders #(assoc % (:id order) order)) (update :id #(or % (swap! id-counter inc))))]
{(->> order :day) (->> order :day day-customers second)})) (prn "order 1" order)
(swap! orders assoc (:id order) order)
(prn "order 2" (@orders (:id order)))
(if start-from
(->> start-from
time/start-of-week
(time/days-range 28)
(map time/iso-date)
(map day-customers)
(into {}))
{(:day order) (->> order :day day-customers second)})))
(defn- delete-order [{id :id}] (defn- delete-order [{id :id}]
(println "deleting order" id) (println "deleting order" id)
(let [day (-> (get @orders id) :day)] (let [day (-> (get @orders id) :day)]
(swap! days (fn [ds] (update ds day (partial remove #{id}))))
(swap! orders #(dissoc % id)) (swap! orders #(dissoc % id))
{day (->> day day-customers second)})) {day (->> day day-customers second)}))

View File

@ -28,7 +28,9 @@
(fn [form] (re-frame/dispatch [::event/save-order (format-raw-order form)])))) (fn [form] (re-frame/dispatch [::event/save-order (format-raw-order form)]))))
(defn format-order [{:keys [id who day hour notes products state]}] (defn format-order [{:keys [id who day hour notes products state]}]
[:div {:class [:order state] :key (gensym)} [:div {:class [:order state] :key (gensym)
:draggable true
:on-drag-start #(-> % .-dataTransfer (.setData "text" id))}
[:div {:class :actions} [:div {:class :actions}
(condp = state (condp = state
:waiting [:button {:on-click #(re-frame/dispatch [::event/fulfill-order id])} "✓"] :waiting [:button {:on-click #(re-frame/dispatch [::event/fulfill-order id])} "✓"]
@ -50,7 +52,11 @@
(into [:div {:class :products}]))]) (into [:div {:class :products}]))])
(defn day [{:keys [date orders]}] (defn day [{:keys [date orders]}]
[:div {:class [:day (when (time/today? date) :today)]} [:div {:class [:day (when (time/today? date) :today)]
:on-drag-over #(.preventDefault %)
:on-drop #(let [id (-> % .-dataTransfer (.getData "text") prod/num-or-nil)]
(.preventDefault %)
(re-frame/dispatch [::event/move-order id (time/iso-date date)]))}
[:div {:class :day-header} (time/format-date date)] [:div {:class :day-header} (time/format-date date)]
[:div [:div
[:div {:class :orders} [:div {:class :orders}

View File

@ -35,6 +35,15 @@
:on-success [::process-fetched-days] :on-success [::process-fetched-days]
:on-fail [::failed-blah]}})) :on-fail [::failed-blah]}}))
(re-frame/reg-event-fx
::move-order
(fn [{{orders :orders start-date :start-date} :db} [_ id day]]
{:http {:method :post
:url "save-order"
:params (-> id orders (assoc :day day :start-from start-date))
:on-success [::process-fetched-days]
:on-fail [::failed-blah]}}))
(re-frame/reg-event-db (re-frame/reg-event-db
::edit-order ::edit-order
(fn [{orders :orders :as db} [_ day id]] (fn [{orders :orders :as db} [_ day id]]