Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 16, 2023
1 parent 3a4d286 commit c08dcc2
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 36 deletions.
2 changes: 1 addition & 1 deletion resources/views/resources/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
</div>
@endif

@include('root::resources.table.table')
@include('root::table.table')
@endsection
3 changes: 3 additions & 0 deletions resources/views/resources/relation.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<turbo-frame id="relation-{{ $attribute }}">
@include('root::table.table')
</turbo-frame>
19 changes: 18 additions & 1 deletion resources/views/resources/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,30 @@
@foreach($fields as $field)
<tr>
<th>{{ $field['label'] }}</th>
@include('root::resources.table.cell', $field)
@include('root::table.cell', $field)
</tr>
@endforeach
</table>
</div>
</div>
</div>
</div>

@if($model->exists)
<div class="l-row__column">
@foreach($relations as $relation)
<turbo-frame id="relation-{{ $relation['attribute'] }}" src="{{ $relation['url'] }}">
<div class="app-card">
<div class="app-card__header">
<h2 class="app-card__title">{{ $relation['label'] }}</h2>
</div>
<div class="app-card__body">
Loading...
</div>
</div>
</turbo-frame>
@endforeach
</div>
@endif
</div>
@endsection
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<div class="app-card" x-data="{ selection: [], all: false }">
<div class="app-card__header">
<h2 class="app-card__title">{{ $title }}</h2>
@include('root::resources.table.filters')
@include('root::table.filters')
</div>
<div class="app-card__body">
<div class="data-table">
@includeWhen(! empty($actions), 'root::resources.table.actions')
@includeWhen(! empty($actions), 'root::table.actions')
@if($data->isNotEmpty())
<div class="table-responsive">
<table class="table table--hover">
Expand All @@ -20,7 +20,7 @@
</th>
@endif
@foreach($data[0]['fields'] as $column)
@include('root::resources.table.column', $column)
@include('root::table.column', $column)
@endforeach
<th scope="col">
<span class="sr-only">{{ __('Actions') }}</span>
Expand All @@ -43,7 +43,7 @@ class="form-check__control"
</td>
@endif
@foreach($row['fields'] as $cell)
@include('root::resources.table.cell', $cell)
@include('root::table.cell', $cell)
@endforeach
<td>
<div class="data-table__actions">
Expand Down Expand Up @@ -99,7 +99,7 @@ class="form-control form-control--sm"
</div>
<p>{{ __('Showing :from to :to of :total results', ['from' => $data->firstItem(), 'to' => $data->lastItem(), 'total' => $data->total()]) }}</p>
</div>
{!! $data->links('root::resources.table.pagination') !!}
{!! $data->links('root::table.pagination') !!}
</div>
@else
<x-root::alert>
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/ID.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public function __construct(string $label = 'ID', string $modelAttribute = 'id')
{
parent::__construct($label, $modelAttribute);

$this->hiddenOn(['crate', 'update']);
$this->hiddenOn(['create', 'update']);
}
}
6 changes: 3 additions & 3 deletions src/Fields/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Cone\Root\Http\Controllers\MediaController;
use Cone\Root\Models\Medium;
use Cone\Root\Traits\HasMedia;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
Expand Down Expand Up @@ -119,7 +120,7 @@ public function resolveFilters(Request $request): Filters
/**
* Paginate the results.
*/
public function paginate(Request $request, Model $model): array
public function paginate(Request $request, Model $model): LengthAwarePaginator
{
return $this->resolveFilters($request)
->apply($request, $this->resolveRelatableQuery($request, $model))
Expand All @@ -133,8 +134,7 @@ public function paginate(Request $request, Model $model): array
return array_merge($option, [
'html' => View::make('root::fields.file-option', $option)->render(),
]);
})
->toArray();
});
}

/**
Expand Down
59 changes: 47 additions & 12 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

use Closure;
use Cone\Root\Filters\RenderableFilter;
use Cone\Root\Http\Controllers\RelationController;
use Cone\Root\Interfaces\Form;
use Cone\Root\Root;
use Cone\Root\Traits\AsForm;
use Cone\Root\Traits\RegistersRoutes;
use Cone\Root\Traits\ResolvesActions;
use Cone\Root\Traits\ResolvesFields;
use Cone\Root\Traits\ResolvesFilters;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation as EloquentRelation;
Expand All @@ -17,8 +21,9 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

abstract class Relation extends Field
abstract class Relation extends Field // implements Form
{
// use AsForm;
use ResolvesActions;
use ResolvesFilters;
use ResolvesFields;
Expand Down Expand Up @@ -132,6 +137,14 @@ public function getUriKey(): string
return str_replace('.', '-', $this->getRequestKey());
}

/**
* Get the route parameter name.
*/
public function getRouteParameterName(): string
{
return 'field';
}

/**
* Set the as subresource attribute.
*/
Expand Down Expand Up @@ -351,14 +364,35 @@ public function getPerPageOptions(): array
return [5, 10, 15, 25];
}

/**
* Resolve the filtered query for the given request.
*/
public function resolveFilteredQuery(Request $request, Model $model): Builder
{
return $this->resolveFilters($request)->apply($request, $this->getRelation($model)->getQuery());
}

/**
* Paginate the results.
*/
public function paginate(Request $request, Model $model): array
public function paginate(Request $request, Model $model): LengthAwarePaginator
{
return [
//
];
return $this->resolveFilteredQuery($request, $model)
->latest()
->paginate($request->input('per_page'))
->withQueryString()
->through(function (Model $model) use ($request): array {
return [
'id' => $model->getKey(),
'url' => '',
'model' => $model,
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $model)
->visible('relation.index')
->mapToDisplay($request, $model),
];
});
}

/**
Expand All @@ -380,7 +414,7 @@ public function registerRoutes(Request $request, Router $router): void
public function routes(Router $router): void
{
if ($this->isSubResource()) {
//
$router->get('/', [RelationController::class, 'index']);
}
}

Expand Down Expand Up @@ -408,13 +442,13 @@ public function toInput(Request $request, Model $model): array
}

/**
* Get the sub reosurce representation of the relation.
* Get the sub resource representation of the relation.
*/
public function toSubResource(Request $request, Model $model): array
{
return [
//
];
return array_merge($this->toArray(), [
'url' => sprintf('%s?model=%s', $this->getUri(), $model->getRouteKey()),
]);
}

/**
Expand All @@ -423,10 +457,11 @@ public function toSubResource(Request $request, Model $model): array
public function toIndex(Request $request, Model $model): array
{
return array_merge($this->toArray(), [
'title' => $this->getRelatedName(),
'key' => $this->modelAttribute,
'title' => $this->label,
'actions' => $this->resolveActions($request)
->authorized($request, $model)
->visible('index')
->visible('relation.index')
->mapToForms($request, $model),
'data' => $this->paginate($request, $model),
'perPageOptions' => $this->getPerPageOptions(),
Expand Down
28 changes: 15 additions & 13 deletions src/Http/Controllers/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Cone\Root\Http\Controllers;

use Cone\Root\Http\Requests\CreateRequest;
use Cone\Root\Http\Requests\IndexRequest;
use Cone\Root\Http\Requests\ResourceRequest;
use Cone\Root\Http\Requests\ShowRequest;
use Cone\Root\Http\Requests\UpdateRequest;
Expand All @@ -12,23 +11,26 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Redirect;
use Inertia\Inertia;
use Inertia\Response;
use Illuminate\Support\Facades\Response as ResponseFactory;

class HasOneOrManyController extends Controller
class RelationController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request): Response
{
$field = $request->resolved();
$resource = $request->route('_resource');

return Inertia::render(
'Relations/Index',
$field->toIndex($request, $model)
);
$field = $request->route('field');

// Gate::allowIf($field->authorized($request, $model));

$model = $resource->resolveRouteBinding($request, $request->input('model', ''));

return ResponseFactory::view('root::resources.relation', $field->toIndex($request, $model));
}

/**
Expand All @@ -55,7 +57,7 @@ public function create(CreateRequest $request, Model $model): Response
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Http\RedirectResponse
*/
public function store(CreateRequest $request, Model $model): RedirectResponse
public function store(Request $request, Model $model): RedirectResponse
{
$field = $request->resolved();

Expand Down Expand Up @@ -86,7 +88,7 @@ public function store(CreateRequest $request, Model $model): RedirectResponse
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Inertia\Response
*/
public function show(ShowRequest $request, Model $model, Model $related): Response
public function show(Request $request, Model $model, Model $related): Response
{
return Inertia::render(
'Relations/Show',
Expand All @@ -101,7 +103,7 @@ public function show(ShowRequest $request, Model $model, Model $related): Respon
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Inertia\Response
*/
public function edit(UpdateRequest $request, Model $model, Model $related): Response
public function edit(Request $request, Model $model, Model $related): Response
{
$field = $request->resolved();

Expand Down Expand Up @@ -147,7 +149,7 @@ public function update(UpdateRequest $request, Model $model, Model $related): Re
* @param \Illuminate\Database\Eloquent\Model $related
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(ResourceRequest $request, Model $model, Model $related): RedirectResponse
public function destroy(Request $request, Model $model, Model $related): RedirectResponse
{
$trashed = class_uses_recursive(SoftDeletes::class) && $related->trashed();

Expand Down

0 comments on commit c08dcc2

Please sign in to comment.