logout button

This commit is contained in:
Daniel O'Connell 2021-03-27 18:40:11 +01:00
parent ccf3214aba
commit 37f0498ddf
3 changed files with 21 additions and 11 deletions

View File

@ -247,7 +247,7 @@ html body .calendar .day .product .product-name {
html body .calendar .day .product .product-amount { html body .calendar .day .product .product-amount {
width: 40px; width: 40px;
max-height: 5px; max-height: 5px;
margin-right: 10px; display: inline-block;
} }
html body .calendar .day .product .product-price { html body .calendar .day .product .product-price {

View File

@ -70,13 +70,14 @@
[:label {:for id} label]) [:label {:for id} label])
[:input (merge {:name id :id id} handlers (dissoc opts :parser))] [:input (merge {:name id :id id} handlers (dissoc opts :parser))]
(when (and label (#{:checkbox :radio} (:type opts))) (when (and label (#{:checkbox :radio} (:type opts)))
[:label {:for id} label]) [:label {:for id} label])]))
]))
(defn settings-options [] (defn settings-options []
[:div [:div
[:h3 "Ustawienia używtkownika"]
[:button {:type :button :on-click #(re-frame/dispatch [:chicken-master.events/log-out])} "wyloguj"]
[:h3 "Ustawienia wyglądu kalendarza"] [:h3 "Ustawienia wyglądu kalendarza"]
[:button {:type :button :on-click #(set-item! :bearer-token nil)} "wyloguj"]
(input :first-day-offset "o ile dni przesunąć niedziele w lewo" (input :first-day-offset "o ile dni przesunąć niedziele w lewo"
{:type :number :max 7 :min 0 :parser #(js/parseInt %)}) {:type :number :max 7 :min 0 :parser #(js/parseInt %)})

View File

@ -34,15 +34,19 @@
::initialize-db ::initialize-db
(fn [_ _] (fn [_ _]
(time/update-settings config/default-settings) (time/update-settings config/default-settings)
(let [user (some-> js/window (.-localStorage) (.getItem :bearer-token))] (let [user (some-> js/window (.-localStorage) (.getItem :bearer-token))
{:db (assoc db/default-db user (when-not (= user "null") user)
result {:db (assoc db/default-db
:settings config/default-settings :settings config/default-settings
:current-user user) :current-user user)}]
:dispatch [(when user ::load-db)]}))) (if user
(assoc result :dispatch [::load-db])
result))))
(re-frame/reg-event-fx (re-frame/reg-event-fx
::load-db ::load-db
(fn [_ _] (fn [_ _]
(prn "loading")
(time/update-settings config/default-settings) (time/update-settings config/default-settings)
{:fx [[:dispatch [::show-from-date (time/iso-date (time/today))]] {:fx [[:dispatch [::show-from-date (time/iso-date (time/today))]]
[:dispatch [::start-loading]] [:dispatch [::start-loading]]
@ -68,8 +72,8 @@
(re-frame/reg-event-fx (re-frame/reg-event-fx
::failed-request ::failed-request
(fn [{db :db} [_ response]] (fn [{db :db} [_ response]]
{:db (update db :current-user #(when-not (= (:status response) 401) %)) {:fx [(when (= (:status response) 401) [:dispatch [::log-out]])
:fx [[:dispatch [::log-error (str response)]] [:dispatch [::log-error (str response)]]
[:dispatch [::stop-loading]]]})) [:dispatch [::stop-loading]]]}))
(re-frame/reg-event-fx (re-frame/reg-event-fx
@ -234,6 +238,11 @@
{:db (assoc db :current-user (b64/encodeString (str (user "name") ":" (user "password")))) {:db (assoc db :current-user (b64/encodeString (str (user "name") ":" (user "password"))))
:dispatch [::load-db]})) :dispatch [::load-db]}))
(re-frame/reg-event-db
::log-out
(fn [db _]
(config/set-item! :bearer-token nil)
(dissoc db :current-user)))
(comment (comment
(re-frame/dispatch-sync [::show-stock]) (re-frame/dispatch-sync [::show-stock])