diff --git a/lib/ascend/hills.ex b/lib/ascend/hills.ex index 3cf49b4..6aa5e39 100644 --- a/lib/ascend/hills.ex +++ b/lib/ascend/hills.ex @@ -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() @@ -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 """ diff --git a/lib/ascend_web/live/hill_live/index.html.heex b/lib/ascend_web/live/hill_live/index.html.heex index 7ee8490..4cc44bb 100644 --- a/lib/ascend_web/live/hill_live/index.html.heex +++ b/lib/ascend_web/live/hill_live/index.html.heex @@ -13,19 +13,21 @@ module={AscendWeb.Live.SortingComponent} id={"sorting-name"} key={:name} + display_name="Name" sorting={@sorting} /> - Dobih <.live_component module={AscendWeb.Live.SortingComponent} id={"sorting-metres"} key={:metres} + display_name="Metres" sorting={@sorting} /> <.live_component module={AscendWeb.Live.SortingComponent} id={"sorting-feet"} key={:feet} + display_name="Feet" sorting={@sorting} /> Grid ref @@ -40,7 +42,6 @@ <%= live_redirect "#{hill.name}", to: Routes.hill_show_path(@socket, :show, hill) %> - <%= hill.dobih_id %> <%= hill.metres %> <%= hill.feet %> <%= hill.grid_ref %> diff --git a/lib/ascend_web/live/infinity_live.ex b/lib/ascend_web/live/infinity_live.ex index 68584d9..cb0fadd 100644 --- a/lib/ascend_web/live/infinity_live.ex +++ b/lib/ascend_web/live/infinity_live.ex @@ -3,6 +3,7 @@ defmodule AscendWeb.InfinityLive do alias Ascend.Hills alias AscendWeb.Forms.FilterForm + alias AscendWeb.Forms.SortingForm def render(assigns) do ~H""" @@ -13,13 +14,44 @@ defmodule AscendWeb.InfinityLive do filter={@filter} /> - + + + + + + + + + + + + <%= for hill <- @hills do %> - @@ -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 @@ -76,28 +109,33 @@ 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 @@ -105,6 +143,11 @@ defmodule AscendWeb.InfinityLive 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 diff --git a/lib/ascend_web/live/sorting_component.ex b/lib/ascend_web/live/sorting_component.ex index 3b91fe6..94a83a2 100644 --- a/lib/ascend_web/live/sorting_component.ex +++ b/lib/ascend_web/live/sorting_component.ex @@ -4,7 +4,7 @@ defmodule AscendWeb.Live.SortingComponent do def render(assigns) do ~H"""
- <%= @key %> <%= chevron(@sorting, @key) %> + <%= @display_name %> <%= chevron(@sorting, @key) %>
""" end diff --git a/test/ascend/hills_test.exs b/test/ascend/hills_test.exs index e0d3fbe..271fc1e 100644 --- a/test/ascend/hills_test.exs +++ b/test/ascend/hills_test.exs @@ -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 @@ -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], @@ -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
+ <.live_component + module={AscendWeb.Live.SortingComponent} + id={"sorting-name"} + key={:name} + display_name="Name" + sorting={@sorting} /> + + <.live_component + module={AscendWeb.Live.SortingComponent} + id={"sorting-metres"} + key={:metres} + display_name="Metres" + sorting={@sorting} /> + + <.live_component + module={AscendWeb.Live.SortingComponent} + id={"sorting-feet"} + key={:feet} + display_name="Feet" + sorting={@sorting} /> + Grid refClassificationRegionArea
<%= live_redirect "#{hill.name}", to: Routes.hill_show_path(@socket, :show, hill) %> <%= hill.dobih_id %> <%= hill.metres %> <%= hill.feet %> <%= hill.grid_ref %>