Skip to content

Commit

Permalink
only show comments for page you are on
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nelson committed Sep 26, 2023
1 parent a28d3e6 commit f517e30
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 21 deletions.
3 changes: 3 additions & 0 deletions assets/js/launch-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export class LaunchCommentsElement extends LitElement {
@liveStateConfig('topic')
get topic() { return `launch_comments:${this.siteId}`; }

@liveStateConfig('params.url')
get pageUrl() { return window.location.href; }

@query('input[name="author"]')
author: HTMLInputElement | undefined;

Expand Down
8 changes: 4 additions & 4 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ config :launch_cart, :sandbox, Ecto.Adapters.SQL.Sandbox

config :wallaby,
otp_app: :launch_cart,
base_url: "http://localhost:4002"
# chromedriver: [
# headless: false
# ]
base_url: "http://localhost:4002",
chromedriver: [
headless: false
]
4 changes: 3 additions & 1 deletion lib/factory.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule LaunchCart.Factory do

alias Faker.Person
alias Faker.Lorem
alias Faker.Internet

Expand Down Expand Up @@ -97,7 +98,8 @@ defmodule LaunchCart.Factory do
def comment_factory() do
%Comment{
comment_site: build(:comment_site),
author: "Bob",
author: Person.name,
url: Internet.url(),
comment: "I think therefore I am"
}
end
Expand Down
8 changes: 6 additions & 2 deletions lib/launch_cart/comments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ defmodule LaunchCart.Comments do
[%Comment{}, ...]
"""
def list_comments(site_id) do
from(c in Comment, where: c.comment_site_id == ^site_id, order_by: {:desc, c.inserted_at})
def list_comments(site_id, url) do
from(c in Comment,
where: c.comment_site_id == ^site_id,
where: c.url == ^url,
order_by: {:desc, c.inserted_at}
)
|> Repo.all()
end

Expand Down
17 changes: 8 additions & 9 deletions lib/launch_cart_web/channels/comments_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ defmodule LaunchCartWeb.CommentsChannel do
use LiveState.Channel, web_module: LaunchCartWeb

alias LaunchCart.Comments
alias LaunchCart.CommentSites
alias LaunchCart.CommentSites.CommentSite
alias LiveState.Event

@impl true
def state_key, do: :state

@impl true
@spec init(any, any, any) :: {:ok, %{comments: any}}
def init("launch_comments:" <> comment_site_id, _params, _socket) do
@spec init(any, any, any) :: {:ok, map()}
def init("launch_comments:" <> comment_site_id, %{"url" => url}, _socket) do
Phoenix.PubSub.subscribe(LaunchCart.PubSub, "comments:#{comment_site_id}")
case CommentSites.get_comment_site!(comment_site_id) do

case Comments.list_comments(comment_site_id, url) do
nil -> {:error, "Comment site not found"}
%CommentSite{comments: comments} ->
{:ok, %{comments: comments}}
comments -> {:ok, %{comments: comments, url: url}}
end
end

Expand All @@ -30,8 +28,9 @@ defmodule LaunchCartWeb.CommentsChannel do
end

@impl true
def handle_message({:comment_created, comment}, state) do
{:noreply, state |> Map.put(:comments, Comments.list_comments(comment.comment_site_id))}
def handle_message({:comment_created, comment}, %{url: url} = state) do
{:noreply,
state |> Map.put(:comments, Comments.list_comments(comment.comment_site_id, url))}
end

@impl true
Expand Down
11 changes: 9 additions & 2 deletions test/launch_cart/comments_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ defmodule LaunchCart.CommentsTest do

test "list_comments/1 returns all comments for site" do
comment = insert(:comment)
assert Comments.list_comments(comment.comment_site_id) |> Enum.map(& &1.id) == [comment.id]

assert Comments.list_comments(comment.comment_site_id, comment.url) |> Enum.map(& &1.id) ==
[comment.id]
end

test "get_comment!/1 returns the comment with given id" do
Expand All @@ -35,7 +37,12 @@ defmodule LaunchCart.CommentsTest do

test "update_comment/2 with valid data updates the comment" do
comment = insert(:comment)
update_attrs = %{author: "some updated author", comment: "some updated comment", url: "some updated url"}

update_attrs = %{
author: "some updated author",
comment: "some updated comment",
url: "some updated url"
}

assert {:ok, %Comment{} = comment} = Comments.update_comment(comment, update_attrs)
assert comment.author == "some updated author"
Expand Down
40 changes: 37 additions & 3 deletions test/launch_cart_web/features/launch_comments_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule LaunchCartWeb.Features.LaunchCommentsTest do
alias LaunchCart.Repo
alias LaunchCart.CommentSites
alias LaunchCart.Comments
alias LaunchCartWeb.Endpoint
alias LaunchCartWeb.Router.Helpers, as: Routes

import Wallaby.Query
import LaunchCart.Factory
Expand All @@ -29,20 +31,52 @@ defmodule LaunchCartWeb.Features.LaunchCommentsTest do
end

feature "sees new comments as they are created", %{session: session, comment_site: comment_site} do

Check failure on line 33 in test/launch_cart_web/features/launch_comments_test.exs

View workflow job for this annotation

GitHub Actions / Run Application Tests

feature sees new comments as they are created (LaunchCartWeb.Features.LaunchCommentsTest)

Check failure on line 33 in test/launch_cart_web/features/launch_comments_test.exs

View workflow job for this annotation

GitHub Actions / Run application tests to generate HTML files

feature sees new comments as they are created (LaunchCartWeb.Features.LaunchCommentsTest)
session = session
|> visit("/fake_comment_site/#{comment_site.id}")
session =
session
|> visit("/fake_comment_site/#{comment_site.id}")

{:ok, _comment} =
Comments.create_comment(%{
comment_site_id: comment_site.id,
url: Routes.page_url(Endpoint, :fake_comment_site, comment_site.id),
author: "Yo Mamma",
comment: "is so great that you are lucky to have her."
})

session
|> find(css("launch-comments"))
|> shadow_root()
|> assert_has(css("div[part='comment-text']", text: "is so great"))
end

feature "sees comments for current page url only", %{

Check failure on line 52 in test/launch_cart_web/features/launch_comments_test.exs

View workflow job for this annotation

GitHub Actions / Run Application Tests

feature sees comments for current page url only (LaunchCartWeb.Features.LaunchCommentsTest)

Check failure on line 52 in test/launch_cart_web/features/launch_comments_test.exs

View workflow job for this annotation

GitHub Actions / Run application tests to generate HTML files

feature sees comments for current page url only (LaunchCartWeb.Features.LaunchCommentsTest)
session: session,
comment_site: comment_site
} do
session =
session
|> visit("/fake_comment_site/#{comment_site.id}")

{:ok, _comment} =
Comments.create_comment(%{
comment_site_id: comment_site.id,
url: "http://foo.bar",
url: Routes.page_url(Endpoint, :fake_comment_site, comment_site.id),
author: "Yo Mamma",
comment: "is so great that you are lucky to have her."
})

{:ok, _comment} =
Comments.create_comment(%{
comment_site_id: comment_site.id,
url: "http://foo.bar/dad",
author: "Yo Daddy",
comment: "is pretty good."
})

session
|> find(css("launch-comments"))
|> shadow_root()
|> assert_has(css("div[part='comment-text']", text: "is so great"))
|> refute_has(css("div[part='comment-text']", text: "is pretty good"))
end
end

0 comments on commit f517e30

Please sign in to comment.