move navigation to javascript

pull/1974/head
Ayrat Badykov 6 years ago
parent 44643f9ad8
commit 648b808bbb
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 31
      apps/block_scout_web/assets/js/lib/async_listing_load.js
  2. 39
      apps/block_scout_web/lib/block_scout_web/controllers/address_transaction_controller.ex

@ -53,13 +53,15 @@ export const asyncInitialState = {
/* link to the next page */ /* link to the next page */
nextPagePath: null, nextPagePath: null,
/* link to the previous page */ /* link to the previous page */
prevPagePath: null prevPagePath: null,
/* visited pages */
pagesStack: []
} }
export function asyncReducer (state = asyncInitialState, action) { export function asyncReducer (state = asyncInitialState, action) {
switch (action.type) { switch (action.type) {
case 'ELEMENTS_LOAD': { case 'ELEMENTS_LOAD': {
return Object.assign({}, state, { nextPagePath: action.nextPagePath, prevPagePath: action.prevPagePath }) return Object.assign({}, state, { nextPagePath: action.nextPagePath })
} }
case 'ADD_ITEM_KEY': { case 'ADD_ITEM_KEY': {
return Object.assign({}, state, { itemKey: action.itemKey }) return Object.assign({}, state, { itemKey: action.itemKey })
@ -79,22 +81,41 @@ export function asyncReducer (state = asyncInitialState, action) {
loadingFirstPage: false loadingFirstPage: false
}) })
} }
case 'ITEMS_FETCHED': { case 'ITEMS_FETCHED': {
var prevPagePath = null
console.log(state.pagesStack)
if (state.pagesStack.length >= 2) {
prevPagePath = state.pagesStack[state.pagesStack.length - 2]
}
console.log(state.pagesStack)
return Object.assign({}, state, { return Object.assign({}, state, {
requestError: false, requestError: false,
items: action.items, items: action.items,
nextPagePath: action.nextPagePath, nextPagePath: action.nextPagePath,
prevPagePath: action.prevPagePath prevPagePath: prevPagePath
}) })
} }
case 'NAVIGATE_TO_OLDER': { case 'NAVIGATE_TO_OLDER': {
console.log('NAVIGATE_TO_OLDER')
history.replaceState({}, null, state.nextPagePath) history.replaceState({}, null, state.nextPagePath)
if (state.pagesStack.length == 0) {
state.pagesStack.push(window.location.href.split('?')[0])
}
state.pagesStack.push(state.nextPagePath)
return Object.assign({}, state, { beyondPageOne: true }) return Object.assign({}, state, { beyondPageOne: true })
} }
case 'NAVIGATE_TO_NEWER': { case 'NAVIGATE_TO_NEWER': {
history.replaceState({}, null, state.prevPagePath) history.replaceState({}, null, state.prevPagePath)
state.pagesStack.pop()
return Object.assign({}, state, { beyondPageOne: true }) return Object.assign({}, state, { beyondPageOne: true })
} }
default: default:

@ -7,7 +7,6 @@ defmodule BlockScoutWeb.AddressTransactionController do
import BlockScoutWeb.AddressController, only: [transaction_count: 1, validation_count: 1] import BlockScoutWeb.AddressController, only: [transaction_count: 1, validation_count: 1]
import BlockScoutWeb.Chain, only: [current_filter: 1, paging_options: 1, next_page_params: 3, split_list_by_page: 1] import BlockScoutWeb.Chain, only: [current_filter: 1, paging_options: 1, next_page_params: 3, split_list_by_page: 1]
import BlockScoutWeb.PaginationHelpers
alias BlockScoutWeb.TransactionView alias BlockScoutWeb.TransactionView
alias Explorer.{Chain, Market} alias Explorer.{Chain, Market}
@ -38,7 +37,6 @@ defmodule BlockScoutWeb.AddressTransactionController do
results_plus_one = Chain.address_to_transactions_with_rewards(address, options) results_plus_one = Chain.address_to_transactions_with_rewards(address, options)
{results, next_page} = split_list_by_page(results_plus_one) {results, next_page} = split_list_by_page(results_plus_one)
cur_page_number = current_page_number(params)
next_page_url = next_page_url =
case next_page_params(next_page, results, params) do case next_page_params(next_page, results, params) do
@ -46,18 +44,11 @@ defmodule BlockScoutWeb.AddressTransactionController do
nil nil
next_page_params -> next_page_params ->
next_params =
add_navigation_params(
Map.delete(next_page_params, "type"),
cur_page_path(conn, address, Map.delete(next_page_params, "type")),
cur_page_number
)
address_transaction_path( address_transaction_path(
conn, conn,
:index, :index,
address, address,
next_params Map.delete(next_page_params, "type")
) )
end end
@ -83,12 +74,7 @@ defmodule BlockScoutWeb.AddressTransactionController do
end end
end) end)
json(conn, %{ json(conn, %{items: items_json, next_page_path: next_page_url})
items: items_json,
next_page_path: next_page_url,
prev_page_path: params["prev_page_path"],
cur_page_number: cur_page_number
})
else else
:error -> :error ->
unprocessable_entity(conn) unprocessable_entity(conn)
@ -101,8 +87,6 @@ defmodule BlockScoutWeb.AddressTransactionController do
def index(conn, %{"address_id" => address_hash_string} = params) do def index(conn, %{"address_id" => address_hash_string} = params) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string), with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash) do {:ok, address} <- Chain.hash_to_address(address_hash) do
cur_page_number = current_page_number(params)
render( render(
conn, conn,
"index.html", "index.html",
@ -112,8 +96,7 @@ defmodule BlockScoutWeb.AddressTransactionController do
filter: params["filter"], filter: params["filter"],
transaction_count: transaction_count(address), transaction_count: transaction_count(address),
validation_count: validation_count(address), validation_count: validation_count(address),
current_path: current_path(conn), current_path: current_path(conn)
cur_page_number: cur_page_number
) )
else else
:error -> :error ->
@ -123,20 +106,4 @@ defmodule BlockScoutWeb.AddressTransactionController do
not_found(conn) not_found(conn)
end end
end end
defp cur_page_path(conn, address, %{"block_number" => _, "index" => _} = params) do
# new_params = Map.put(params, "next_page", false)
if params["prev_page_path"] do
params["prev_page_path"] |> URI.decode()
else
address_transaction_path(
conn,
:index,
address
)
end
end
defp cur_page_path(conn, address, params), do: address_transaction_path(conn, :index, address, params)
end end

Loading…
Cancel
Save