From 19078f2fe0715077bbc5e2dcd9ad50d9bc885f4c Mon Sep 17 00:00:00 2001 From: Guillaume Cauchon Date: Fri, 8 Sep 2023 15:37:27 -0400 Subject: [PATCH] Refactor ElixirBoilerplateWeb to segment in each components in separate modules --- assets/js/app.js | 2 - .../api/version/controller.ex | 2 +- lib/elixir_boilerplate_web/component.ex | 9 ++ lib/elixir_boilerplate_web/controller.ex | 15 ++++ .../elixir_boilerplate_web.ex | 86 +------------------ .../errors/error_html.ex | 2 +- lib/elixir_boilerplate_web/home/html.ex | 4 +- lib/elixir_boilerplate_web/home/live.ex | 2 +- .../home/templates/index.html.heex | 2 +- .../home/templates/index_live.html.heex | 2 +- lib/elixir_boilerplate_web/html.ex | 14 +++ lib/elixir_boilerplate_web/layouts/layouts.ex | 2 +- lib/elixir_boilerplate_web/live_view.ex | 9 ++ lib/elixir_boilerplate_web/router.ex | 3 + mix.exs | 2 +- 15 files changed, 62 insertions(+), 94 deletions(-) create mode 100644 lib/elixir_boilerplate_web/component.ex create mode 100644 lib/elixir_boilerplate_web/controller.ex create mode 100644 lib/elixir_boilerplate_web/html.ex create mode 100644 lib/elixir_boilerplate_web/live_view.ex diff --git a/assets/js/app.js b/assets/js/app.js index 58f14bdd..47bee88f 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -1,8 +1,6 @@ // Include phoenix_html to handle method=PUT/DELETE in forms and buttons. import 'phoenix_html'; -const DELAY_IN_MILISECONDS = 200; - // Establish Phoenix Socket and LiveView configuration. import {Socket} from 'phoenix'; import {LiveSocket} from 'phoenix_live_view'; diff --git a/lib/elixir_boilerplate_web/api/version/controller.ex b/lib/elixir_boilerplate_web/api/version/controller.ex index fc1fabd1..b5d4e357 100644 --- a/lib/elixir_boilerplate_web/api/version/controller.ex +++ b/lib/elixir_boilerplate_web/api/version/controller.ex @@ -1,5 +1,5 @@ defmodule ElixirBoilerplateWeb.Api.Version.Controller do - use ElixirBoilerplateWeb, :controller + use ElixirBoilerplateWeb.Controller @spec index(Plug.Conn.t(), map) :: Plug.Conn.t() def index(conn, _) do diff --git a/lib/elixir_boilerplate_web/component.ex b/lib/elixir_boilerplate_web/component.ex new file mode 100644 index 00000000..342c81c0 --- /dev/null +++ b/lib/elixir_boilerplate_web/component.ex @@ -0,0 +1,9 @@ +defmodule ElixirBoilerplateWeb.Component do + defmacro __using__(_opts) do + quote do + use Phoenix.LiveComponent + + unquote(ElixirBoilerplateWeb.html_helpers()) + end + end +end diff --git a/lib/elixir_boilerplate_web/controller.ex b/lib/elixir_boilerplate_web/controller.ex new file mode 100644 index 00000000..886af04a --- /dev/null +++ b/lib/elixir_boilerplate_web/controller.ex @@ -0,0 +1,15 @@ +defmodule ElixirBoilerplateWeb.Controller do + defmacro __using__(_opts) do + quote do + use Phoenix.Controller, + namespace: ElixirBoilerplateWeb, + formats: [:html, :json], + layouts: [html: ElixirBoilerplateWeb.Layouts] + + import Plug.Conn + import ElixirBoilerplate.Gettext + + unquote(ElixirBoilerplateWeb.verified_routes()) + end + end +end diff --git a/lib/elixir_boilerplate_web/elixir_boilerplate_web.ex b/lib/elixir_boilerplate_web/elixir_boilerplate_web.ex index 85adc10c..fb1645d3 100644 --- a/lib/elixir_boilerplate_web/elixir_boilerplate_web.ex +++ b/lib/elixir_boilerplate_web/elixir_boilerplate_web.ex @@ -1,88 +1,13 @@ defmodule ElixirBoilerplateWeb do - @moduledoc """ - The entrypoint for defining your web interface, such - as controllers, components, channels, and so on. - This can be used in your application as: - use ElixirBoilerplateWeb, :controller - use ElixirBoilerplateWeb, :html - The definitions below will be executed for every controller, - component, etc, so keep them short and clean, focused - on imports, uses and aliases. - Do NOT define functions inside the quoted expressions - below. Instead, define additional modules and import - those modules here. - """ - def static_paths, do: ~w(assets fonts images favicon.ico robots.txt) - def router do - quote do - use Phoenix.Router, helpers: false - - # Import common connection and controller functions to use in pipelines - import Plug.Conn - import Phoenix.Controller - import Phoenix.LiveView.Router - end - end - - def channel do - quote do - use Phoenix.Channel - end - end - - def controller do - quote do - use Phoenix.Controller, - namespace: ElixirBoilerplateWeb, - formats: [:html, :json], - layouts: [html: ElixirBoilerplateWeb.Layouts] - - import Plug.Conn - import ElixirBoilerplate.Gettext - - unquote(verified_routes()) - end - end - - def live_view do - quote do - use Phoenix.LiveView, - layout: {ElixirBoilerplateWeb.Layouts, :live} - - unquote(html_helpers()) - end - end - - def live_component do - quote do - use Phoenix.LiveComponent - - unquote(html_helpers()) - end - end - - def html do - quote do - use Phoenix.Component - - # Import convenience functions from controllers - import Phoenix.Controller, - only: [get_csrf_token: 0, view_module: 1, view_template: 1] - - # Include general helpers for rendering HTML - unquote(html_helpers()) - end - end - - defp html_helpers do + def html_helpers do quote do # HTML escaping functionality import Phoenix.HTML + # Core UI components and translation import ElixirBoilerplate.Gettext - import ElixirBoilerplateWeb.Components.Branding import ElixirBoilerplateWeb.Components.Core # Shortcut for generating JS commands @@ -101,11 +26,4 @@ defmodule ElixirBoilerplateWeb do statics: ElixirBoilerplateWeb.static_paths() end end - - @doc """ - When used, dispatch to the appropriate controller/view/etc. - """ - defmacro __using__(which) when is_atom(which) do - apply(__MODULE__, which, []) - end end diff --git a/lib/elixir_boilerplate_web/errors/error_html.ex b/lib/elixir_boilerplate_web/errors/error_html.ex index ed649f2d..54288506 100644 --- a/lib/elixir_boilerplate_web/errors/error_html.ex +++ b/lib/elixir_boilerplate_web/errors/error_html.ex @@ -1,5 +1,5 @@ defmodule ElixirBoilerplateWeb.Errors.ErrorHTML do - use ElixirBoilerplateWeb, :html + use ElixirBoilerplateWeb.HTML # If you want to customize your error pages, # uncomment the embed_templates/1 call below diff --git a/lib/elixir_boilerplate_web/home/html.ex b/lib/elixir_boilerplate_web/home/html.ex index b0834983..fa7493dd 100644 --- a/lib/elixir_boilerplate_web/home/html.ex +++ b/lib/elixir_boilerplate_web/home/html.ex @@ -1,5 +1,7 @@ defmodule ElixirBoilerplateWeb.Home.HTML do - use ElixirBoilerplateWeb, :html + use ElixirBoilerplateWeb.HTML + + alias ElixirBoilerplateWeb.Components.Branding embed_templates("templates/*") diff --git a/lib/elixir_boilerplate_web/home/live.ex b/lib/elixir_boilerplate_web/home/live.ex index 11b294c6..0e649a82 100644 --- a/lib/elixir_boilerplate_web/home/live.ex +++ b/lib/elixir_boilerplate_web/home/live.ex @@ -1,5 +1,5 @@ defmodule ElixirBoilerplateWeb.Home.Live do - use ElixirBoilerplateWeb, :live_view + use ElixirBoilerplateWeb.LiveView def mount(_, _, socket) do socket = assign(socket, :message, "Hello, world!") diff --git a/lib/elixir_boilerplate_web/home/templates/index.html.heex b/lib/elixir_boilerplate_web/home/templates/index.html.heex index 9c732571..432ac231 100644 --- a/lib/elixir_boilerplate_web/home/templates/index.html.heex +++ b/lib/elixir_boilerplate_web/home/templates/index.html.heex @@ -4,7 +4,7 @@
- <.logo width={500} /> +

diff --git a/lib/elixir_boilerplate_web/home/templates/index_live.html.heex b/lib/elixir_boilerplate_web/home/templates/index_live.html.heex index 8848fb71..203d3f9f 100644 --- a/lib/elixir_boilerplate_web/home/templates/index_live.html.heex +++ b/lib/elixir_boilerplate_web/home/templates/index_live.html.heex @@ -4,7 +4,7 @@

- <.logo width={500} /> +

diff --git a/lib/elixir_boilerplate_web/html.ex b/lib/elixir_boilerplate_web/html.ex new file mode 100644 index 00000000..44c19ff9 --- /dev/null +++ b/lib/elixir_boilerplate_web/html.ex @@ -0,0 +1,14 @@ +defmodule ElixirBoilerplateWeb.HTML do + defmacro __using__(_opts) do + quote do + use Phoenix.Component + + # Import convenience functions from controllers + import Phoenix.Controller, + only: [get_csrf_token: 0, view_module: 1, view_template: 1] + + # Include general helpers for rendering HTML + unquote(ElixirBoilerplateWeb.html_helpers()) + end + end +end diff --git a/lib/elixir_boilerplate_web/layouts/layouts.ex b/lib/elixir_boilerplate_web/layouts/layouts.ex index d3c6f328..917f05b6 100644 --- a/lib/elixir_boilerplate_web/layouts/layouts.ex +++ b/lib/elixir_boilerplate_web/layouts/layouts.ex @@ -1,5 +1,5 @@ defmodule ElixirBoilerplateWeb.Layouts do - use ElixirBoilerplateWeb, :html + use ElixirBoilerplateWeb.HTML embed_templates("templates/*") diff --git a/lib/elixir_boilerplate_web/live_view.ex b/lib/elixir_boilerplate_web/live_view.ex new file mode 100644 index 00000000..4c3b211a --- /dev/null +++ b/lib/elixir_boilerplate_web/live_view.ex @@ -0,0 +1,9 @@ +defmodule ElixirBoilerplateWeb.LiveView do + defmacro __using__(_opts) do + quote do + use Phoenix.LiveView, layout: {ElixirBoilerplateWeb.Layouts, :live} + + unquote(ElixirBoilerplateWeb.html_helpers()) + end + end +end diff --git a/lib/elixir_boilerplate_web/router.ex b/lib/elixir_boilerplate_web/router.ex index e99b4bc2..8a13c11a 100644 --- a/lib/elixir_boilerplate_web/router.ex +++ b/lib/elixir_boilerplate_web/router.ex @@ -1,6 +1,9 @@ defmodule ElixirBoilerplateWeb.Router do use Phoenix.Router, helpers: false + # Import common connection and controller functions to use in pipelines + import Plug.Conn + import Phoenix.Controller import Phoenix.LiveView.Router pipeline :browser do diff --git a/mix.exs b/mix.exs index d7ef111d..155b248c 100644 --- a/mix.exs +++ b/mix.exs @@ -104,7 +104,7 @@ defmodule ElixirBoilerplate.Mixfile do {:excoveralls, "~> 0.16", only: :test}, # Dialyzer - {:dialyxir, "~> 1.3", only: [:dev, :test], runtime: false}, + {:dialyxir, "~> 1.3", only: [:dev, :test], runtime: false} ] end