Skip to content

Instantly share code, notes, and snippets.

@mdarby

mdarby/diff.diff Secret

Last active October 11, 2019 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdarby/de3c205e9b4e9ca34812e7a72e4dc1ec to your computer and use it in GitHub Desktop.
Save mdarby/de3c205e9b4e9ca34812e7a72e4dc1ec to your computer and use it in GitHub Desktop.
diff --git c/assets/babel.config.js w/assets/babel.config.js
index d5df645..3e7037a 100644
--- c/assets/babel.config.js
+++ w/assets/babel.config.js
@@ -5,8 +5,7 @@ const presets = [
firefox: "60",
chrome: "67",
safari: "11.1"
- },
- useBuiltIns: "usage"
+ }
}],
["@babel/preset-react"]
];
diff --git c/assets/js/app.js w/assets/js/app.js
index f0e544c..6220dd7 100644
--- c/assets/js/app.js
+++ w/assets/js/app.js
@@ -1,2 +1,19 @@
// Import UI dependencies
import 'bootstrap';
+
+import { Socket } from "phoenix";
+import { LiveSocket } from "phoenix_live_view";
+
+let logger = function(kind, msg, data) {
+ console.log(`${kind}: ${msg}`, data)
+};
+
+let liveSocket = new LiveSocket("/live", Socket, { logger: logger });
+liveSocket.connect();
+
+window.liveSocket = liveSocket;
\ No newline at end of file
diff --git c/assets/webpack.base.js w/assets/webpack.base.js
index 441d156..2262d55 100644
--- c/assets/webpack.base.js
+++ w/assets/webpack.base.js
@@ -26,16 +26,14 @@ module.exports = (options) => ({
filename: 'js/[name].js',
chunkFilename: 'js/[name].chunk.js'
},
-
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
- loader: 'babel-loader',
- options: options.babelQuery,
- },
+ loader: 'babel-loader'
+ }
},
{
// Preprocess our own .scss files
@@ -153,4 +151,3 @@ module.exports = (options) => ({
}
}
});
-
diff --git c/config/config.exs w/config/config.exs
index e51e490..787c49e 100644
--- c/config/config.exs
+++ w/config/config.exs
@@ -17,8 +17,8 @@ config :groot, FooWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "asdf",
render_errors: [view: FooWeb.ErrorView, accepts: ~w(html json)],
- pubsub: [name: Foo.PubSub,
- adapter: Phoenix.PubSub.PG2]
+ pubsub: [name: Foo.PubSub, adapter: Phoenix.PubSub.PG2],
+ live_view: [signing_salt: "HUGE SALT STRING, NO REALLY"]
# Configures Elixir's Logger
config :logger, :console,
diff --git c/config/dev.exs w/config/dev.exs
index ae958a3..a533f15 100644
--- c/config/dev.exs
+++ w/config/dev.exs
@@ -37,12 +37,13 @@ config :groot, FooWeb.Endpoint,
~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$},
~r{priv/gettext/.*(po)$},
~r{lib/groot_web/views/.*(ex)$},
- ~r{lib/groot_web/templates/.*(eex)$}
+ ~r{lib/groot_web/templates/.*(eex)$},
+ ~r{lib/my_app_web/live/.*(ex)$}
]
]
# Do not include metadata nor timestamps in development logs
-config :logger, :console, format: "[$level] $message\n"
+config :logger, :console, format: "[$level] $message\n", level: :debug
# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
diff --git c/lib/groot_web.ex w/lib/groot_web.ex
index 54b8d38..69e5a91 100644
--- c/lib/groot_web.ex
+++ w/lib/groot_web.ex
@@ -24,6 +24,7 @@ defmodule FooWeb do
import FooWeb.Gettext
alias FooWeb.Router.Helpers, as: Routes
+ import Phoenix.LiveView.Controller
end
end
@@ -44,6 +45,8 @@ defmodule FooWeb do
import FooWeb.Gettext
alias FooWeb.Router.Helpers, as: Routes
+
+ import Phoenix.LiveView #, only: [live_render: 2, live_render: 3]
end
end
@@ -52,6 +55,7 @@ defmodule FooWeb do
use Phoenix.Router
import Plug.Conn
import Phoenix.Controller
+ import Phoenix.LiveView.Router
end
end
diff --git c/lib/groot_web/endpoint.ex w/lib/groot_web/endpoint.ex
index 7224599..13fba13 100644
--- c/lib/groot_web/endpoint.ex
+++ w/lib/groot_web/endpoint.ex
@@ -3,6 +3,7 @@ defmodule FooWeb.Endpoint do
use Sentry.Phoenix.Endpoint
#socket "/socket", FooWeb.UserSocket
+ socket "/live", Phoenix.LiveView.Socket
# Serve at "/" the static files from "priv/static" directory.
#
diff --git c/lib/groot_web/live/password_strength_live.ex w/lib/groot_web/live/password_strength_live.ex
new file mode 100644
index 0000000..92a2df7
--- /dev/null
+++ w/lib/groot_web/live/password_strength_live.ex
@@ -0,0 +1,42 @@
+defmodule FooWeb.PasswordStrengthLive do
+ use Phoenix.LiveView
+ require Logger
+
+ def render(assigns) do
+ ~L"""
+ <div class="">
+ <div>
+ <%= @deploy_step %>
+ </div>
+ </div>
+ """
+ end
+
+ def mount(_session, socket) do
+ Logger.warn("------------------")
+ Logger.warn("Mounted")
+ Logger.warn("------------------")
+ {:ok, assign(socket, deploy_step: "Ready!")}
+ end
+
+ def handle_event("validate", _params, socket) do
+ Logger.warn("------------------")
+ Logger.warn("VALIDATE")
+ Logger.warn("------------------")
+ {:noreply, assign(socket, deploy_step: "Changed")}
+ end
+end
+
+
+
+# Form has correct phx_change trigger at top level
+# Can manually send events to the backend and receive UI update correctly
+ # window.liveSocket.views[""].pushWithReply("event", {type: "form", event: "validate", value: "country=IT"});
+# Updating a text field triggers no events
+# Adding phx_change handler directly to input does nothing on update
+# If I add @babel/preset-env to webpack I get a clean transpile
+# If I add phoenix_live_view to package.json I get webpack errors re: core-js
+# I created a new app with this config and it worked
+# Adding a button with a phx_click handler does not work
+# I didn't have to add the preset-env in the sample app.
+# Either present-env is silently swallowing errors or it's just not working.
\ No newline at end of file
diff --git c/lib/groot_web/router.ex w/lib/groot_web/router.ex
index 15ce079..e25ae25 100644
--- c/lib/groot_web/router.ex
+++ w/lib/groot_web/router.ex
@@ -7,6 +7,7 @@ defmodule FooWeb.Router do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
+ plug Phoenix.LiveView.Flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug Plug.Logger
diff --git c/lib/groot_web/templates/reset_password/index.html.eex w/lib/groot_web/templates/reset_password/index.html.eex
index dcd8bc4..48501ef 100644
--- c/lib/groot_web/templates/reset_password/index.html.eex
+++ w/lib/groot_web/templates/reset_password/index.html.eex
@@ -1,15 +1,18 @@
+<div class="center-form">
+ <%= render_one get_flash(@conn, :error), FooWeb.LayoutView, "_error.html", as: :error %>
- <div class="center-form">
- <%= render_one get_flash(@conn, :error), FooWeb.LayoutView, "_error.html", as: :error %>
+ <div class="form mb-5">
+ <h2>Reset Password</h2>
- <div class="form mb-5">
- <h2>Reset Password</h2>
-
- <%= form_for @conn, Routes.reset_password_path(@conn, :create), [as: :reset_password], fn f -> %>
- <%= password_input f, :password, class: "form-control mb-2", placeholder: "New Password" %>
- <%= password_input f, :password_confirm, class: "form-control mb-3", placeholder: "Confirm New Password" %>
- <%= hidden_input f, :reset_token, value: @reset_token %>
- <%= submit "Reset Password", class: "g-btn" %>
- <% end %>
- </div>
+ <%= form_for @conn, Routes.reset_password_path(@conn, :create), [as: :reset_password, phx_change: :validate, data_phx_bound: true], fn f -> %>
+ <%= password_input f, :password, class: "form-control mb-2", placeholder: "New Password", value: input_value(f, :password) %>
+ <%= password_input f, :password_confirm, class: "form-control mb-3", placeholder: "Confirm New Password", value: input_value(f, :password_confirmation) %>
+ <%= hidden_input f, :reset_token, value: @reset_token %>
+ <%= submit "Reset Password", class: "g-btn" %>
+ <% end %>
</div>
+
+ <button phx-click="validate">HERE</button>
+
+ <%= live_render(@conn, FooWeb.PasswordStrengthLive) %>
+</div>
\ No newline at end of file
diff --git c/mix.exs w/mix.exs
index 03135dd..6e76650 100644
--- c/mix.exs
+++ w/mix.exs
@@ -56,6 +56,7 @@ defmodule Foo.Mixfile do
{:phoenix_ecto, "~> 4.0"},
{:phoenix_html, "~> 2.10"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
+ {:phoenix_live_view, "~> 0.3.0"},
{:phoenix_pubsub, "~> 1.0"},
{:plug, "~> 1.8"},
{:plug_cowboy, "~> 2.0"},
diff --git c/mix.lock w/mix.lock
index 2aa0acf..3dee5b5 100644
--- c/mix.lock
+++ w/mix.lock
@@ -39,6 +39,7 @@
"phoenix_ecto": {:hex, :phoenix_ecto, "4.0.0", "c43117a136e7399ea04ecaac73f8f23ee0ffe3e07acfcb8062fe5f4c9f0f6531", [:mix], [{:ecto, "~> 3.0", [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.13.3", "850e292ff6e204257f5f9c4c54a8cb1f6fbc16ed53d360c2b780a3d0ba333867", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.2.1", "274a4b07c4adbdd7785d45a8b0bb57634d0b4f45b18d2c508b26c0344bd59b8f", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm"},
+ "phoenix_live_view": {:hex, :phoenix_live_view, "0.3.1", "5474c9e70db4e5bb23c1d1200d9a119ee76b927f3352554d31f5f31eaa1ea568", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.4.9", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.13.2", [hex: :phoenix_html, repo: "hexpm", optional: false]}], "hexpm"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "1.1.2", "496c303bdf1b2e98a9d26e89af5bba3ab487ba3a3735f74bf1f4064d2a845a3e", [:mix], [], "hexpm"},
"plug": {:hex, :plug, "1.8.3", "12d5f9796dc72e8ac9614e94bda5e51c4c028d0d428e9297650d09e15a684478", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm"},
"plug_cowboy": {:hex, :plug_cowboy, "2.1.0", "b75768153c3a8a9e8039d4b25bb9b14efbc58e9c4a6e6a270abff1cd30cbe320", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
@mdarby
Copy link
Author

mdarby commented Oct 11, 2019

Note that package.json has been removed. I have confirmed that phoenix_live_view.js is being bundled via Webpack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment