Skip to content

Commit

Permalink
Store pagination data in user settings
Browse files Browse the repository at this point in the history
  • Loading branch information
GeniJaho committed Mar 15, 2024
1 parent 8bbac34 commit 5b943f4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/Actions/Photos/FilterPhotosAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class FilterPhotosAction
/**
* @return LengthAwarePaginator<Photo>
*/
public function run(User $user, int $perPage): LengthAwarePaginator
public function run(User $user): LengthAwarePaginator
{
$photos = $user
->photos()
->filter($user->settings->photo_filters)
->withExists('items')
->latest('id')
->paginate($perPage)
->paginate($user->settings->per_page)
->withQueryString();

$photos->getCollection()->transform(function (Photo $photo) {
Expand Down
6 changes: 1 addition & 5 deletions app/DTO/UserSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

namespace App\DTO;

use Spatie\LaravelData\Attributes\Validation\BooleanType;
use Spatie\LaravelData\Attributes\Validation\Required;
use Spatie\LaravelData\Data;

class UserSettings extends Data
{
public function __construct(
#[Required, BooleanType]
public bool $picked_up_by_default = false,
#[Required, BooleanType]
public bool $recycled_by_default = false,
#[Required, BooleanType]
public bool $deposit_by_default = false,
public ?PhotoFilters $photo_filters = null,
public int $per_page = 12,
) {
}
}
12 changes: 7 additions & 5 deletions app/Http/Controllers/PhotosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ public function index(
} elseif ($request->boolean('clear_filters')) {
$user->settings->photo_filters = null;
$user->save();
} elseif ($request->boolean('set_per_page')) {
$perPage = in_array($request->integer('per_page'), [12, 24, 48, 96, 192])
? $request->integer('per_page')
: 12;
$user->settings->per_page = $perPage;
$user->save();
}

$perPage = in_array($request->integer('per_page'), [12, 24, 48, 96, 192])
? $request->integer('per_page')
: 12;

$photos = $filterPhotosAction->run($user, $perPage);
$photos = $filterPhotosAction->run($user);

$tagsAndItems = $getTagsAndItemsAction->run();

Expand Down
6 changes: 2 additions & 4 deletions resources/js/Pages/Photos/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const perPage = ref(perPageOptions.find(option => option.value === props.photos.
watch(perPage, (value) => {
router.get(window.location.pathname, {
set_per_page: true,
per_page: value.value,
});
});
Expand Down Expand Up @@ -99,10 +100,7 @@ const deletePhoto = (photoId) => {
const filter = (filters) => {
clearSelection();
router.get(window.location.pathname, {
...filters,
per_page: perPage.value.value,
});
router.get(window.location.pathname, filters);
}
</script>

Expand Down
12 changes: 11 additions & 1 deletion tests/Feature/Photos/ListPhotosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@
$this->actingAs($user = User::factory()->create());
Photo::factory(25)->for($user)->create();

$response = $this->get('/my-photos?per_page=24');
$response = $this->get('/my-photos?set_per_page=true&per_page=24');

$response->assertOk();
$response->assertInertia(fn (AssertableInertia $page) => $page
->component('Photos/Index')
->has('photos.data', 24)
->where('photos.per_page', 24)
->etc()
);

$response = $this->get('/my-photos');

$response->assertOk();
$response->assertInertia(fn (AssertableInertia $page) => $page
Expand Down

0 comments on commit 5b943f4

Please sign in to comment.