localstorage db

This commit is contained in:
Daniel O'Connell 2021-02-07 17:23:55 +01:00
parent b1889b44b6
commit 5110316617
3 changed files with 64 additions and 23 deletions

View File

@ -32,7 +32,13 @@
(set-item! val (apply fun (get-item val) args))
(get-item val))
(defn purge-items []
(doseq [item [:stock-products :products :customers :orders :settings]]
(remove-item! item))
(set-item! :id-counter -1))
(defn generate-items []
(set-item! :settings {})
(set-item! :stock-products {:eggs 22 :milk 32 :cabbage 54 :carrots 11 :cows 32 :ants 21})
(set-item! :id-counter -1)
@ -60,8 +66,6 @@
flatten
(map #(vector (:id %) %))
(into {}))))
(if-not (get-item :orders)
(generate-items))
(defn fetch-customers [_]
(get-item :customers))

View File

@ -6,20 +6,34 @@
(def debug?
^boolean goog.DEBUG)
(def default-settings {:first-day-offset 1 ; which is the first day of the week (add the offset to `day-names`)
:day-names ["Niedz" "Pon" "Wt" "Śr" "Czw" "Pt" "Sob"] ; how days should be displayed in the calendar view
: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 true ; Show an add order button in each day
(defn set-item!
"Set `key' in browser's localStorage to `val`."
[key val]
(.setItem (.-localStorage js/window) key val))
:show-order-time false ; display the time of each order
:show-order-notes true ; display notes
:editable-number-inputs false ; only allow number modifications in the edit modal
:hide-fulfilled-orders false
(defn get-setting
"Returns value of `key' from browser's localStorage."
([key]
(-> js/window (.-localStorage) (.getItem :settings) cljs.reader/read-string (get key)))
([key default]
(if (nil? (get-setting key))
default
(get-setting key))))
:http-dispatch :http;-xhrio
:backend-url "http://localhost:3000/"
(def default-settings {:first-day-offset (get-setting :first-day-offset 1) ; which is the first day of the week (add the offset to `day-names`)
:day-names (get-setting :day-names ["Niedz" "Pon" "Wt" "Śr" "Czw" "Pt" "Sob"]) ; how days should be displayed in the calendar view
:calendar-heading (get-setting :calendar-heading false) ; show a header with the names of days
:show-date (get-setting :show-date true) ; display the date for each day
:show-day-name-with-date (get-setting :show-day-name-with-date true) ; add the day name to each date
:show-day-add-order (get-setting :show-day-add-order true) ; Show an add order button in each day
:show-order-time (get-setting :show-order-time false) ; display the time of each order
:show-order-notes (get-setting :show-order-notes true) ; display notes
:editable-number-inputs (get-setting :editable-number-inputs false) ; only allow number modifications in the edit modal
:hide-fulfilled-orders (get-setting :hide-fulfilled-orders false)
:http-dispatch (get-setting :http-dispatch :http);-xhrio
:backend-url (get-setting :backend-url "http://localhost:3000/")
})
(defn- settings [key]
@ -34,6 +48,8 @@
(assoc db :settings settings))))
(defn change-setting [key val]
(set-item! :settings (assoc (get-setting :settings) key val))
(prn (get-setting :settings))
(re-frame/dispatch [::change-setting key val]))
(defn input [id label opts]
@ -75,4 +91,14 @@
[:option {:value :http} "client side mock"]
[:option {:value :http-xhrio} "re-frame-http-fx"]]
(input :backend-url "backend URL" {})])
(input :backend-url "backend URL" {})
[:button {:on-click #(re-frame/dispatch
[:chicken-master.events/confirm-action
"na pewno wyczyścić?"
:chicken-master.events/clear-database])} "Wyczyść bazę"]
[:button {:on-click #(re-frame/dispatch
[:chicken-master.events/confirm-action
"na pewno nowe dane wygenerować?"
:chicken-master.events/generate-database])} "Wygeneruj dane"]
])

View File

@ -119,6 +119,7 @@
days (into #{} (time/get-weeks day 2))
filtered-orders (->> orders vals (filter (comp days :day)) (group-by :day))]
(assoc db
:loading? nil
:start-date day
:current-days (map #(vector % (get filtered-orders %)) (sort days))))))
@ -144,7 +145,8 @@
(re-frame/reg-event-fx
::remove-customer
(fn [_ [_ id]]
{(settings :http-dispatch) (http-request :delete (str "customers/" id)
{:dispatch [::start-loading]
(settings :http-dispatch) (http-request :delete (str "customers/" id)
:on-success ::process-stock)}))
;;; Storage events
@ -197,13 +199,10 @@
;; Settings
(re-frame/reg-event-fx
(re-frame/reg-event-db
::show-settings
(fn [{db :db} _]
{:db (assoc-in db [:settings :show] true)}))
(fn [db _]
(assoc-in db [:settings :show] true)))
(comment
@ -212,7 +211,19 @@
)
;;;;;;;; Backend mocks
(re-frame/reg-event-fx
::clear-database
(fn [_ _]
(mocks/purge-items)
(.reload js/location)))
(re-frame/reg-event-fx
::generate-database
(fn [_ _]
(mocks/generate-items)
{:fx [[:dispatch [::start-loading]]
[:dispatch [::fetch-stock]]
[:dispatch [::fetch-orders]]]}))
(re-frame/reg-fx
:http