Skip to content

Commit

Permalink
fixed error with going past the final page
Browse files Browse the repository at this point in the history
  • Loading branch information
nwalker2398 committed Feb 16, 2024
1 parent 3c5a1e6 commit 2043250
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 53 deletions.
8 changes: 7 additions & 1 deletion app/controllers/api/v1/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,20 @@ def index
status = statuses.include?(param_status) ? statuses.index(param_status) : param_status
page = index_params[:page] ? Integer(index_params[:page]) : 1
identifier = index_params[:identifier]
last_page = false

resources = Resource
identifier && resources = resources.where(identifier: identifier)
status && status != 'any' && resources = resources.where(status: status)
last_page = per_page * (page - 1) < resources.order(:status).length && per_page * page >= resources.order(:status).length
resources = resources.limit(per_page).offset((page - 1) * per_page)
status && status != 'any' && resources = resources.order(:status)

puts last_page
render json:
resources.map(&:attributes)
{ resources: resources.map(&:attributes),
last_page: last_page
}
end

private
Expand Down
24 changes: 14 additions & 10 deletions app/javascript/components/resources/ResourceList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default function ResourceList() {
const [searchParams] = useSearchParams();
const navigate = useNavigate();
const [filteredResources, setFilteredResources] = useState([]);
const [pageState, setPageState] = useState({ identifier: '', status: 'Any', pageNumber: 1, per_page: 50});
const [pageState, setPageState] = useState({ identifier: '', status: 'Any', pageNumber: 1, per_page: 50 });
const [lastPage, setLastPage] = useState(false);

useEffect(() => {
console.log(searchParams.identifier)
Expand All @@ -22,15 +23,18 @@ export default function ResourceList() {
(async () => {
const response = await fetch(fetch_url);
const data = await response.json();
setFilteredResources(data);
})();

setFilteredResources(data['resources']);
setLastPage(data['last_page'])
})()
}, [searchParams]);

function setURL(identifier, status, page) {
function setURL(identifier, status, page, per_page) {
let url = '?';
if (identifier) { url = url + 'identifier=' + identifier + '&'};
if (status) { url = url + 'status=' + status + '&'};
if (page) { url = url + 'page=' + page};
if (page) { url = url + 'page=' + page + '&'};
if (per_page) { url = url + 'per_page=' + per_page};
navigate(url);
}

Expand All @@ -42,18 +46,18 @@ export default function ResourceList() {
if(status === 'undefined') {
status = '@undefined'
}
setURL(pageState.identifier, status, 1);
setURL(pageState.identifier, status, 1, pageState.per_page);
}

function nextPage() {
if(filteredResources.length == pageState.per_page) {
setURL(pageState.identifier, pageState.status, pageState.pageNumber + 1);
if(!lastPage) {
setURL(pageState.identifier, pageState.status, parseInt(pageState.pageNumber) + 1, pageState.per_page);
}
}

function prevPage() {
if(pageState.pageNumber > 1) {
setURL(pageState.identifier, pageState.status, pageState.pageNumber - 1);
if(parseInt(pageState.pageNumber) > 1) {
setURL(pageState.identifier, pageState.status, parseInt(pageState.pageNumber) - 1, pageState.per_page);
}
}

Expand Down
99 changes: 57 additions & 42 deletions spec/requests/api/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
end
get_with_auth '/api/v1/resources'
expect(response).to have_http_status(:success)
expected_response_json = resources.map do |resource|
{
expected_response_json =
{
'resources' => resources.map do |resource| {
'accessed_at' => nil,
'created_at' => resource.created_at.to_time.iso8601(3),
'error_message' => nil,
Expand All @@ -36,7 +37,9 @@
'updated_at' => resource.updated_at.to_time.iso8601(3),
'width' => resource.width
}
end
end,
'last_page' => true
}
expect(JSON.parse(response.body)).to eq(expected_response_json)
end

Expand All @@ -49,19 +52,23 @@
expect(response).to have_http_status(:success)
resource = resources[1]
expected_response_json =
[{ 'accessed_at' => nil,
'created_at' => resource.created_at.to_time.iso8601(3),
'error_message' => nil,
'featured_region' => resource.featured_region,
'height' => resource.height,
'id' => resources.index(resource) + 1,
'identifier' => resource.identifier,
'pcdm_type' => 'Image',
'source_uri' => resource.source_uri,
'status' => 'pending',
'updated_at' => resource.updated_at.to_time.iso8601(3),
'width' => resource.width
}]
{
'resources' => [{
'accessed_at' => nil,
'created_at' => resource.created_at.to_time.iso8601(3),
'error_message' => nil,
'featured_region' => resource.featured_region,
'height' => resource.height,
'id' => resources.index(resource) + 1,
'identifier' => resource.identifier,
'pcdm_type' => 'Image',
'source_uri' => resource.source_uri,
'status' => 'pending',
'updated_at' => resource.updated_at.to_time.iso8601(3),
'width' => resource.width
}],
'last_page' => true
}
expect(JSON.parse(response.body)).to eq(expected_response_json)
end

Expand All @@ -75,19 +82,23 @@
expect(response).to have_http_status(:success)
resource = resources[1]
expected_response_json =
[{ 'accessed_at' => nil,
'created_at' => resource.created_at.to_time.iso8601(3),
'error_message' => nil,
'featured_region' => resource.featured_region,
'height' => resource.height,
'id' => resources.index(resource) + 1,
'identifier' => resource.identifier,
'pcdm_type' => 'Image',
'source_uri' => resource.source_uri,
'status' => 'ready',
'updated_at' => resource.updated_at.to_time.iso8601(3),
'width' => resource.width
}]
{
'resources' => [{
'accessed_at' => nil,
'created_at' => resource.created_at.to_time.iso8601(3),
'error_message' => nil,
'featured_region' => resource.featured_region,
'height' => resource.height,
'id' => resources.index(resource) + 1,
'identifier' => resource.identifier,
'pcdm_type' => 'Image',
'source_uri' => resource.source_uri,
'status' => 'ready',
'updated_at' => resource.updated_at.to_time.iso8601(3),
'width' => resource.width
}],
'last_page' => true
}
expect(JSON.parse(response.body)).to eq(expected_response_json)
end

Expand All @@ -100,19 +111,23 @@
expect(response).to have_http_status(:success)
resource = resources[1]
expected_response_json =
[{ 'accessed_at' => nil,
'created_at' => resource.created_at.to_time.iso8601(3),
'error_message' => nil,
'featured_region' => resource.featured_region,
'height' => resource.height,
'id' => resources.index(resource) + 1,
'identifier' => resource.identifier,
'pcdm_type' => 'Image',
'source_uri' => resource.source_uri,
'status' => 'pending',
'updated_at' => resource.updated_at.to_time.iso8601(3),
'width' => resource.width
}]
{
'resources' => [{
'accessed_at' => nil,
'created_at' => resource.created_at.to_time.iso8601(3),
'error_message' => nil,
'featured_region' => resource.featured_region,
'height' => resource.height,
'id' => resources.index(resource) + 1,
'identifier' => resource.identifier,
'pcdm_type' => 'Image',
'source_uri' => resource.source_uri,
'status' => 'pending',
'updated_at' => resource.updated_at.to_time.iso8601(3),
'width' => resource.width
}],
'last_page' => true
}
expect(JSON.parse(response.body)).to eq(expected_response_json)
end

Expand Down

0 comments on commit 2043250

Please sign in to comment.