Skip to content

Commit

Permalink
comments appear as they are added
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nelson committed Sep 25, 2023
1 parent 1906f03 commit 027e37e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
20 changes: 18 additions & 2 deletions lib/launch_cart/comments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ defmodule LaunchCart.Comments do
[%Comment{}, ...]
"""
def list_comments do
Repo.all(Comment)
def list_comments(site_id) do
from(c in Comment, where: c.comment_site_id == ^site_id, order_by: {:desc, c.inserted_at})
|> Repo.all()
end

@doc """
Expand Down Expand Up @@ -53,6 +54,21 @@ defmodule LaunchCart.Comments do
%Comment{}
|> Comment.changeset(attrs)
|> Repo.insert()
|> case do
{:ok, comment} ->
Phoenix.PubSub.broadcast(
LaunchCart.PubSub,
"comments:#{comment.comment_site_id}",
{:comment_created, comment}
)

# what do here?
# comment |> CommentEmail.comment_added() |> Mailer.deliver() |> IO.inspect()
{:ok, comment}

{:error, changeset} ->
{:error, changeset}
end
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion test/launch_cart/comments_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule LaunchCart.CommentsTest do

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

test "get_comment!/1 returns the comment with given id" do
Expand Down
19 changes: 19 additions & 0 deletions test/launch_cart_web/features/launch_comments_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule LaunchCartWeb.Features.LaunchCommentsTest do

alias LaunchCart.Repo
alias LaunchCart.CommentSites
alias LaunchCart.Comments

import Wallaby.Query
import LaunchCart.Factory
Expand All @@ -26,4 +27,22 @@ defmodule LaunchCartWeb.Features.LaunchCommentsTest do
assert %{comments: [%{comment: "Has sumpin to say", author: "Bob"}]} =
CommentSites.get_comment_site!(comment_site.id) |> Repo.preload(:comments)
end

feature "sees new comments as they are created", %{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",
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
end

0 comments on commit 027e37e

Please sign in to comment.