-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: merge staging into master (#557)
- Loading branch information
Showing
54 changed files
with
3,076 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Enums; | ||
|
||
enum SortDirection: string | ||
{ | ||
case ASC = 'asc'; | ||
|
||
case DESC = 'desc'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Livewire\Concerns; | ||
|
||
use App\Enums\SortDirection; | ||
|
||
trait HasTableSorting | ||
{ | ||
public string $sortKey; | ||
|
||
public SortDirection $sortDirection; | ||
|
||
public function bootHasTableSorting(): void | ||
{ | ||
$this->sortKey = static::defaultSortKey(); | ||
|
||
if (request()->has('sort-direction')) { | ||
$sortDirection = request()->get('sort-direction'); | ||
if ($sortDirection === SortDirection::DESC->value) { | ||
$this->sortDirection = SortDirection::DESC; | ||
} else { | ||
$this->sortDirection = SortDirection::ASC; | ||
} | ||
} else { | ||
$this->sortDirection = static::defaultSortDirection(); | ||
} | ||
} | ||
|
||
public function queryStringHasTableSorting(): array | ||
{ | ||
return [ | ||
'sortKey' => ['as' => 'sort', 'except' => static::defaultSortKey()], | ||
'sortDirectionQuery' => ['as' => 'sort-direction', 'except' => static::defaultSortDirection()->value], | ||
]; | ||
} | ||
|
||
public function sortBy(string $key): void | ||
{ | ||
if ($this->sortKey === $key) { | ||
if ($this->sortDirection === SortDirection::ASC) { | ||
$this->sortDirection = SortDirection::DESC; | ||
} else { | ||
$this->sortDirection = SortDirection::ASC; | ||
} | ||
} else { | ||
$this->sortKey = $key; | ||
$this->sortDirection = SortDirection::ASC; | ||
} | ||
|
||
$this->gotoPage(1); | ||
} | ||
|
||
public static function defaultSortKey(): string | ||
{ | ||
return constant(static::class.'::INITIAL_SORT_KEY'); | ||
} | ||
|
||
public static function defaultSortDirection(): SortDirection | ||
{ | ||
return constant(static::class.'::INITIAL_SORT_DIRECTION'); | ||
} | ||
|
||
public function getSortDirectionQueryProperty(): string | ||
{ | ||
return $this->sortDirection->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.