From 160b32af1b720ca4e57c9bad10138d68892c7c72 Mon Sep 17 00:00:00 2001 From: Chris Nelson Date: Thu, 29 Jun 2023 12:39:39 -0400 Subject: [PATCH 1/9] WIP adding launch-comments --- lib/launch_cart/comment_sites.ex | 104 +++ lib/launch_cart/comment_sites/comment_site.ex | 22 + lib/launch_cart_web.ex | 1 + .../components/core_components.ex | 689 ++++++++++++++++++ .../live/comment_site_live/form_component.ex | 64 ++ .../form_component.html.heex | 28 + .../live/comment_site_live/index.ex | 48 ++ .../live/comment_site_live/index.html.heex | 40 + .../live/comment_site_live/show.ex | 21 + .../live/comment_site_live/show.html.heex | 1 + lib/launch_cart_web/router.ex | 7 + .../20230628211511_create_comment_sites.exs | 15 + test/launch_cart/comment_sites_test.exs | 61 ++ .../live/comment_site_live_test.exs | 120 +++ .../fixtures/comment_sites_fixtures.ex | 21 + 15 files changed, 1242 insertions(+) create mode 100644 lib/launch_cart/comment_sites.ex create mode 100644 lib/launch_cart/comment_sites/comment_site.ex create mode 100644 lib/launch_cart_web/components/core_components.ex create mode 100644 lib/launch_cart_web/live/comment_site_live/form_component.ex create mode 100644 lib/launch_cart_web/live/comment_site_live/form_component.html.heex create mode 100644 lib/launch_cart_web/live/comment_site_live/index.ex create mode 100644 lib/launch_cart_web/live/comment_site_live/index.html.heex create mode 100644 lib/launch_cart_web/live/comment_site_live/show.ex create mode 100644 lib/launch_cart_web/live/comment_site_live/show.html.heex create mode 100644 priv/repo/migrations/20230628211511_create_comment_sites.exs create mode 100644 test/launch_cart/comment_sites_test.exs create mode 100644 test/launch_cart_web/live/comment_site_live_test.exs create mode 100644 test/support/fixtures/comment_sites_fixtures.ex diff --git a/lib/launch_cart/comment_sites.ex b/lib/launch_cart/comment_sites.ex new file mode 100644 index 0000000..3546f84 --- /dev/null +++ b/lib/launch_cart/comment_sites.ex @@ -0,0 +1,104 @@ +defmodule LaunchCart.CommentSites do + @moduledoc """ + The CommentSites context. + """ + + import Ecto.Query, warn: false + alias LaunchCart.Repo + + alias LaunchCart.CommentSites.CommentSite + + @doc """ + Returns the list of comment_sites. + + ## Examples + + iex> list_comment_sites() + [%CommentSite{}, ...] + + """ + def list_comment_sites do + Repo.all(CommentSite) + end + + @doc """ + Gets a single comment_site. + + Raises `Ecto.NoResultsError` if the Comment site does not exist. + + ## Examples + + iex> get_comment_site!(123) + %CommentSite{} + + iex> get_comment_site!(456) + ** (Ecto.NoResultsError) + + """ + def get_comment_site!(id), do: Repo.get!(CommentSite, id) + + @doc """ + Creates a comment_site. + + ## Examples + + iex> create_comment_site(%{field: value}) + {:ok, %CommentSite{}} + + iex> create_comment_site(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_comment_site(attrs \\ %{}) do + %CommentSite{} + |> CommentSite.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a comment_site. + + ## Examples + + iex> update_comment_site(comment_site, %{field: new_value}) + {:ok, %CommentSite{}} + + iex> update_comment_site(comment_site, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_comment_site(%CommentSite{} = comment_site, attrs) do + comment_site + |> CommentSite.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a comment_site. + + ## Examples + + iex> delete_comment_site(comment_site) + {:ok, %CommentSite{}} + + iex> delete_comment_site(comment_site) + {:error, %Ecto.Changeset{}} + + """ + def delete_comment_site(%CommentSite{} = comment_site) do + Repo.delete(comment_site) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking comment_site changes. + + ## Examples + + iex> change_comment_site(comment_site) + %Ecto.Changeset{data: %CommentSite{}} + + """ + def change_comment_site(%CommentSite{} = comment_site, attrs \\ %{}) do + CommentSite.changeset(comment_site, attrs) + end +end diff --git a/lib/launch_cart/comment_sites/comment_site.ex b/lib/launch_cart/comment_sites/comment_site.ex new file mode 100644 index 0000000..763fa7a --- /dev/null +++ b/lib/launch_cart/comment_sites/comment_site.ex @@ -0,0 +1,22 @@ +defmodule LaunchCart.CommentSites.CommentSite do + use Ecto.Schema + import Ecto.Changeset + alias LaunchCart.Accounts.User + + @primary_key {:id, :binary_id, autogenerate: true} + @foreign_key_type :binary_id + schema "comment_sites" do + field :name, :string + field :url, :string + belongs_to :user, User + + timestamps() + end + + @doc false + def changeset(comment_site, attrs) do + comment_site + |> cast(attrs, [:name, :url]) + |> validate_required([:name, :url]) + end +end diff --git a/lib/launch_cart_web.ex b/lib/launch_cart_web.ex index 9bbbfa8..cc53ded 100644 --- a/lib/launch_cart_web.ex +++ b/lib/launch_cart_web.ex @@ -62,6 +62,7 @@ defmodule LaunchCartWeb do def component do quote do use Phoenix.Component + import LaunchCartWeb.CoreComponents unquote(view_helpers()) end diff --git a/lib/launch_cart_web/components/core_components.ex b/lib/launch_cart_web/components/core_components.ex new file mode 100644 index 0000000..e8d239c --- /dev/null +++ b/lib/launch_cart_web/components/core_components.ex @@ -0,0 +1,689 @@ +defmodule LaunchCartWeb.CoreComponents do + @moduledoc """ + Provides core UI components. + + The components in this module use Tailwind CSS, a utility-first CSS framework. + See the [Tailwind CSS documentation](https://tailwindcss.com) to learn how to + customize the generated components in this module. + + Icons are provided by [heroicons](https://heroicons.com). See `icon/1` for usage. + """ + use Phoenix.Component + + alias Phoenix.LiveView.JS + import LaunchCartWeb.Gettext + + @doc """ + Renders a modal. + + ## Examples + + <.modal id="confirm-modal"> + Are you sure? + <:confirm>OK + <:cancel>Cancel + + + JS commands may be passed to the `:on_cancel` and `on_confirm` attributes + for the caller to react to each button press, for example: + + <.modal id="confirm" on_confirm={JS.push("delete")} on_cancel={JS.navigate(~p"/posts")}> + Are you sure you? + <:confirm>OK + <:cancel>Cancel + + """ + attr :id, :string, required: true + attr :show, :boolean, default: false + attr :on_cancel, JS, default: %JS{} + attr :on_confirm, JS, default: %JS{} + + slot :inner_block, required: true + slot :title + slot :subtitle + slot :confirm + slot :cancel + + def modal(assigns) do + ~H""" +