-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #319 from pulibrary/313-browse-page
Add initial browse page
- Loading branch information
Showing
5 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
defmodule DpulCollectionsWeb.BrowseLive do | ||
use DpulCollectionsWeb, :live_view | ||
import DpulCollectionsWeb.Gettext | ||
alias DpulCollections.{Item, Solr} | ||
|
||
def mount(_params, _session, socket) do | ||
{:ok, socket} | ||
end | ||
|
||
def handle_params(params, _uri, socket) do | ||
given_seed = params["r"] | ||
|
||
if given_seed do | ||
socket = | ||
socket | ||
|> assign( | ||
items: | ||
Solr.random(500, given_seed)["docs"] | ||
|> Enum.map(&Item.from_solr(&1)) | ||
) | ||
|
||
{:noreply, socket} | ||
else | ||
{:noreply, push_patch(socket, to: "/browse?r=#{Enum.random(1..3000)}", replace: true)} | ||
end | ||
end | ||
|
||
def handle_event("randomize", _map, socket) do | ||
{:noreply, push_patch(socket, to: "/browse?r=#{Enum.random(1..3000)}")} | ||
end | ||
|
||
def render(assigns) do | ||
~H""" | ||
<div class="my-5 grid grid-flow-row auto-rows-max gap-10 grid-cols-4"> | ||
<h1 class="text-2xl col-span-3"><%= gettext("Browse") %></h1> | ||
<button class="col-span-1 btn-primary" phx-click="randomize"> | ||
<%= gettext("Randomize") %> | ||
</button> | ||
</div> | ||
<div class="grid grid-cols-5 gap-3"> | ||
<.browse_item :for={item <- @items} item={item} /> | ||
</div> | ||
""" | ||
end | ||
|
||
attr :item, Item, required: true | ||
|
||
def browse_item(assigns) do | ||
~H""" | ||
<div id={"item-#{@item.id}"} class="item"> | ||
<div class="flex flex-wrap gap-5 md:max-h-60 max-h-[22rem] overflow-hidden justify-center md:justify-start relative"> | ||
<.thumb :if={@item.page_count} thumb={thumbnail_service_url(@item)} /> | ||
</div> | ||
<h2 class="underline text-2xl font-bold pt-4"> | ||
<.link navigate={@item.url} class="item-link"><%= @item.title %></.link> | ||
</h2> | ||
<div class="text-xl"><%= @item.date %></div> | ||
</div> | ||
""" | ||
end | ||
|
||
def thumb(assigns) do | ||
~H""" | ||
<img | ||
class="h-[350px] w-[350px] md:h-[225px] md:w-[225px] border border-solid border-gray-400" | ||
src={"#{@thumb}/square/350,350/0/default.jpg"} | ||
alt="thumbnail image" | ||
style=" | ||
background-color: lightgray;" | ||
width="350" | ||
height="350" | ||
/> | ||
""" | ||
end | ||
|
||
defp thumbnail_service_url(%{primary_thumbnail_service_url: thumbnail_url}) | ||
when is_binary(thumbnail_url) do | ||
thumbnail_url | ||
end | ||
|
||
defp thumbnail_service_url(%{image_service_urls: [url | _]}) do | ||
url | ||
end | ||
|
||
# TODO: default image? | ||
defp thumbnail_service_url(_), do: "" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
defmodule DpulCollectionsWeb.BrowseLiveTest do | ||
use DpulCollectionsWeb.ConnCase | ||
import Phoenix.LiveViewTest | ||
import SolrTestSupport | ||
alias DpulCollections.Solr | ||
@endpoint DpulCollectionsWeb.Endpoint | ||
|
||
setup do | ||
on_exit(fn -> Solr.delete_all(active_collection()) end) | ||
end | ||
|
||
test "GET /", %{conn: conn} do | ||
conn = get(conn, ~p"/browse") | ||
assert redirected_to(conn, 302) =~ "/browse?r=" | ||
end | ||
|
||
test "click random button", %{conn: conn} do | ||
Solr.add(SolrTestSupport.mock_solr_documents(500), active_collection()) | ||
Solr.commit(active_collection()) | ||
|
||
{:ok, view, html} = live(conn, "/browse?r=0") | ||
|
||
initial_order = | ||
html | ||
|> Floki.parse_document!() | ||
|> Floki.find(".item-link") | ||
|> Enum.flat_map(fn a -> Floki.attribute(a, "href") end) | ||
|
||
assert Enum.count(initial_order) == 500 | ||
|
||
{:ok, document} = | ||
view | ||
|> render_click("randomize") | ||
|> Floki.parse_document() | ||
|
||
new_order = | ||
document | ||
|> Floki.find(".item-link") | ||
|> Enum.flat_map(fn a -> Floki.attribute(a, "href") end) | ||
|
||
assert initial_order != new_order | ||
end | ||
|
||
test "renders a link when there's a page count but no thumbnail", %{conn: conn} do | ||
Solr.add( | ||
[ | ||
%{ | ||
id: "n", | ||
title_txtm: "Document-n", | ||
page_count_i: 3 | ||
} | ||
], | ||
active_collection() | ||
) | ||
|
||
Solr.commit(active_collection()) | ||
|
||
{:ok, view, html} = live(conn, "/browse?r=0") | ||
|
||
view | ||
|> has_element?(".item-link") | ||
end | ||
|
||
test "renders a link when page count is zero and there's no thumbnail", %{conn: conn} do | ||
Solr.add( | ||
[ | ||
%{ | ||
id: "n", | ||
title_txtm: "Document-n", | ||
page_count_i: 0 | ||
} | ||
], | ||
active_collection() | ||
) | ||
|
||
Solr.commit(active_collection()) | ||
|
||
{:ok, view, html} = live(conn, "/browse?r=0") | ||
|
||
view | ||
|> has_element?(".item-link") | ||
end | ||
end |