Skip to content

Commit

Permalink
Merge pull request #423 from krivtsov/add_pagination
Browse files Browse the repository at this point in the history
Add pagination
  • Loading branch information
vtm9 committed Jan 18, 2019
2 parents 2faee01 + 106dc01 commit 835aabc
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tmp
*.elixir_ls/
*deployment.retry
*~
*.swp


# Generated on crash by the VM
Expand Down
3 changes: 3 additions & 0 deletions services/app/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ config :one_signal, OneSignal,
app_id: System.get_env("ONESIGNAL_APP_ID"),
api_key: System.get_env("ONESIGNAL_API_KEY")

config :scrivener_html,
routes_helper: CodebattleWeb.Router.Helpers

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"
1 change: 1 addition & 0 deletions services/app/lib/codebattle/repo.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
defmodule Codebattle.Repo do
use Ecto.Repo, otp_app: :codebattle
use Scrivener, page_size: 10
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ defmodule CodebattleWeb.UserController do
plug(CodebattleWeb.Plugs.RequireAuth when action in @all)

def index(conn, params) do
# TODO: add paginator
q = Map.get(params, "q", %{"sort" => %{"desc" => "rating"}})
sort_query = Map.get(q, "sort")

order =
Enum.map(sort_query, fn {key, value} -> {String.to_atom(key), String.to_atom(value)} end)

query = from(users in User, order_by: ^order, preload: [:user_games])
users = Repo.all(query)
page =
query
|> Repo.paginate(params)

direction =
case Map.keys(sort_query) |> List.first() do
"asc" -> "desc"
"desc" -> "asc"
end

render(conn, "index.html", users: users, direction: direction)
render(conn, "index.html", users: page.entries, page: page, direction: direction)
end

def show(conn, %{"id" => user_id}) do
Expand Down
10 changes: 8 additions & 2 deletions services/app/lib/codebattle_web/templates/user/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.text-center
h2.font-weight-normal= gettext "Users rating"
p = gettext "Total: "
= Enum.count(@users)
= @page.total_entries

table.table
thead.text-left
Expand All @@ -18,7 +18,11 @@
= for {user, index} <- Enum.with_index(@users) do
tr
td.p-3.align-middle
= index + 1
= if @page.page_number > 1 do
= index + 1 + (@page.page_number - 1) * 10
= else
= index + 1
end
td.text-left.p-3.align-middle
= link to: user_path(@conn, :show, user.id) do
img.attachment.rounded.mr-3[alt="#{user.name}" src="https://avatars0.githubusercontent.com/u/#{user.github_id}" style="width:50px"]
Expand All @@ -33,3 +37,5 @@
i.fa.fa-github
td.p-3.align-middle
= user.inserted_at

p = pagination_links @page, previous: Phoenix.HTML.raw("&leftarrow;"), next: Phoenix.HTML.raw("&rightarrow;"), view_style: :bootstrap_v4
1 change: 1 addition & 0 deletions services/app/lib/codebattle_web/views/user_view.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
defmodule CodebattleWeb.UserView do
use CodebattleWeb, :view
import Scrivener.HTML
end
6 changes: 5 additions & 1 deletion services/app/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ defmodule Codebattle.Mixfile do
:ueberauth,
:ueberauth_github,
:gproc,
:one_signal
:one_signal,
:scrivener_ecto,
:scrivener_html
]
]
end
Expand Down Expand Up @@ -82,6 +84,8 @@ defmodule Codebattle.Mixfile do
{:envy, "~> 1.1.1"},
{:one_signal, git: "https://github.com/vtm9/one_signal.git"},
{:paginator, "~> 0.6"},
{:scrivener_ecto, "~> 2.0"},
{:scrivener_html, git: "https://github.com/hlongvu/scrivener_html.git"},

# dev_and_test
{:credo, "~> 0.8", only: [:dev, :test], runtime: false},
Expand Down
4 changes: 4 additions & 0 deletions services/app/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
"mochiweb": {:hex, :mochiweb, "2.18.0", "eb55f1db3e6e960fac4e6db4e2db9ec3602cc9f30b86cd1481d56545c3145d2e", [:rebar3], [], "hexpm"},
"mock": {:hex, :mock, "0.3.2", "e98e998fd76c191c7e1a9557c8617912c53df3d4a6132f561eb762b699ef59fa", [:mix], [{:meck, "~> 0.8.8", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm"},
"neotoma": {:hex, :neotoma, "1.7.3", "d8bd5404b73273989946e4f4f6d529e5c2088f5fa1ca790b4dbe81f4be408e61", [:rebar], [], "hexpm"},
"oauth2": {:hex, :oauth2, "0.9.4", "632e8e8826a45e33ac2ea5ac66dcc019ba6bb5a0d2ba77e342d33e3b7b252c6e", [:mix], [{:hackney, "~> 1.7", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"one_signal": {:git, "https://github.com/vtm9/one_signal.git", "82ddb88798f8ce109eb6782909128e2b5bf1c1c5", []},
"paginator": {:hex, :paginator, "0.6.0", "bc2c01abdd98281ff39b6a7439cf540091122a7927bdaabc167c61d4508f9cbb", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm"},
Expand All @@ -49,6 +50,9 @@
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"postgrex": {:hex, :postgrex, "0.14.1", "63247d4a5ad6b9de57a0bac5d807e1c32d41e39c04b8a4156a26c63bcd8a2e49", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"},
"scrivener": {:hex, :scrivener, "2.5.0", "e1f78c62b6806d91cc9c4778deef1ea4e80aa9fadfce2c16831afe0468cc8a2c", [:mix], [], "hexpm"},
"scrivener_ecto": {:hex, :scrivener_ecto, "2.0.0", "e6ee5cc49b44e6115029185f07ffddd7e78475aa40394e98219800fed18f8e42", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:scrivener, "~> 2.4", [hex: :scrivener, repo: "hexpm", optional: false]}], "hexpm"},
"scrivener_html": {:git, "https://github.com/hlongvu/scrivener_html.git", "dd6418413739eb7f46e672d42f5c16acc391e499", []},
"slime": {:hex, :slime, "0.16.0", "4f9c677ca37b2817cd10422ecb42c524fe904d3630acf242b81dfe189900272a", [:mix], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"telemetry": {:hex, :telemetry, "0.3.0", "099a7f3ce31e4780f971b4630a3c22ec66d22208bc090fe33a2a3a6a67754a73", [:rebar3], [], "hexpm"},
Expand Down

0 comments on commit 835aabc

Please sign in to comment.