Skip to content

Commit

Permalink
index filter, sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Advaith3600 committed Apr 4, 2022
1 parent 1bfd243 commit 60e103b
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 83 deletions.
32 changes: 20 additions & 12 deletions src/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Exception;
use Illuminate\Http\Request;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Advaith\SeamlessAdmin\ModelResolver;

class AdminController extends Controller
{
private $resolver;
private ModelResolver $resolver;

public function __construct()
{
Expand All @@ -17,7 +20,7 @@ public function __construct()
$this->resolver = app('modelResolver');
}

public function welcome()
public function welcome(): View
{
return view('seamless::welcome');
}
Expand All @@ -32,15 +35,20 @@ private function resolveType(string $type): string
return $type;
}

public function index($type)
public function index($type): View
{
$type = $this->resolveType($type);

$fillable = collect((new $type)->getFillable())->diff((new $type)->getHidden());

$data = $type::orderBy('id', 'desc')
$data = $type::orderBy(request()->by ?? 'id', request()->order ?? 'desc')
->when(request()->q, function ($query) use ($fillable) {
$search = request()->q;
foreach ($fillable as $column)
$query->orWhere($column, 'like', "%{$search}%");
})
->select((clone $fillable)->add('id')->toArray())
->paginate(10);
->paginate(request()->perPage ?? 10);

return view('seamless::type.index', [
'type' => $type,
Expand All @@ -49,7 +57,7 @@ public function index($type)
]);
}

public function show($type, $id)
public function show($type, $id): View
{
$type = $this->resolveType($type);

Expand All @@ -67,7 +75,7 @@ public function show($type, $id)
]);
}

public function create($type)
public function create($type): View
{
$type = $this->resolveType($type);

Expand All @@ -77,7 +85,7 @@ public function create($type)
]);
}

public function store($type, Request $request)
public function store($type, Request $request): RedirectResponse | string
{
$type = $this->resolveType($type);

Expand All @@ -91,7 +99,7 @@ public function store($type, Request $request)
}
}

public function edit($type, $id)
public function edit($type, $id): View
{
$type = $this->resolveType($type);

Expand All @@ -112,7 +120,7 @@ public function edit($type, $id)
]);
}

public function update($type, $id, Request $request)
public function update($type, $id, Request $request): RedirectResponse | string
{
$type = $this->resolveType($type);

Expand All @@ -126,7 +134,7 @@ public function update($type, $id, Request $request)
}
}

public function delete($type, Request $request)
public function delete($type, Request $request): View
{
$ids = $request->ids;

Expand All @@ -138,7 +146,7 @@ public function delete($type, Request $request)
]);
}

public function destroy($type, Request $request)
public function destroy($type, Request $request): RedirectResponse | string
{
$type = $this->resolveType($type);
$ids = $request->ids;
Expand Down
2 changes: 1 addition & 1 deletion src/resources/assets/css/app.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/resources/assets/js/type-index.js

Large diffs are not rendered by default.

45 changes: 32 additions & 13 deletions src/resources/assets/src/js/type-index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
import { createApp } from 'vue';

document.querySelectorAll('[data-link]').forEach(elem => {
elem.addEventListener('click', event => {
event.preventDefault();
location.href = elem.dataset.link;
});
});

document.querySelectorAll('[data-link] a').forEach(elem => {
elem.addEventListener('click', event => {
event.stopPropagation();
});
});

createApp({
data() {
return {
Expand All @@ -27,6 +14,38 @@ createApp({
checkAll(ids) {
if (this.selected.size === ids.length) this.selected = new Set;
else this.selected = new Set(ids)
},
massDeleteURI() {
return encodeURI([...this.selected].map((u, i) => `ids[${i}]=${u}`).join('&'));
},
redirect(url) {
location.href = url;
},
sort(by, order) {
const query = {};
decodeURIComponent(location.search)
.slice(1)
.split('&')
.forEach(item => {
if (item === '') return;
const _query = item.split('=');
query[_query[0]] = _query[1];
});

if (by === query['by'] && order === 'desc') {
delete query['by'];
delete query['order'];
} else {
if (query['by'] === by) query['order'] = order === 'asc' ? 'desc' : 'asc';
else query['order'] = 'asc';
query['by'] = by;
}

const array = [];
for (const key of Object.keys(query))
array.push(`${key}=${query[key]}`);

location.search = `?${array.join('&')}`;
}
}
}).mount('#app')
40 changes: 39 additions & 1 deletion src/resources/assets/src/scss/_helper.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$green: #27ae60;
$green: #20a056;
$grey: #444;
$yellow: #e4cf1b;
$red: #cc2626;
Expand Down Expand Up @@ -74,3 +74,41 @@ table {
padding: 6px 12px;
}
}

.search {
position: relative;

input {
padding-right: 34px;
}

.icon {
position: absolute;
top: 50%;
right: 1rem;
transform: translateY(-50%);
cursor: pointer;

svg { width: 16px; }
}
}

.sort {
cursor: pointer;

svg {
width: 14px;
height: 14px;

$scale: 1.2;
$offset: 1px;
&.up { transform: translateY($offset) scale($scale); }
&.down { transform: translateY(-$offset) scale($scale); }
}

$opacity: 0.4;
&.up svg.down { opacity: $opacity; }
&.down svg.up { opacity: $opacity; }

&.light svg { opacity: $opacity; }
}
10 changes: 8 additions & 2 deletions src/resources/views/type/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ class="input"
</div>

<div class="flex gap-2 mt-4 justify-end">
<a href="{{ route('admin.type.index', request()->type) }}" class="btn grey">Cancel</a>
<button class="btn" type="submit">Create</button>
<a href="{{ route('admin.type.index', request()->type) }}" class="btn grey">
<i data-feather="x"></i>
Cancel
</a>
<button class="btn" type="submit">
<i data-feather="plus"></i>
Create
</button>
</div>
</form>
</div>
Expand Down
10 changes: 8 additions & 2 deletions src/resources/views/type/delete.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@
you are doing before proceeding.</p>

<div class="mt-4 flex justify-end gap-2">
<a href="{{ route('admin.type.index', request()->type) }}" class="btn grey">Cancel</a>
<button class="btn red" onclick="document.getElementById('del').submit()">Delete</button>
<a href="{{ route('admin.type.index', request()->type) }}" class="btn grey">
<i data-feather="x"></i>
Cancel
</a>
<button class="btn red" onclick="document.getElementById('del').submit()">
<i data-feather="trash-2"></i>
Delete
</button>
</div>

<form action="{{ route('admin.type.destroy', [request()->type, 'ids' => $ids]) }}" method="post" id="del">
Expand Down
10 changes: 8 additions & 2 deletions src/resources/views/type/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ class="input"
</div>

<div class="flex gap-2 mt-4 justify-end">
<a href="{{ route('admin.type.index', request()->type) }}" class="btn grey">Cancel</a>
<button class="btn" type="submit">Edit</button>
<a href="{{ route('admin.type.index', request()->type) }}" class="btn grey">
<i data-feather="x"></i>
Cancel
</a>
<button class="btn" type="submit">
<i data-feather="edit"></i>
Edit
</button>
</div>
</form>
</div>
Expand Down
Loading

0 comments on commit 60e103b

Please sign in to comment.