Skip to content

Commit 47f41b1

Browse files
author
midesweb
committed
crud list blade component
1 parent 75dcf2c commit 47f41b1

File tree

6 files changed

+131
-1
lines changed

6 files changed

+131
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { LitElement, html, css } from 'lit';
2+
import '@dile/crud/components/crud/crud.js';
3+
import { CrudConfigBuilder } from '@dile/crud/lib/CrudConfigBuilder';
4+
import { MyResponseApiAdapter } from './responseApiAdapter.js';
5+
6+
7+
export class ApiKitCrud extends LitElement {
8+
static styles = [
9+
css`
10+
:host {
11+
display: block;
12+
}
13+
`
14+
];
15+
16+
static get properties() {
17+
return {
18+
endpoint: { type: String },
19+
config: { type: Object },
20+
generatedConfig: { type: Object },
21+
};
22+
}
23+
24+
firstUpdated() {
25+
this.config = {
26+
...this.config,
27+
responseAdapter: new MyResponseApiAdapter(),
28+
}
29+
this.generatedConfig = new CrudConfigBuilder(this.endpoint, this.config).getConfig();
30+
this.generatedConfig.templates.item = this.getItemTemplate();
31+
}
32+
33+
render() {
34+
return html`
35+
${this.generatedConfig
36+
? html`
37+
<dile-crud
38+
.config="${this.generatedConfig}">
39+
</dile-crud>
40+
`
41+
: html`<p>Loading...</p>`}
42+
`;
43+
}
44+
45+
getItemTemplate() {
46+
47+
}
48+
}
49+
customElements.define('api-kit-crud', ApiKitCrud);

resources/assets/js/api-kit.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { ApiKitCrud } from './api-kit-crud.js';
2+
import { html } from 'lit';
3+
4+
window.litHtml = html;
5+
window.ClassApiKitCrud = ApiKitCrud;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ResponseApiAdapter } from '@dile/crud/lib/ResponseApiAdapter';
2+
3+
export class MyResponseApiAdapter extends ResponseApiAdapter {
4+
getElementList() {
5+
return this.response.data.result.data;
6+
}
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
document.addEventListener('DOMContentLoaded', () => {
3+
class CrudListComponent extends ClassApiKitCrud {
4+
getItemTemplate(item) {
5+
return (item) => litHtml`@if($itemElement) <{{ $itemElement }} .item="${item}"></{{ $itemElement }}> @elseif(count($displayProperties) === 0) Set $itemElement component or $displayProperties array to show item content @else @foreach($displayProperties as $property) ${ item.{{ $property }} } @if(!$loop->last) - @endif @endforeach @endif`;
6+
}
7+
}
8+
customElements.define('crud-list-component', CrudListComponent);
9+
});
10+
</script>
11+
12+
<crud-list-component
13+
endpoint="{{ $endpoint }}"
14+
config="{{ json_encode($config) }}"
15+
></crud-list-component>

src/APIKitServiceProvider.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,24 @@ class APIKitServiceProvider extends ServiceProvider
1010
{
1111
public function boot(): void
1212
{
13-
// ...
13+
// Load views
14+
$this->loadViewsFrom(__DIR__.'/../resources/views', 'api-kit');
15+
16+
if ($this->app->runningInConsole()) {
17+
// Publish views
18+
$this->publishes([
19+
__DIR__.'/../resources/views' => resource_path('views/vendor/api-kit'),
20+
], 'api-kit-views');
21+
22+
$this->publishes([
23+
__DIR__.'/../resources/assets' => resource_path('vendor/api-kit'),
24+
], 'api-kit-assets');
25+
}
26+
27+
// Register Blade components
28+
$this->loadViewComponentsAs('api-kit', [
29+
\EscuelaIT\APIKit\View\Components\CrudList::class,
30+
]);
1431
}
1532

1633
public function register(): void

src/View/Components/CrudList.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace EscuelaIT\APIKit\View\Components;
6+
7+
use Illuminate\Support\Str;
8+
use Illuminate\View\Component;
9+
10+
class CrudList extends Component
11+
{
12+
public array $config;
13+
14+
public function __construct(
15+
public string $endpoint,
16+
public $itemElement = null,
17+
public $displayProperties = [],
18+
public ?array $filters = null,
19+
public ?array $sort = null,
20+
) {
21+
22+
$this->config = [
23+
'availableFilters' => $this->filters ?? [],
24+
'sort' => $this->sort ?? null,
25+
'customization' => [
26+
'disableFilter' => $this->filters ? false : true,
27+
'disableSort' => $this->sort ? false : true,
28+
'disablePagination' => false,
29+
]
30+
];
31+
}
32+
33+
public function render()
34+
{
35+
return view('api-kit::components.crud-list');
36+
}
37+
}

0 commit comments

Comments
 (0)