Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 24, 2023
1 parent 4c37122 commit d86c989
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 6 deletions.
2 changes: 1 addition & 1 deletion resources/views/table/filters.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<form method="GET" action="{{ URL::full() }}" id="{{ $key }}" class="app-card__actions" onchange="this.requestSubmit()">
<form method="GET" action="{{ $url }}" id="{{ $key }}" class="app-card__actions" onchange="this.requestSubmit()">
@if(! empty($filters))
<div class="data-table-filter" x-data="{ open: false }" x-on:click.outside="open = false">
<button
Expand Down
2 changes: 1 addition & 1 deletion resources/views/table/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class="form-check__control"
form="{{ $key }}"
class="form-control form-control--sm"
id="per_page"
name="per_page"
name="{{ $perPageKey }}"
onchange="this.form.requestSubmit()"
>
@foreach($perPageOptions as $option)
Expand Down
21 changes: 20 additions & 1 deletion src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cone\Root\Fields;

use Closure;
use Cone\Root\Filters\Filter;
use Cone\Root\Filters\RenderableFilter;
use Cone\Root\Filters\Search;
use Cone\Root\Filters\Sort;
Expand Down Expand Up @@ -361,6 +362,14 @@ protected function resolveField(Request $request, Field $field): void
}
}

/**
* Handle the callback for the filter resolution.
*/
protected function resolveFilter(Request $request, Filter $filter): void
{
$filter->setKey(sprintf('%s:%s', $this->getRequestKey(), $filter->getKey()));
}

/**
* Set the query resolver.
*/
Expand Down Expand Up @@ -438,6 +447,14 @@ public function getPerPageOptions(): array
return [5, 10, 15, 25];
}

/**
* Get the per page key.
*/
public function getPerPageKey(): string
{
return sprintf('%s:per_page', $this->getRequestKey());
}

/**
* The relations to be eagerload.
*/
Expand Down Expand Up @@ -469,7 +486,7 @@ public function paginate(Request $request, Model $model): LengthAwarePaginator
->with($this->with)
->withCount($this->withCount)
->latest();
})->paginate($request->input('per_page', 5))->withQueryString();
})->paginate($request->input($this->getPerPageKey(), 5))->withQueryString();
}

/**
Expand Down Expand Up @@ -627,6 +644,7 @@ public function toIndex(Request $request, Model $model): array
return $this->mapRelated($request, $model, $related);
}),
'perPageOptions' => $this->getPerPageOptions(),
'perPageKey' => $this->getPerPageKey(),
'filters' => $this->resolveFilters($request)
->authorized($request)
->renderable()
Expand All @@ -635,6 +653,7 @@ public function toIndex(Request $request, Model $model): array
})
->all(),
'activeFilters' => $this->resolveFilters($request)->active($request)->count(),
'url' => $this->modelUrl($model),
]);
}

Expand Down
23 changes: 22 additions & 1 deletion src/Filters/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,38 @@ abstract class Filter
use Authorizable;
use Makeable;

/**
* The filter key.
*/
protected string $key;

/**
* Create a new filter instance.
*/
public function __construct()
{
$this->key = Str::of(static::class)->classBasename()->snake()->value();
}

/**
* Apply the filter on the query.
*/
abstract public function apply(Request $request, Builder $query, mixed $value): Builder;

/**
* Set the filter key.
*/
public function setKey(string $key): void
{
$this->key = $key;
}

/**
* Get the key.
*/
public function getKey(): string
{
return Str::of(static::class)->classBasename()->snake()->value();
return $this->key;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Filters/MediaSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Cone\Root\Filters;

use Cone\Root\Fields\Fields;

class MediaSearch extends Search
{
/**
Expand All @@ -17,6 +19,8 @@ class MediaSearch extends Search
public function __construct(array $attributes = ['file_name'])
{
$this->attributes = $attributes;

parent::__construct(new Fields());
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Filters/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Search extends RenderableFilter
public function __construct(Fields $fields)
{
$this->fields = $fields;

parent::__construct();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Filters/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Sort extends Filter
public function __construct(Fields $fields)
{
$this->fields = $fields;

parent::__construct();
}

/**
Expand Down
25 changes: 23 additions & 2 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cone\Root\Actions\Action;
use Cone\Root\Fields\Field;
use Cone\Root\Fields\Relation;
use Cone\Root\Filters\Filter;
use Cone\Root\Filters\RenderableFilter;
use Cone\Root\Filters\Search;
use Cone\Root\Filters\Sort;
Expand Down Expand Up @@ -268,6 +269,14 @@ protected function resolveField(Request $request, Field $field): void
$field->resolveErrorsUsing(fn (Request $request): MessageBag => $this->errors($request));
}

/**
* Handle the callback for the filter resolution.
*/
protected function resolveFilter(Request $request, Filter $filter): void
{
$filter->setKey(sprintf('%s:%s', $this->getKey(), $filter->getKey()));
}

/**
* Handle the callback for the action resolution.
*/
Expand All @@ -289,14 +298,22 @@ public function getPerPageOptions(): array
->toArray();
}

/**
* Get the per page key.
*/
public function getPerPageKey(): string
{
return sprintf('%s:per_page', $this->getKey());
}

/**
* Perform the query and the pagination.
*/
public function paginate(Request $request): LengthAwarePaginator
{
return $this->resolveFilteredQuery($request)
->latest()
->paginate($request->input('per_page'))
->paginate($request->input($this->getPerPageKey()))
->withQueryString()
->through(function (Model $model) use ($request): array {
return [
Expand Down Expand Up @@ -376,6 +393,7 @@ public function toIndex(Request $request): array
->visible('index')
->toArray(),
'perPageOptions' => $this->getPerPageOptions(),
'perPageKey' => $this->getPerPageKey(),
'filters' => $this->resolveFilters($request)
->authorized($request)
->renderable()
Expand All @@ -384,6 +402,7 @@ public function toIndex(Request $request): array
})
->all(),
'activeFilters' => $this->resolveFilters($request)->active($request)->count(),
'url' => trim(sprintf('%s?%s', $this->getUri(), $request->getQueryString()), '?'),
]);
}

Expand Down Expand Up @@ -431,7 +450,9 @@ public function toShow(Request $request, Model $model): array
->subResource()
->authorized($request, $model)
->map(static function (Relation $relation) use ($request, $model): array {
return $relation->toSubResource($request, $model);
return array_merge($relation->toSubResource($request, $model), [
'url' => trim(sprintf('%s?%s', $relation->modelUrl($model), $request->getQueryString()), '?'),
]);
}),
]);
}
Expand Down

0 comments on commit d86c989

Please sign in to comment.