Show draft/scheduled status banners for authenticated users
This commit is contained in:
@@ -22,11 +22,14 @@ defmodule FirehoseWeb.BlogController do
|
||||
def show(conn, %{"slug" => slug}) do
|
||||
blog = conn.assigns.blog
|
||||
post = blog.get_post!(slug)
|
||||
visibility = Blogex.Post.visibility(post)
|
||||
|
||||
render(conn, :show,
|
||||
page_title: post.title,
|
||||
post: post,
|
||||
base_path: blog.base_path()
|
||||
base_path: blog.base_path(),
|
||||
visibility: visibility,
|
||||
authenticated: conn.assigns[:current_user] != nil
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,4 +1,23 @@
|
||||
<div class="space-y-8">
|
||||
<a href={@base_path} class="text-sm text-primary hover:underline">← Back to posts</a>
|
||||
|
||||
<%= if @authenticated and @visibility == :draft do %>
|
||||
<div
|
||||
class="rounded-lg bg-amber-50 border border-amber-200 px-4 py-3 text-amber-800 text-sm font-medium"
|
||||
id="post-status-banner"
|
||||
>
|
||||
Draft — not published
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= if @authenticated and @visibility == :scheduled do %>
|
||||
<div
|
||||
class="rounded-lg bg-blue-50 border border-blue-200 px-4 py-3 text-blue-800 text-sm font-medium"
|
||||
id="post-status-banner"
|
||||
>
|
||||
This post is scheduled for {Calendar.strftime(@post.date, "%B %d, %Y")}
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<.post_show post={@post} base_path={@base_path} />
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
%{
|
||||
title: "Future Test Post",
|
||||
author: "Test Author",
|
||||
tags: ~w(test),
|
||||
description: "A post scheduled for the future"
|
||||
}
|
||||
---
|
||||
This is a future test post.
|
||||
@@ -0,0 +1,58 @@
|
||||
defmodule FirehoseWeb.BlogControllerTest do
|
||||
use FirehoseWeb.ConnCase, async: false
|
||||
|
||||
describe "GET /blog/:blog_id/:slug - status banners" do
|
||||
test "authenticated user sees draft banner on draft post", %{conn: conn} do
|
||||
conn =
|
||||
conn
|
||||
|> init_test_session(%{})
|
||||
|> assign(:current_user, %{id: 1})
|
||||
|> get(~p"/blog/engineering/hello-world")
|
||||
|
||||
assert html_response(conn, 200) =~ "Draft"
|
||||
assert conn.resp_body =~ "not published"
|
||||
end
|
||||
|
||||
test "authenticated user sees scheduled banner on future post", %{conn: conn} do
|
||||
conn =
|
||||
conn
|
||||
|> init_test_session(%{})
|
||||
|> assign(:current_user, %{id: 1})
|
||||
|> get(~p"/blog/engineering/future-test-post")
|
||||
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ "scheduled for"
|
||||
assert response =~ "January 01, 2099"
|
||||
end
|
||||
|
||||
test "authenticated user sees no banner on live post", %{conn: conn} do
|
||||
conn =
|
||||
conn
|
||||
|> init_test_session(%{})
|
||||
|> assign(:current_user, %{id: 1})
|
||||
|> get(~p"/blog/engineering/why-firehose")
|
||||
|
||||
response = html_response(conn, 200)
|
||||
refute response =~ "Draft"
|
||||
refute response =~ "scheduled for"
|
||||
end
|
||||
|
||||
test "unauthenticated user sees no banner on draft post", %{conn: conn} do
|
||||
response =
|
||||
conn
|
||||
|> get(~p"/blog/engineering/hello-world")
|
||||
|> html_response(200)
|
||||
|
||||
refute response =~ "post-status-banner"
|
||||
end
|
||||
|
||||
test "unauthenticated user sees no banner on future post", %{conn: conn} do
|
||||
response =
|
||||
conn
|
||||
|> get(~p"/blog/engineering/future-test-post")
|
||||
|> html_response(200)
|
||||
|
||||
refute response =~ "post-status-banner"
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user