Skip to content

Commit

Permalink
We now hide the edit button for published ballots. We also hide the p…
Browse files Browse the repository at this point in the history
…ublish button for published ballots as well as updates some basic content display.
  • Loading branch information
zorn committed Aug 8, 2024
1 parent b68f25f commit 197c965
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
8 changes: 8 additions & 0 deletions lib/flick/ranked_voting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ defmodule Flick.RankedVoting do
|> Repo.update()
end

@doc """
Returns a boolean value indicating whether the given
`Flick.RankedVoting.Ballot` entity can be updated.
"""
@spec can_update_ballot?(Ballot.t()) :: boolean()
def can_update_ballot?(%Ballot{published_at: nil}), do: true
def can_update_ballot?(_), do: false

@doc """
Publishes the given `Flick.RankedVoting.Ballot` entity.
Expand Down
27 changes: 15 additions & 12 deletions lib/flick_web/live/ballots/viewer_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ defmodule FlickWeb.Ballots.ViewerLive do
<dl>
<dt class="font-bold">Question Title</dt>
<dd class="pb-4"><%= @ballot.question_title %></dd>
<dd id="ballot-question-title" class="pb-4"><%= @ballot.question_title %></dd>
<dt class="font-bold">Possible Answers</dt>
<dd class="pb-4"><%= @ballot.possible_answers %></dd>
<dd id="ballot-possible-answers" class="pb-4"><%= @ballot.possible_answers %></dd>
<dt class="font-bold">URL Slug</dt>
<dd class="pb-4"><%= @ballot.url_slug %></dd>
<dd id="ballot-url-slug" class="pb-4"><%= @ballot.url_slug %></dd>
</dl>
<.button>
<.button :if={RankedVoting.can_update_ballot?(@ballot)} id="edit-ballot-button">
<.link
navigate={~p"/#{@ballot.url_slug}/#{@ballot.id}/edit"}
class="text-white no-underline"
Expand All @@ -62,17 +62,20 @@ defmodule FlickWeb.Ballots.ViewerLive do
<div class="my-6">
<%= if @ballot.published_at do %>
Published at: <%= @ballot.published_at %>
<div class="prose">
<p>This ballot was published at: <%= @ballot.published_at %></p>
<p>
You can invite people to vote using the URL:
<.link navigate={~p"/#{@ballot.url_slug}"}>
<%= URI.append_path(@socket.host_uri, "/#{@ballot.url_slug}") %>
</.link>
</p>
</div>
<% else %>
<.button phx-click="publish">Publish</.button>
<.button phx-click="publish" id="publish-ballot-button">Publish</.button>
<% end %>
</div>
<p>Some ballot detail page.</p>
<p id="ballot-question-title"><%= @ballot.question_title %></p>
<div>
<pre><%= inspect(@ballot, pretty: true) %></pre>
</div>
</div>
"""
end
Expand Down
49 changes: 48 additions & 1 deletion test/flick_web/live/ballots/viewer_live_test.exs
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
defmodule FlickWeb.Ballots.ViewerLiveTest do
@moduledoc """
Verifies the expected logic of `FlickWeb.Ballots.ViewerLive`.
Viewer: http://localhost:4000/<url-slug>/<secret>
"""

use FlickWeb.ConnCase, async: true

alias Flick.RankedVoting

test "renders ballot details", ~M{conn} do
ballot = ballot_fixture()
assert {:ok, view, _html} = live(conn, ~p"/#{ballot.url_slug}/#{ballot.id}")
assert {:ok, view, _html} = live(conn, view_path(ballot))
assert has_element?(view, "#ballot-question-title", ballot.question_title)
assert has_element?(view, "#ballot-possible-answers", ballot.possible_answers)
assert has_element?(view, "#ballot-url-slug", ballot.url_slug)
end

test "responds with 404 when no ballot is found", ~M{conn} do
assert_raise Ecto.NoResultsError, fn ->
live(conn, ~p"/ballots/#{Ecto.UUID.generate()}")
end
end

test "presents edit button for non published ballots", ~M{conn} do
ballot = ballot_fixture()
assert {:ok, view, _html} = live(conn, view_path(ballot))
assert has_element?(view, "#edit-ballot-button")
end

test "hides edit button for non published ballots", ~M{conn} do
ballot = ballot_fixture()
{:ok, published_ballot} = RankedVoting.publish_ballot(ballot)
{:ok, view, _html} = live(conn, view_path(published_ballot))
refute has_element?(view, "#edit-ballot-button")
end

test "presents the publish button for non published ballots", ~M{conn} do
ballot = ballot_fixture()
assert {:ok, view, _html} = live(conn, ~p"/#{ballot.url_slug}/#{ballot.id}")
assert has_element?(view, "#publish-ballot-button")
end

test "hides the publish button for published ballots", ~M{conn} do
ballot = ballot_fixture()
{:ok, published_ballot} = RankedVoting.publish_ballot(ballot)
{:ok, view, _html} = live(conn, view_path(published_ballot))
refute has_element?(view, "#publish-ballot-button")
end

test "presents the vote link for a published ballot", ~M{conn} do
ballot = ballot_fixture()
{:ok, published_ballot} = RankedVoting.publish_ballot(ballot)
{:ok, view, _html} = live(conn, view_path(published_ballot))
assert has_element?(view, "a[href='/#{published_ballot.url_slug}']")
end

defp view_path(ballot) do
~p"/#{ballot.url_slug}/#{ballot.id}"
end
end

0 comments on commit 197c965

Please sign in to comment.