Skip to content

Commit

Permalink
feat: add sorting to infinity
Browse files Browse the repository at this point in the history
  • Loading branch information
colindensem committed Jul 8, 2022
1 parent 557390c commit 5bcacdb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 13 deletions.
3 changes: 2 additions & 1 deletion lib/ascend/hills.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ defmodule Ascend.Hills do
def list_hills_with_pagination(offset, limit, opts \\ %{}) do
from(h in Hill)
|> filter(opts)
|> sort(opts)
|> limit(^limit)
|> offset(^offset)
|> Repo.all()
Expand Down Expand Up @@ -100,7 +101,7 @@ defmodule Ascend.Hills do
end

defp sort(query, %{}) do
order_by(query, {:asc, :metres})
order_by(query, {:desc, :metres})
end

@doc """
Expand Down
5 changes: 3 additions & 2 deletions lib/ascend_web/live/hill_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@
module={AscendWeb.Live.SortingComponent}
id={"sorting-name"}
key={:name}
display_name="Name"
sorting={@sorting} />
</th>
<th>Dobih</th>
<th><.live_component
module={AscendWeb.Live.SortingComponent}
id={"sorting-metres"}
key={:metres}
display_name="Metres"
sorting={@sorting} />
</th>
<th><.live_component
module={AscendWeb.Live.SortingComponent}
id={"sorting-feet"}
key={:feet}
display_name="Feet"
sorting={@sorting} />
</th>
<th>Grid ref</th>
Expand All @@ -40,7 +42,6 @@
<td>
<%= live_redirect "#{hill.name}", to: Routes.hill_show_path(@socket, :show, hill) %>
</td>
<td><%= hill.dobih_id %></td>
<td><%= hill.metres %></td>
<td><%= hill.feet %></td>
<td><%= hill.grid_ref %></td>
Expand Down
53 changes: 48 additions & 5 deletions lib/ascend_web/live/infinity_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule AscendWeb.InfinityLive do

alias Ascend.Hills
alias AscendWeb.Forms.FilterForm
alias AscendWeb.Forms.SortingForm

def render(assigns) do
~H"""
Expand All @@ -13,13 +14,44 @@ defmodule AscendWeb.InfinityLive do
filter={@filter}
/>
<table>
<tbody id={"hills-#{@count}"} phx-update="append" phx-hook="InfinityScroll">
<thead>
<tr>
<th>
<.live_component
module={AscendWeb.Live.SortingComponent}
id={"sorting-name"}
key={:name}
display_name="Name"
sorting={@sorting} />
</th>
<th>
<.live_component
module={AscendWeb.Live.SortingComponent}
id={"sorting-metres"}
key={:metres}
display_name="Metres"
sorting={@sorting} />
</th>
<th>
<.live_component
module={AscendWeb.Live.SortingComponent}
id={"sorting-feet"}
key={:feet}
display_name="Feet"
sorting={@sorting} />
</th>
<th>Grid ref</th>
<th>Classification</th>
<th>Region</th>
<th>Area</th>
</tr>
</thead>
<tbody id={"hills-#{@key}"} phx-update="append" phx-hook="InfinityScroll">
<%= for hill <- @hills do %>
<tr id={"hill-#{hill.id}"}>
<td>
<%= live_redirect "#{hill.name}", to: Routes.hill_show_path(@socket, :show, hill) %>
</td>
<td><%= hill.dobih_id %></td>
<td><%= hill.metres %></td>
<td><%= hill.feet %></td>
<td><%= hill.grid_ref %></td>
Expand All @@ -35,10 +67,11 @@ defmodule AscendWeb.InfinityLive do

def mount(_params, _session, socket) do
count = Hills.hill_count()
key = :erlang.phash2(count)

socket =
socket
|> assign(offset: 0, limit: 25, count: count)
|> assign(offset: 0, limit: 25, count: count, key: key)

{:ok, socket, temporary_assigns: [hills: []]}
end
Expand Down Expand Up @@ -76,35 +109,45 @@ defmodule AscendWeb.InfinityLive do
socket
|> assign(offset: 0)
|> assign(limit: 25)
|> assign(key: :erlang.phash2(params))

{:noreply, push_patch(socket, to: path, replace: true)}
end

defp merge_and_sanitize_params(socket, overrides \\ %{}) do
%{filter: filter} = socket.assigns
%{sorting: sorting, filter: filter} = socket.assigns

%{}
|> Map.merge(filter)
|> Map.merge(sorting)
|> Map.merge(overrides)
|> Enum.reject(fn {_key, value} -> is_nil(value) end)
|> Map.new()
end

defp parse_params(socket, params) do
with {:ok, filter_opts} <- FilterForm.parse(params) do
with {:ok, filter_opts} <- FilterForm.parse(params),
{:ok, sorting_opts} <- SortingForm.parse(params) do
socket
|> assign_filter(filter_opts)
|> assign_sorting(sorting_opts)
else
_error ->
socket
|> assign_filter()
|> assign_sorting()
end
end

defp assign_filter(socket, overrides \\ %{}) do
assign(socket, :filter, FilterForm.default_values(overrides))
end

defp assign_sorting(socket, overrides \\ %{}) do
opts = Map.merge(SortingForm.default_values(), overrides)
assign(socket, :sorting, opts)
end

defp load_hills(socket) do
%{offset: offset, limit: limit} = socket.assigns

Expand Down
2 changes: 1 addition & 1 deletion lib/ascend_web/live/sorting_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule AscendWeb.Live.SortingComponent do
def render(assigns) do
~H"""
<div phx-click="sort" phx-target={@myself}>
<%= @key %> <%= chevron(@sorting, @key) %>
<%= @display_name %> <%= chevron(@sorting, @key) %>
</div>
"""
end
Expand Down
19 changes: 15 additions & 4 deletions test/ascend/hills_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ defmodule Ascend.HillsTest do
}

test "list_hills/1 returns all hills" do
hill1 = insert(:hill, metres: 1000, feet: 3280.84)
hill2 = insert(:hill, metres: 500, feet: 1640.42)
hill1 = insert(:hill, metres: 500, feet: 1640.42)
hill2 = insert(:hill, metres: 1000, feet: 3280.84)
assert Hills.list_hills(%{}) == [hill2, hill1]
end

Expand All @@ -47,8 +47,8 @@ defmodule Ascend.HillsTest do
end

test "list_hills_with_total_count/1 returns all hills" do
hill1 = insert(:hill, metres: 1000, feet: 3280.84)
hill2 = insert(:hill, metres: 500, feet: 1640.42)
hill1 = insert(:hill, metres: 500, feet: 1640.42)
hill2 = insert(:hill, metres: 1000, feet: 3280.84)

assert Hills.list_hills_with_total_count(%{}) == %{
hills: [hill2, hill1],
Expand Down Expand Up @@ -96,6 +96,17 @@ defmodule Ascend.HillsTest do
assert Hills.list_hills_with_pagination(1, 2, %{}) == [hill2, hill3]
end

test "list_hills_with_pagination/3 with name sort" do
insert(:hill, name: "Z Hill")
hill2 = insert(:hill, name: "AB Hill")
hill3 = insert(:hill, name: "AA Hill")

params = %{name: "a", sort_by: :name, sort_dir: :asc}
assert Hills.list_hills_with_pagination(0, 2, params) == [hill3, hill2]
assert Hills.list_hills_with_pagination(0, 1, params) == [hill3]
assert Hills.list_hills_with_pagination(1, 1, params) == [hill2]
end

test "get_hill!/1 returns the hill with given id" do
hill = insert(:hill)
assert Hills.get_hill!(hill.id) == hill
Expand Down

0 comments on commit 5bcacdb

Please sign in to comment.