Skip to content

Commit

Permalink
add frontend logic to handle duplicates as well
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaCrotti committed Feb 23, 2019
1 parent 78036ea commit 5c8e0dd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
24 changes: 13 additions & 11 deletions src/cljs/byf/common/handlers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
41 changes: 26 additions & 15 deletions src/cljs/byf/league_detail/handlers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand All @@ -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?]))
Expand Down Expand Up @@ -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))

Expand All @@ -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))

Expand Down
13 changes: 9 additions & 4 deletions src/cljs/byf/league_detail/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
[]
Expand Down

0 comments on commit 5c8e0dd

Please sign in to comment.