From 6cac8a9042f0ae58795b477513aeb35822d59234 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sat, 23 Jun 2018 18:06:56 -0400 Subject: [PATCH 1/5] Add desktop nofications --- assets/js/app.js | 12 ++++++++++++ assets/js/services/notifications.js | 16 ++++++++++++++++ assets/package.json | 3 ++- assets/yarn.lock | 12 +++++++++++- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 assets/js/services/notifications.js diff --git a/assets/js/app.js b/assets/js/app.js index adb6cd8..e7f5c6c 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -5,9 +5,11 @@ import "phoenix_html"; import React from "react"; import ReactDOM from "react-dom"; +import watch from "redux-watch"; import { createStore } from "redux"; import Channel from "./services/channel"; +import Notifications from "./services/notifications"; import chessBoardReducer from "./reducers/chess-board"; import { setGameId } from "./store/actions"; @@ -17,6 +19,7 @@ import MoveList from "./components/move-list"; import GameInfo from "./components/game-info"; const store = createStore(chessBoardReducer); +const notifications = new Notifications(); class App extends React.Component { componentWillMount() { @@ -24,6 +27,15 @@ class App extends React.Component { store.dispatch(setGameId(gameId)); + let w = watch(store.getState, "turn"); + store.subscribe(w((newVal, oldVal, objectPath) => { + const player = store.getState().player; + + if (oldVal != null && newVal == player) { + notifications.notifyTurn(player); + } + })); + this.channel = new Channel(store, gameId); } diff --git a/assets/js/services/notifications.js b/assets/js/services/notifications.js new file mode 100644 index 0000000..27ae525 --- /dev/null +++ b/assets/js/services/notifications.js @@ -0,0 +1,16 @@ +class Notifications { + constructor() { + Notification.requestPermission(); + } + + notifyTurn(player) { + if (!document.hasFocus()) { + new Notification("Chess", { + body: "Your opponent has moved.", + icon: `/images/king_${player}.svg` + }); + } + } +} + +export default Notifications; diff --git a/assets/package.json b/assets/package.json index 98a198b..06f26e2 100644 --- a/assets/package.json +++ b/assets/package.json @@ -17,7 +17,8 @@ "react": "^16.2.0", "react-dom": "^16.2.0", "react-redux": "^5.0.6", - "redux": "^3.7.2" + "redux": "^3.7.2", + "redux-watch": "^1.1.1" }, "devDependencies": { "babel-brunch": "^6.1.1", diff --git a/assets/yarn.lock b/assets/yarn.lock index 1dc774a..20d6963 100644 --- a/assets/yarn.lock +++ b/assets/yarn.lock @@ -2732,6 +2732,10 @@ object-keys@^1.0.8: version "1.0.11" resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.0.11.tgz#c54601778ad560f1142ce0e01bcca8b56d13426d" +object-path@^0.9.2: + version "0.9.2" + resolved "https://registry.yarnpkg.com/object-path/-/object-path-0.9.2.tgz#0fd9a74fc5fad1ae3968b586bda5c632bd6c05a5" + object.omit@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/object.omit/-/object.omit-2.0.1.tgz#1a9c744829f39dbb858c76ca3579ae2a54ebd1fa" @@ -2864,7 +2868,7 @@ performance-now@^2.1.0: version "1.3.2" "phoenix_html@file:../deps/phoenix_html": - version "2.11.1" + version "2.11.2" pify@^2.0.0: version "2.3.0" @@ -3184,6 +3188,12 @@ redent@^1.0.0: indent-string "^2.1.0" strip-indent "^1.0.1" +redux-watch@^1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/redux-watch/-/redux-watch-1.1.1.tgz#a15a59220894a132c93f75fb71a420bc35cb7843" + dependencies: + object-path "^0.9.2" + redux@^3.7.2: version "3.7.2" resolved "https://registry.yarnpkg.com/redux/-/redux-3.7.2.tgz#06b73123215901d25d065be342eb026bc1c8537b" From 7f1bebf203bab8f1987e2fbcb9aa95181808775a Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sun, 24 Jun 2018 11:59:54 -0400 Subject: [PATCH 2/5] Move listeners into their own class --- assets/js/app.js | 12 +++--------- assets/js/services/notifications.js | 12 ++++++++---- assets/js/store/listeners.js | 27 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 assets/js/store/listeners.js diff --git a/assets/js/app.js b/assets/js/app.js index e7f5c6c..32a8f30 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -5,7 +5,6 @@ import "phoenix_html"; import React from "react"; import ReactDOM from "react-dom"; -import watch from "redux-watch"; import { createStore } from "redux"; import Channel from "./services/channel"; @@ -13,6 +12,7 @@ import Notifications from "./services/notifications"; import chessBoardReducer from "./reducers/chess-board"; import { setGameId } from "./store/actions"; +import Listeners from "./store/listeners"; import ChessBoard from "./components/chess-board"; import MoveList from "./components/move-list"; @@ -27,14 +27,8 @@ class App extends React.Component { store.dispatch(setGameId(gameId)); - let w = watch(store.getState, "turn"); - store.subscribe(w((newVal, oldVal, objectPath) => { - const player = store.getState().player; - - if (oldVal != null && newVal == player) { - notifications.notifyTurn(player); - } - })); + this.listeners = new Listeners(store); + this.listeners.setListeners(notifications); this.channel = new Channel(store, gameId); } diff --git a/assets/js/services/notifications.js b/assets/js/services/notifications.js index 27ae525..75376cf 100644 --- a/assets/js/services/notifications.js +++ b/assets/js/services/notifications.js @@ -4,11 +4,15 @@ class Notifications { } notifyTurn(player) { + this.notify({ + body: "Your opponent has moved.", + icon: `/images/king_${player}.svg`, + }); + } + + notify(options) { if (!document.hasFocus()) { - new Notification("Chess", { - body: "Your opponent has moved.", - icon: `/images/king_${player}.svg` - }); + new Notification("Chess", options); } } } diff --git a/assets/js/store/listeners.js b/assets/js/store/listeners.js new file mode 100644 index 0000000..d3f58c0 --- /dev/null +++ b/assets/js/store/listeners.js @@ -0,0 +1,27 @@ +import watch from "redux-watch"; + +class Listeners { + constructor(store) { + this.store = store; + } + + setListeners(notifications) { + this.notifications = notifications; + + let watcher = watch(this.store.getState, "turn"); + + this.store.subscribe( + watcher(this.notifyTurn.bind(this)) + ); + } + + notifyTurn(newVal, oldVal) { + const player = this.store.getState().player; + + if (oldVal != null && newVal == player) { + this.notifications.notifyTurn(player); + } + } +}; + +export default Listeners; From 30f701de2985c42c17bde34d85eb924fa87ca4d3 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" Date: Sat, 11 Aug 2018 23:18:20 +0000 Subject: [PATCH 3/5] Bump credo from 0.9.3 to 0.10.0 Bumps [credo](https://github.com/rrrene/credo) from 0.9.3 to 0.10.0. - [Release notes](https://github.com/rrrene/credo/releases) - [Changelog](https://github.com/rrrene/credo/blob/master/CHANGELOG.md) - [Commits](https://github.com/rrrene/credo/commits) Signed-off-by: dependabot[bot] --- mix.lock | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mix.lock b/mix.lock index b26b3d3..f03afb2 100644 --- a/mix.lock +++ b/mix.lock @@ -8,7 +8,7 @@ "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"}, "cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [:rebar3], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"}, "cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], [], "hexpm"}, - "credo": {:hex, :credo, "0.9.3", "76fa3e9e497ab282e0cf64b98a624aa11da702854c52c82db1bf24e54ab7c97a", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:poison, ">= 0.0.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"}, + "credo": {:hex, :credo, "0.10.0", "66234a95effaf9067edb19fc5d0cd5c6b461ad841baac42467afed96c78e5e9e", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"}, "db_connection": {:hex, :db_connection, "1.1.3", "89b30ca1ef0a3b469b1c779579590688561d586694a3ce8792985d4d7e575a61", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"}, "decimal": {:hex, :decimal, "1.5.0", "b0433a36d0e2430e3d50291b1c65f53c37d56f83665b43d79963684865beab68", [:mix], [], "hexpm"}, "distillery": {:hex, :distillery, "1.5.3", "b2f4fc34ec71ab4f1202a796f9290e068883b042319aa8c9aa45377ecac8597a", [:mix], [], "hexpm"}, @@ -22,6 +22,7 @@ "hackney": {:hex, :hackney, "1.12.1", "8bf2d0e11e722e533903fe126e14d6e7e94d9b7983ced595b75f532e04b7fdc7", [:rebar3], [{:certifi, "2.3.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"}, "httpoison": {:hex, :httpoison, "0.13.0", "bfaf44d9f133a6599886720f3937a7699466d23bb0cd7a88b6ba011f53c6f562", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"}, "idna": {:hex, :idna, "5.1.1", "cbc3b2fa1645113267cc59c760bafa64b2ea0334635ef06dbac8801e42f7279c", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"}, + "jason": {:hex, :jason, "1.1.1", "d3ccb840dfb06f2f90a6d335b536dd074db748b3e7f5b11ab61d239506585eb2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"}, "jose": {:hex, :jose, "1.8.4", "7946d1e5c03a76ac9ef42a6e6a20001d35987afd68c2107bcd8f01a84e75aa73", [:mix, :rebar3], [{:base64url, "~> 0.0.1", [hex: :base64url, repo: "hexpm", optional: false]}], "hexpm"}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"}, "mime": {:hex, :mime, "1.3.0", "5e8d45a39e95c650900d03f897fbf99ae04f60ab1daa4a34c7a20a5151b7a5fe", [:mix], [], "hexpm"}, From 590303e29d2c7139f84137d587834fdb60680f0d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" Date: Sat, 11 Aug 2018 23:20:59 +0000 Subject: [PATCH 4/5] Bump phoenix_html from 2.11.2 to 2.12.0 Bumps [phoenix_html](https://github.com/phoenixframework/phoenix_html) from 2.11.2 to 2.12.0. - [Release notes](https://github.com/phoenixframework/phoenix_html/releases) - [Changelog](https://github.com/phoenixframework/phoenix_html/blob/master/CHANGELOG.md) - [Commits](https://github.com/phoenixframework/phoenix_html/compare/v2.11.2...v2.12.0) Signed-off-by: dependabot[bot] --- mix.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mix.lock b/mix.lock index f03afb2..0d5fc10 100644 --- a/mix.lock +++ b/mix.lock @@ -30,10 +30,10 @@ "parse_trans": {:hex, :parse_trans, "3.2.0", "2adfa4daf80c14dc36f522cf190eb5c4ee3e28008fc6394397c16f62a26258c2", [:rebar3], [], "hexpm"}, "phoenix": {:hex, :phoenix, "1.3.3", "bafb5fa408d202e8d9f739e781bdb908446a2c1c1e00797c1158918ed55566a4", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"}, "phoenix_ecto": {:hex, :phoenix_ecto, "3.3.0", "702f6e164512853d29f9d20763493f2b3bcfcb44f118af2bc37bb95d0801b480", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"}, - "phoenix_html": {:hex, :phoenix_html, "2.11.2", "86ebd768258ba60a27f5578bec83095bdb93485d646fc4111db8844c316602d6", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"}, + "phoenix_html": {:hex, :phoenix_html, "2.12.0", "1fb3c2e48b4b66d75564d8d63df6d53655469216d6b553e7e14ced2b46f97622", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"}, "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.1.5", "8d4c9b1ef9ca82deee6deb5a038d6d8d7b34b9bb909d99784a49332e0d15b3dc", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.0 or ~> 1.2 or ~> 1.3", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm"}, "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.2", "bfa7fd52788b5eaa09cb51ff9fcad1d9edfeb68251add458523f839392f034c1", [:mix], [], "hexpm"}, - "plug": {:hex, :plug, "1.6.0", "90d338a44c8cd762c32d3ea324f6728445c6145b51895403854b77f1536f1617", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"}, + "plug": {:hex, :plug, "1.6.2", "e06a7bd2bb6de5145da0dd950070110dce88045351224bd98e84edfdaaf5ffee", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"}, "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}, "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm"}, "postgrex": {:hex, :postgrex, "0.13.5", "3d931aba29363e1443da167a4b12f06dcd171103c424de15e5f3fc2ba3e6d9c5", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm"}, From 04ae9b5e68aaff97a651fbab73662132d74f9fab Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" Date: Mon, 13 Aug 2018 14:10:38 +0000 Subject: [PATCH 5/5] Bump phoenix from 1.3.3 to 1.3.4 Bumps [phoenix](https://github.com/phoenixframework/phoenix) from 1.3.3 to 1.3.4. - [Release notes](https://github.com/phoenixframework/phoenix/releases) - [Changelog](https://github.com/phoenixframework/phoenix/blob/master/CHANGELOG.md) - [Commits](https://github.com/phoenixframework/phoenix/compare/v1.3.3...v1.3.4) Signed-off-by: dependabot[bot] --- mix.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mix.lock b/mix.lock index 0d5fc10..3069382 100644 --- a/mix.lock +++ b/mix.lock @@ -28,11 +28,11 @@ "mime": {:hex, :mime, "1.3.0", "5e8d45a39e95c650900d03f897fbf99ae04f60ab1daa4a34c7a20a5151b7a5fe", [:mix], [], "hexpm"}, "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"}, "parse_trans": {:hex, :parse_trans, "3.2.0", "2adfa4daf80c14dc36f522cf190eb5c4ee3e28008fc6394397c16f62a26258c2", [:rebar3], [], "hexpm"}, - "phoenix": {:hex, :phoenix, "1.3.3", "bafb5fa408d202e8d9f739e781bdb908446a2c1c1e00797c1158918ed55566a4", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"}, + "phoenix": {:hex, :phoenix, "1.3.4", "aaa1b55e5523083a877bcbe9886d9ee180bf2c8754905323493c2ac325903dc5", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"}, "phoenix_ecto": {:hex, :phoenix_ecto, "3.3.0", "702f6e164512853d29f9d20763493f2b3bcfcb44f118af2bc37bb95d0801b480", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"}, "phoenix_html": {:hex, :phoenix_html, "2.12.0", "1fb3c2e48b4b66d75564d8d63df6d53655469216d6b553e7e14ced2b46f97622", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"}, "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.1.5", "8d4c9b1ef9ca82deee6deb5a038d6d8d7b34b9bb909d99784a49332e0d15b3dc", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.0 or ~> 1.2 or ~> 1.3", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm"}, - "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.2", "bfa7fd52788b5eaa09cb51ff9fcad1d9edfeb68251add458523f839392f034c1", [:mix], [], "hexpm"}, + "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.1.0", "d55e25ff1ff8ea2f9964638366dfd6e361c52dedfd50019353598d11d4441d14", [:mix], [], "hexpm"}, "plug": {:hex, :plug, "1.6.2", "e06a7bd2bb6de5145da0dd950070110dce88045351224bd98e84edfdaaf5ffee", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"}, "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}, "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm"},