Skip to content

Commit

Permalink
Merge pull request #319 from pulibrary/313-browse-page
Browse files Browse the repository at this point in the history
Add initial browse page
  • Loading branch information
tpendragon authored Feb 19, 2025
2 parents d3273e3 + 9db2dd9 commit fd12146
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/dpul_collections/solr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ defmodule DpulCollections.Solr do
response.body["response"]
end

def random(count, seed, collection \\ read_collection()) do
fl = Enum.join(@query_field_list, ",")

solr_params = [
fl: fl,
rows: count,
sort: "random_#{seed} desc"
]

{:ok, response} =
Req.get(
select_url(collection),
params: solr_params
)

response.body["response"]
end

defp query_param(search_state) do
Enum.reject([search_state[:q], date_query(search_state)], &is_nil(&1))
|> Enum.join(" ")
Expand Down
87 changes: 87 additions & 0 deletions lib/dpul_collections_web/live/browse_live.ex
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
1 change: 1 addition & 0 deletions lib/dpul_collections_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defmodule DpulCollectionsWeb.Router do
pipe_through :browser

live "/", HomeLive, :live
live "/browse", BrowseLive, :live
live "/search", SearchLive, :live
live "/item/:id", ItemLive, :live
live "/i/:slug/item/:id", ItemLive, :live
Expand Down
18 changes: 18 additions & 0 deletions test/dpul_collections/solr_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ defmodule DpulCollections.SolrTest do
assert Solr.document_count() == 1
end

test ".random/3 with two different seeds returns different results" do
Solr.add(SolrTestSupport.mock_solr_documents(), active_collection())
Solr.commit(active_collection())

set1 = Solr.random(5, "100")
set2 = Solr.random(5, "999")
assert set1 != set2
end

test ".random/3 with the same seed returns the same results" do
Solr.add(SolrTestSupport.mock_solr_documents(), active_collection())
Solr.commit(active_collection())

set1 = Solr.random(5, "100")
set2 = Solr.random(5, "100")
assert set1 == set2
end

test ".latest_document" do
doc = %{
"id" => "3cb7627b-defc-401b-9959-42ebc4488f74",
Expand Down
83 changes: 83 additions & 0 deletions test/dpul_collections_web/live/browse_live_test.exs
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

0 comments on commit fd12146

Please sign in to comment.