Skip to content

Commit

Permalink
Merge pull request #419
Browse files Browse the repository at this point in the history
Add pagination
  • Loading branch information
portableant authored May 26, 2022
2 parents 29c1a36 + 7907c19 commit 05892f1
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 23 deletions.
14 changes: 8 additions & 6 deletions app/Http/Controllers/researchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ public function index(): View
}

/**
* @param Request $request
* @return View
*/
public function projects(): View
public function projects(Request $request): View
{
$projects = ResearchProjects::list();
$projects = ResearchProjects::list($request);
return view('research.projects', compact('projects'));
}

Expand All @@ -74,11 +75,12 @@ public function project(string $slug): View|Response
}

/**
* @param Request $request
* @return View
*/
public function profiles(): View
public function profiles(Request $request): View
{
$profiles = StaffProfiles::list();
$profiles = StaffProfiles::list($request);
return view('research.profiles', compact('profiles'));
}

Expand Down Expand Up @@ -137,9 +139,9 @@ public function resource(string $slug): View|Response
/**
* @return View
*/
public function resources(): View
public function resources(Request $request): View
{
$resources = OnlineResources::list();
$resources = OnlineResources::list($request);
return view('research.resources', compact('resources'));
}

Expand Down
19 changes: 15 additions & 4 deletions app/Models/OnlineResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,36 @@
namespace App\Models;

use App\DirectUs;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

class OnlineResources extends Model
{
/**
* @return array
* @param Request $request
* @return LengthAwarePaginator
*/
public static function list(): array
public static function list(Request $request): LengthAwarePaginator
{
$perPage = 12;
$offset = ($request['page'] - 1) * $perPage;
$api = new DirectUs;
$api->setEndpoint('online_resources');
$api->setArguments(
array(
'fields' => '*.*.*.*',
'limit' => 100,
'limit' => $perPage,
'offset' => $offset,
'meta' => '*',
'sort' => 'id'
)
);
return $api->getData();
$resources = $api->getData();
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$total = $resources['meta']['total_count'];
$paginator = new LengthAwarePaginator($resources, $total, $perPage, $currentPage);
$paginator->setPath(route('resources'));
return $paginator;
}

/**
Expand Down
22 changes: 16 additions & 6 deletions app/Models/ResearchProjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,37 @@
namespace App\Models;

use App\DirectUs;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

class ResearchProjects extends Model
{
/**
* @param Request $request
* @param string $sort
* @param int $limit
* @return array
* @return LengthAwarePaginator
*/
public static function list(string $sort = 'title', int $limit = 100): array
public static function list(Request $request, string $sort = 'title'): LengthAwarePaginator
{
$perPage = 12;
$offset = ($request['page'] - 1) * $perPage;
$api = new DirectUs;
$api->setEndpoint('research_projects');
$api->setArguments(
array(
'fields' => '*.*.*.*',
'meta' => 'result_count,total_count,type',
'meta' => '*',
'sort' => $sort,
'limit' => $limit
'limit' => $perPage,
'offset' => $offset
)
);
return $api->getData();
$projects = $api->getData();
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$total = $projects['meta']['total_count'];
$paginator = new LengthAwarePaginator($projects, $total, $perPage, $currentPage);
$paginator->setPath(route('research-projects'));
return $paginator;
}

/**
Expand Down
18 changes: 14 additions & 4 deletions app/Models/StaffProfiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,31 @@
class StaffProfiles extends Model
{
/**
* @return array
* @param Request $request
* @return LengthAwarePaginator
*/
public static function list(): array
public static function list(Request $request): LengthAwarePaginator
{
$perPage = 12;
$offset = ($request['page'] - 1) * $perPage;
$api = new DirectUs;
$api->setEndpoint('staff_profiles');
$api->setArguments(
array(
'fields' => '*.*.*.*',
'meta' => 'result_count,total_count,type',
'meta' => '*',
'limit' => $perPage,
'offset' => $offset,
'sort' => 'last_name',
'filter[research_active][in]' => 'yes'
)
);
return $api->getData();
$staff = $api->getData();
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$total = $staff['meta']['filter_count'];
$paginator = new LengthAwarePaginator($staff, $total, $perPage, $currentPage);
$paginator->setPath(route('research-profiles'));
return $paginator;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion resources/views/research/profiles.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@section('content')
<div class="row">
@foreach($profiles['data'] as $profile)
@foreach($profiles->items()['data'] as $profile)
<x-image-card
:altTag="$profile['profile_image_alt_text']"
:title="$profile['display_name']"
Expand All @@ -17,4 +17,6 @@
</x-image-card>
@endforeach
</div>
{{ $profiles->appends(request()->except('page'))->links() }}

@endsection
3 changes: 2 additions & 1 deletion resources/views/research/projects.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@section('description', 'An overview of Fitzwilliam Museum research projects')
@section('content')
<div class="row">
@foreach($projects['data'] as $project)
@foreach($projects->items()['data'] as $project)
<x-image-card
:altTag="$project['hero_image_alt_text']"
:title="$project['title']"
Expand All @@ -14,4 +14,5 @@
:params="[$project['slug']]"></x-image-card>
@endforeach
</div>
{{ $projects->appends(request()->except('page'))->links() }}
@endsection
4 changes: 3 additions & 1 deletion resources/views/research/resources.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@section('description', 'An overview of Fitzwilliam legacy digital resources')
@section('content')
<div class="row">
@foreach($resources['data'] as $project)
@foreach($resources->items()['data'] as $project)
<x-image-card
:altTag="$project['hero_image_alt_text']"
:title="$project['title']"
Expand All @@ -14,4 +14,6 @@
:params="[$project['slug']]"></x-image-card>
@endforeach
</div>
{{ $resources->appends(request()->except('page'))->links() }}

@endsection

0 comments on commit 05892f1

Please sign in to comment.