From 5c8e0dd3b688faeee33e973c288aa5b99bf785be Mon Sep 17 00:00:00 2001 From: Andrea Crotti Date: Sat, 23 Feb 2019 09:33:30 +0000 Subject: [PATCH] add frontend logic to handle duplicates as well --- src/cljs/byf/common/handlers.cljs | 24 +++++++------- src/cljs/byf/league_detail/handlers.cljs | 41 +++++++++++++++--------- src/cljs/byf/league_detail/views.cljs | 13 +++++--- 3 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/cljs/byf/common/handlers.cljs b/src/cljs/byf/common/handlers.cljs index 567d99e..5d6bb96 100644 --- a/src/cljs/byf/common/handlers.cljs +++ b/src/cljs/byf/common/handlers.cljs @@ -58,17 +58,19 @@ :on-failure [:failed]}})) (defn writer - [page uri on-success transform-params-fn] - (fn [{:keys [db]} _] - {:http-xhrio {:method :post - :uri uri - :params (merge (transform-params-fn db) - {:league_id (get-league-id db)}) - - :format (ajax/json-request-format) - :response-format (ajax/json-response-format {:keywords? true}) - :on-success [on-success] - :on-failure [:failed]}})) + ([page uri on-success transform-params-fn] + (writer page uri on-success transform-params-fn :failed)) + ([page uri on-success transform-params-fn on-failure] + (fn [{:keys [db]} _] + {:http-xhrio {:method :post + :uri uri + :params (merge (transform-params-fn db) + {:league_id (get-league-id db)}) + + :format (ajax/json-request-format) + :response-format (ajax/json-response-format {:keywords? true}) + :on-success [on-success] + :on-failure [on-failure]}}))) (defn failed [page] diff --git a/src/cljs/byf/league_detail/handlers.cljs b/src/cljs/byf/league_detail/handlers.cljs index 080ac78..b8043a5 100644 --- a/src/cljs/byf/league_detail/handlers.cljs +++ b/src/cljs/byf/league_detail/handlers.cljs @@ -45,7 +45,7 @@ :league_id nil :show-all? false :game-config shared/default-game-config - :show-notification false + :notification nil :loading? false :show-graph false}) @@ -63,14 +63,18 @@ [name-mapping vals] (medley/map-keys #(get name-mapping %) vals)) -(rf/reg-sub ::show-notification (getter [:show-notification])) -(rf/reg-event-db ::show-notification - (fn [db _] - (common/assoc-in* db page [:show-notification] true))) +(rf/reg-sub ::notification (getter [:notification])) +(rf/reg-event-db ::notification + (fn [db [_ type msg]] + (common/assoc-in* db + page + [:notification] + {:type type + :msg msg}))) (rf/reg-event-db ::clear-notification (fn [db _] - (common/assoc-in* db page [:show-notification] false))) + (common/assoc-in* db page [:notification] false))) (rf/reg-sub ::show-graph (getter [:show-graph])) (rf/reg-sub ::loading? (getter [:loading?])) @@ -258,15 +262,20 @@ (rf/reg-event-db ::played_at (setter [:game :played_at])) (defn- reload-fn-gen - [extra-signal] - (fn [{:keys [db] :as full} other] - (js/console.log "Other = " other ", full = " full) - {:db db - :dispatch-n (cons extra-signal [[::show-notification] - [::players-handlers/load-players] - [::load-games]])})) + [{:keys [db] :as full} other] + (js/console.log "Other = " other ", full = " full) + {:db db + :dispatch-n [[::notification :success "Game added successfully"] + [::reset-game] + [::players-handlers/load-players] + [::load-games]]}) + +(rf/reg-event-fx ::add-game-success reload-fn-gen) -(rf/reg-event-fx ::add-game-success (reload-fn-gen [::reset-game])) +(rf/reg-event-fx ::add-game-failed + (fn [{:keys [db]}] + (js/console.log "inside game failed") + {:dispatch [::notification :failure "Duplicate game"]})) (rf/reg-event-db ::failed (common/failed page)) @@ -290,7 +299,9 @@ (rf/reg-event-fx ::add-game (common/writer page "/api/add-game" - ::add-game-success game-transform)) + ::add-game-success + game-transform + ::add-game-failed)) (rf/reg-sub ::hidden? (sets/in? page :hidden-players)) diff --git a/src/cljs/byf/league_detail/views.cljs b/src/cljs/byf/league_detail/views.cljs index 0b3ca59..4b15966 100644 --- a/src/cljs/byf/league_detail/views.cljs +++ b/src/cljs/byf/league_detail/views.cljs @@ -428,13 +428,18 @@ (defn notifications [] - (let [show-notification (rf/subscribe [::handlers/show-notification])] + (let [notification (rf/subscribe [::handlers/notification])] (fn [] - (when @show-notification - [:div.notification.is-success + (when @notification + [:div.notification + {:class (case (:type @notification) + :success "is-success" + :failure "is-failure")} + + [:button.delete {:on-click #(rf/dispatch [::handlers/clear-notification])}] - "Thank you, your game has been recorded"])))) + (:msg @notification)])))) (defn root []