From a06b7a93674c883b4191b28939e4ff2548240d7a Mon Sep 17 00:00:00 2001 From: Ryan Pandya Date: Fri, 21 Oct 2022 20:08:52 -0700 Subject: [PATCH] Semi working --- friends/lib/friends_web.ex | 114 +++++++++++++++++- friends/lib/friends_web/live/friends_live.ex | 4 + friends/lib/friends_web/live/index.ex | 22 ++++ friends/lib/friends_web/live/index.html.heex | 18 +++ friends/lib/friends_web/router.ex | 4 +- .../templates/layout/live.html.heex | 62 ++++++++-- .../templates/layout/root.html.heex | 24 +--- .../templates/page/index.html.heex | 41 ------- friends/mix.exs | 4 +- friends/mix.lock | 4 +- 10 files changed, 221 insertions(+), 76 deletions(-) create mode 100644 friends/lib/friends_web/live/friends_live.ex create mode 100644 friends/lib/friends_web/live/index.ex create mode 100644 friends/lib/friends_web/live/index.html.heex delete mode 100644 friends/lib/friends_web/templates/page/index.html.heex diff --git a/friends/lib/friends_web.ex b/friends/lib/friends_web.ex index 0d769ee..ccfbcbd 100644 --- a/friends/lib/friends_web.ex +++ b/friends/lib/friends_web.ex @@ -37,6 +37,7 @@ defmodule FriendsWeb do import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1] + import Phoenix.Component # Include shared imports and aliases for views unquote(view_helpers()) end @@ -70,7 +71,118 @@ defmodule FriendsWeb do def router do quote do use Phoenix.Router - + import Phoenix.Component + import Plug.Conn + import Phoenix.Controller + import Phoenix.LiveView.Router + end + end + + def channel do + quote do + use Phoenix.Channel + import FriendsWeb.Gettext + end + end + + defp view_helpers do + quote do + # Use all HTML functionality (forms, tags, etc) + use Phoenix.HTML + + # Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc) + import Phoenix.LiveView.Helpers + + # Import basic rendering functionality (render, render_layout, etc) + import Phoenix.View + + import FriendsWeb.ErrorHelpers + import FriendsWeb.Gettext + alias FriendsWeb.Router.Helpers, as: Routes + 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 +defmodule FriendsWeb do + @moduledoc """ + The entrypoint for defining your web interface, such + as controllers, views, channels and so on. + + This can be used in your application as: + + use FriendsWeb, :controller + use FriendsWeb, :view + + The definitions below will be executed for every view, + controller, etc, so keep them short and clean, focused + on imports, uses and aliases. + + Do NOT define functions inside the quoted expressions + below. Instead, define any helper function in modules + and import those modules here. + """ + + def controller do + quote do + use Phoenix.Controller, namespace: FriendsWeb + + import Plug.Conn + import FriendsWeb.Gettext + alias FriendsWeb.Router.Helpers, as: Routes + end + end + + def view do + quote do + use Phoenix.View, + root: "lib/friends_web/templates", + namespace: FriendsWeb + + # Import convenience functions from controllers + import Phoenix.Controller, + only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1] + + import Phoenix.Component + # Include shared imports and aliases for views + unquote(view_helpers()) + end + end + + def live_view do + quote do + use Phoenix.LiveView, + layout: {FriendsWeb.LayoutView, "live.html"} + + unquote(view_helpers()) + end + end + + def live_component do + quote do + use Phoenix.LiveComponent + + unquote(view_helpers()) + end + end + + def component do + quote do + use Phoenix.Component + + unquote(view_helpers()) + end + end + + def router do + quote do + use Phoenix.Router + import Phoenix.Component import Plug.Conn import Phoenix.Controller import Phoenix.LiveView.Router diff --git a/friends/lib/friends_web/live/friends_live.ex b/friends/lib/friends_web/live/friends_live.ex new file mode 100644 index 0000000..90267b6 --- /dev/null +++ b/friends/lib/friends_web/live/friends_live.ex @@ -0,0 +1,4 @@ +defmodule FriendsWeb.FriendsLive do + use FriendsWeb, :live_view + +end diff --git a/friends/lib/friends_web/live/index.ex b/friends/lib/friends_web/live/index.ex new file mode 100644 index 0000000..483247f --- /dev/null +++ b/friends/lib/friends_web/live/index.ex @@ -0,0 +1,22 @@ +defmodule FriendsWeb.FriendsLive.Index do + use FriendsWeb, :live_view + import Helpers + + alias Friends.{Friend, Relationship} + + def mount(%{}, _token, socket) do + {:ok, + socket + |> assign(:friends, Friend.all) + |> assign( + :new_friend, + %Friend{}|> Friend.changeset() + ) + |> assign( + :page_title, + "Welcome" + ) + } + end + +end diff --git a/friends/lib/friends_web/live/index.html.heex b/friends/lib/friends_web/live/index.html.heex new file mode 100644 index 0000000..74c76a2 --- /dev/null +++ b/friends/lib/friends_web/live/index.html.heex @@ -0,0 +1,18 @@ +
+
+

All Friends

+
    + <%= for friend <- @friends do %> +
  • +
    + <.link navigate={"/friend/#{friend.slug}"}><%= friend.name %> + <%= pluralize(friend |> relations |> length, "relation") %> +
    +
  • + <% end %> +
+
+ <.link patch={"/friends/new"} class="btn btn-block md:btn-wide text-white">add friend +
+
+
\ No newline at end of file diff --git a/friends/lib/friends_web/router.ex b/friends/lib/friends_web/router.ex index 46f3b14..d76c194 100644 --- a/friends/lib/friends_web/router.ex +++ b/friends/lib/friends_web/router.ex @@ -17,7 +17,9 @@ defmodule FriendsWeb.Router do scope "/", FriendsWeb do pipe_through :browser - get "/", PageController, :index + live "/", FriendsLive.Index + live "/friend/:slug", FriendsLive.Show + end # Other scopes may use custom stacks. diff --git a/friends/lib/friends_web/templates/layout/live.html.heex b/friends/lib/friends_web/templates/layout/live.html.heex index a29d604..9bb672b 100644 --- a/friends/lib/friends_web/templates/layout/live.html.heex +++ b/friends/lib/friends_web/templates/layout/live.html.heex @@ -1,11 +1,51 @@ -
- - - - - <%= @inner_content %> -
+ +
+
+ + + <%= @inner_content %> +
+
+
+ + + +
\ No newline at end of file diff --git a/friends/lib/friends_web/templates/layout/root.html.heex b/friends/lib/friends_web/templates/layout/root.html.heex index 22827b5..385e187 100644 --- a/friends/lib/friends_web/templates/layout/root.html.heex +++ b/friends/lib/friends_web/templates/layout/root.html.heex @@ -1,30 +1,18 @@ - + - <%= live_title_tag assigns[:page_title] || "Friends", suffix: " · Phoenix Framework" %> + <%= live_title_tag assigns[:page_title] || "Welcome", suffix: " · Friends" %> - -
-
- - -
-
+ <%= @inner_content %> + + + diff --git a/friends/lib/friends_web/templates/page/index.html.heex b/friends/lib/friends_web/templates/page/index.html.heex deleted file mode 100644 index f844bd8..0000000 --- a/friends/lib/friends_web/templates/page/index.html.heex +++ /dev/null @@ -1,41 +0,0 @@ -
-

<%= gettext "Welcome to %{name}!", name: "Phoenix" %>

-

Peace of mind from prototype to production

-
- -
- - -
diff --git a/friends/mix.exs b/friends/mix.exs index 28cd5ed..d690303 100644 --- a/friends/mix.exs +++ b/friends/mix.exs @@ -39,9 +39,9 @@ defmodule Friends.MixProject do {:postgrex, ">= 0.0.0"}, {:phoenix_html, "~> 3.0"}, {:phoenix_live_reload, "~> 1.2", only: :dev}, - {:phoenix_live_view, "~> 0.17.5"}, + {:phoenix_live_view, "~> 0.18"}, {:floki, ">= 0.30.0", only: :test}, - {:phoenix_live_dashboard, "~> 0.6"}, + {:phoenix_live_dashboard, "~> 0.7"}, {:esbuild, "~> 0.4", runtime: Mix.env() == :dev}, {:swoosh, "~> 1.3"}, {:telemetry_metrics, "~> 0.6"}, diff --git a/friends/mix.lock b/friends/mix.lock index c70ba33..16287eb 100644 --- a/friends/mix.lock +++ b/friends/mix.lock @@ -29,9 +29,9 @@ "phoenix": {:hex, :phoenix, "1.6.14", "57678366dc1d5bad49832a0fc7f12c2830c10d3eacfad681bfe9602cd4445f04", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d48c0da00b3d4cd1aad6055387917491af9f6e1f1e96cedf6c6b7998df9dba26"}, "phoenix_ecto": {:hex, :phoenix_ecto, "4.4.0", "0672ed4e4808b3fbed494dded89958e22fb882de47a97634c0b13e7b0b5f7720", [:mix], [{:ecto, "~> 3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "09864e558ed31ee00bd48fcc1d4fc58ae9678c9e81649075431e69dbabb43cc1"}, "phoenix_html": {:hex, :phoenix_html, "3.2.0", "1c1219d4b6cb22ac72f12f73dc5fad6c7563104d083f711c3fcd8551a1f4ae11", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "36ec97ba56d25c0136ef1992c37957e4246b649d620958a1f9fa86165f8bc54f"}, - "phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.6.5", "1495bb014be12c9a9252eca04b9af54246f6b5c1e4cd1f30210cd00ec540cf8e", [:mix], [{:ecto, "~> 3.6.2 or ~> 3.7", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_mysql_extras, "~> 0.3", [hex: :ecto_mysql_extras, repo: "hexpm", optional: true]}, {:ecto_psql_extras, "~> 0.7", [hex: :ecto_psql_extras, repo: "hexpm", optional: true]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.17.7", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "ef4fa50dd78364409039c99cf6f98ab5209b4c5f8796c17f4db118324f0db852"}, + "phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.7.1", "b0bf8f3348dec4910907a2ad1453e642f6fe4d444376c1c9b26222d63c73cf97", [:mix], [{:ecto, "~> 3.6.2 or ~> 3.7", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_mysql_extras, "~> 0.5", [hex: :ecto_mysql_extras, repo: "hexpm", optional: true]}, {:ecto_psql_extras, "~> 0.7", [hex: :ecto_psql_extras, repo: "hexpm", optional: true]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.18.0", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6 or ~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "b6c5d744bf4b40692b1b361d3608bdfd05aeab83e17c7bc217d730f007f31abf"}, "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.3.3", "3a53772a6118d5679bf50fc1670505a290e32a1d195df9e069d8c53ab040c054", [: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", "766796676e5f558dbae5d1bdb066849673e956005e3730dfd5affd7a6da4abac"}, - "phoenix_live_view": {:hex, :phoenix_live_view, "0.17.12", "74f4c0ad02d7deac2d04f50b52827a5efdc5c6e7fac5cede145f5f0e4183aedc", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6.0 or ~> 1.7.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.1", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "af6dd5e0aac16ff43571f527a8e0616d62cb80b10eb87aac82170243e50d99c8"}, + "phoenix_live_view": {:hex, :phoenix_live_view, "0.18.2", "635cf07de947235deb030cd6b776c71a3b790ab04cebf526aa8c879fe17c7784", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6 or ~> 1.7", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.1", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "da287a77327e996cc166e4c440c3ad5ab33ccdb151b91c793209b39ebbce5b75"}, "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.1", "ba04e489ef03763bf28a17eb2eaddc2c20c6d217e2150a61e3298b0f4c2012b5", [:mix], [], "hexpm", "81367c6d1eea5878ad726be80808eb5a787a23dee699f96e72b1109c57cdd8d9"}, "phoenix_view": {:hex, :phoenix_view, "1.1.2", "1b82764a065fb41051637872c7bd07ed2fdb6f5c3bd89684d4dca6e10115c95a", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "7ae90ad27b09091266f6adbb61e1d2516a7c3d7062c6789d46a7554ec40f3a56"}, "plug": {:hex, :plug, "1.13.6", "187beb6b67c6cec50503e940f0434ea4692b19384d47e5fdfd701e93cadb4cc2", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "02b9c6b9955bce92c829f31d6284bf53c591ca63c4fb9ff81dfd0418667a34ff"},