This repository was archived by the owner on Nov 26, 2024. It is now read-only.
forked from BadChoice/thrust
-
Notifications
You must be signed in to change notification settings - Fork 4
Added single resource actions #144
Open
BadChoice
wants to merge
5
commits into
master
Choose a base branch
from
feature/singleResourceActions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,32 @@ | ||
| <?php | ||
|
|
||
| namespace BadChoice\Thrust\Actions; | ||
|
|
||
| use BadChoice\Thrust\Resource; | ||
| use BadChoice\Thrust\ResourceGate; | ||
|
|
||
| abstract class SingleResourceAction | ||
| { | ||
| public $title; | ||
| public $icon; | ||
| public Resource $resource; | ||
| public $shouldReload = false; | ||
| public $responseAsPopup = true; | ||
|
|
||
| public static function make($title, ?string $icon = null) | ||
| { | ||
| $action = new static; | ||
| $action->title = $title; | ||
| $action->icon = $icon; | ||
| return $action; | ||
| } | ||
|
|
||
| abstract public function handle(); | ||
|
|
||
| public function getClassForJs() | ||
| { | ||
| return str_replace('\\', '\\\\', get_class($this)); | ||
| } | ||
|
|
||
|
|
||
| } | ||
This file contains hidden or 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 hidden or 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,64 @@ | ||
| <?php | ||
|
|
||
| namespace BadChoice\Thrust\Controllers; | ||
|
|
||
| use BadChoice\Thrust\Facades\Thrust; | ||
| use Illuminate\Routing\Controller; | ||
| use Illuminate\View\ComponentAttributeBag; | ||
|
|
||
| class ThrustSingleResourceActionsController extends Controller | ||
| { | ||
|
|
||
| public function perform($resourceName) | ||
| { | ||
| $action = $this->findActionForResource($resourceName, request('action')); | ||
| try { | ||
| return $this->handleResponse($action, $action->handle()); | ||
| } catch (\Exception $e) { | ||
| return $this->handleException($e); | ||
| } | ||
| } | ||
|
|
||
| private function handleException(\Exception $e) { | ||
| return request()->ajax() | ||
| ? response()->json([ | ||
| 'ok' => false, | ||
| 'message' => $e->getMessage(), | ||
| 'shouldReload' => false, | ||
| 'responseAsPopup' => false | ||
| ]) | ||
| : back()->withErrors(['msg' => $e->getMessage()]); | ||
| } | ||
|
|
||
| private function handleResponse($action, $response){ | ||
| if (request()->ajax()) { | ||
| return response()->json([ | ||
| 'ok' => true, | ||
| 'message' => $response ?? 'done', | ||
| 'shouldReload' => $action->shouldReload, | ||
| 'responseAsPopup' => $action->responseAsPopup | ||
| ]); | ||
| } | ||
|
|
||
| return back()->withMessage($response); | ||
| } | ||
|
|
||
| private function findActionForResource($resourceName, $actionClass) | ||
| { | ||
| $resource = Thrust::make($resourceName); | ||
| $action = collect($resource->singleResourceActions())->first(function ($action) use ($actionClass) { | ||
| return get_class($action) === $actionClass; | ||
| }); | ||
|
|
||
| $action->resource = request('search') && $resource::$searchResource | ||
| ? Thrust::make($resource::$searchResource) | ||
| : $resource; | ||
|
|
||
| return $action; | ||
| } | ||
|
|
||
| private function searchingInResource(): bool | ||
| { | ||
| return (bool) request('search', false); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
37 changes: 37 additions & 0 deletions
37
src/resources/views/actions/singleResourceActions.blade.php
This file contains hidden or 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,37 @@ | ||
| @if(count($resource->singleResourceActions())) | ||
BadChoice marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <x-ui::dropdown> | ||
| <x-slot:trigger> | ||
| <x-ui::secondary-button>@icon(ellipsis-v)</x-ui::secondary-button> | ||
| </x-slot:trigger> | ||
| <div class="flex flex-col gap-2"> | ||
| @foreach($resource->singleResourceActions() as $action) | ||
| <x-ui::tertiary-button :icon="$action->icon" action="function() { doSingleResourceAction('{{ $action->getClassForJs() }}') }"> | ||
| {{ $action->title }} | ||
| </x-ui::tertiary-button> | ||
| @endforeach | ||
| </div> | ||
| </x-ui::dropdown> | ||
|
|
||
| <script> | ||
| async function doSingleResourceAction(actionClass){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Si ho fem amb un js aprofitem per compilar-ho no? Sino tant per tant deixa-ho amb alpinejs I no vam dir que deixavem de fer servir jquery? Si és codi nou entenc que ja no ho hauriem d'aplicar |
||
| await $.post("{{ route('thrust.actions.singleResource.perform', [$resourceName]) }}", { | ||
| "_token": "{{ csrf_token() }}", | ||
| "action" : actionClass, | ||
| }).done(function(data){ | ||
| if (data["responseAsPopup"]){ | ||
| $('#popup').popup('show') | ||
| $("#popupContent").html(data["message"]) | ||
| } else { | ||
| showMessage(data["message"]) | ||
| } | ||
| if (data["shouldReload"]) { | ||
| location.reload() | ||
| } | ||
| }).fail(function(){ | ||
| showMessage("Something went wrong") | ||
| }) | ||
| } | ||
| </script> | ||
|
|
||
| @endif | ||
|
|
||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to have: Per codi nou potser podem posar types a les funcions (param i return) i variables? A codi antic costa més perque tenim moltes classes que els sobre-escriuen pero aqui podriem fer-ho