forked from grokability/snipe-it
-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor modal #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Refactor modal #16
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
fcb5162
Added first version of the new modal
cz-lucas c9c3d45
Added modalactiontype
cz-lucas db79291
Now a predefined filter is loaded and the name, visibilty and groups …
cz-lucas 04267d1
Now the select2 dropdown in the modal works
cz-lucas 86b94b9
Fixed error in dropdown
cz-lucas 4439dd8
Continued with the modal
cz-lucas 5f02114
Some updates for the dropdown in the modal
cz-lucas af71a91
Now an array with all selected groups is send to the backend 🥳
cz-lucas 2ea463f
Added methods for create, update and delete
cz-lucas 9d18d3f
Deletion is now possible
cz-lucas d8ccc46
Creation works also now
cz-lucas 08acddb
Now the update works (execpt for groups)
cz-lucas 2276834
Fixed updates for groups
cz-lucas 226d451
Added some testcases and removed old modal
cz-lucas dc36f24
Added some testcases and removed old modal
cz-lucas d9ded83
Disabling group select when the visibility is set to private
cz-lucas 18a6794
Made it controllable using the keyboard
cz-lucas 82c99cc
Merge branch 'develop' into refactor-modal
cz-lucas 9055043
Implemented comments in pullrequest imbus/snipe-it/pull#16
cz-lucas 694ad5c
Merge branch 'refactor-modal' of github.com:imbus/snipe-it into refac…
cz-lucas f6928a5
Merge branch 'develop' into refactor-modal
cz-lucas 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,245 @@ | ||
| <?php | ||
|
|
||
| namespace App\Livewire\Partials\Advancedsearch; | ||
|
|
||
| use Livewire\Component; | ||
| use Livewire\Attributes\On; | ||
| use Livewire\Attributes\Validate; | ||
|
|
||
| use App\Models\PredefinedFilter; | ||
| use App\Services\PredefinedFilterService; | ||
|
|
||
| enum FilterVisibility: string | ||
| { | ||
| case Private = "private"; | ||
| case Public = "public"; | ||
| } | ||
|
|
||
| enum AdvancedsearchModalAction: string | ||
| { | ||
| case Create = "create"; | ||
| case Edit = "edit"; | ||
| case Delete = "delete"; | ||
| } | ||
|
|
||
| class Modal extends Component | ||
| { | ||
| public $showModal = false; | ||
| public AdvancedsearchModalAction $modalActionType; | ||
|
|
||
| #[Validate("required")] | ||
| public ?string $name = ""; | ||
| public FilterVisibility $visibility = FilterVisibility::Private; | ||
| public $groupSelect = []; | ||
| public array $groupSelectOtherOptions = []; | ||
|
|
||
| protected $listeners = ["groupSelect"]; | ||
|
|
||
| public ?int $filterId; | ||
| public $filterData; | ||
|
|
||
| #[On("openPredefinedFiltersModal")] | ||
| public function openPredefinedFiltersModal( | ||
| PredefinedFilterService $predefinedFilterService, | ||
| string $action, | ||
| ?array $predefinedFilterData = null, | ||
| ?int $predefinedFilterId = null | ||
| ) { | ||
| $this->modalActionType = AdvancedsearchModalAction::from($action); | ||
| $this->showModal = true; | ||
| $this->groupSelect = []; | ||
| $this->groupSelectOtherOptions = []; | ||
| $this->filterData = $predefinedFilterData; | ||
| $this->filterId = $predefinedFilterId; | ||
|
|
||
| $user = auth()->user(); | ||
| $this->groupSelectOtherOptions = $user | ||
| ->groups() | ||
| ->pluck("id") | ||
| ->toArray(); | ||
|
|
||
| if ( | ||
| $this->modalActionType === AdvancedsearchModalAction::Edit && | ||
| $predefinedFilterId !== null | ||
| ) { | ||
| $predefinedFilter = $predefinedFilterService->getFilterById( | ||
| $predefinedFilterId | ||
| ); | ||
| $this->name = $predefinedFilter["name"]; | ||
|
|
||
| if ($predefinedFilter["is_public"] === 1) { | ||
| $this->visibility = FilterVisibility::Public; | ||
| } else { | ||
| $this->visibility = FilterVisibility::Private; | ||
| } | ||
|
|
||
| foreach ($predefinedFilter["permissions"] as $permission) { | ||
| array_push( | ||
| $this->groupSelect, | ||
| $permission->permission_group_id | ||
| ); | ||
| } | ||
|
|
||
| $this->groupSelectOtherOptions = array_diff( | ||
| $this->groupSelectOtherOptions, | ||
| $this->groupSelect | ||
| ); | ||
| } | ||
|
|
||
| $this->dispatch("openPredefinedFiltersModalEvent"); | ||
| } | ||
|
|
||
| #[On("closePredefinedFiltersModal")] | ||
| public function closePredefinedFiltersModal() | ||
| { | ||
| $this->showModal = false; | ||
| $this->groupSelect = []; | ||
| $this->groupSelectOtherOptions = []; | ||
| $this->filterData = null; | ||
| $this->filterId = null; | ||
| $this->name = ""; | ||
| $this->dispatch("closePredefinedFiltersModalEvent"); | ||
| } | ||
|
|
||
| #[On("savePredefinedFiltersModal")] | ||
| public function savePredefinedFiltersModal( | ||
| PredefinedFilterService $predefinedFilterService | ||
| ) { | ||
| $this->validate(); | ||
|
|
||
| $validated = [ | ||
| "name" => $this->name, | ||
| "filter_data" => $this->filterData, | ||
| "is_public" => | ||
| $this->visibility === FilterVisibility::Public ? 1 : 0, | ||
| "permissions" => self::formatPermissions($this->groupSelect), | ||
| ]; | ||
|
|
||
| $createFilterResponse = $predefinedFilterService->createFilter($validated); | ||
|
|
||
| if ($createFilterResponse === true) { | ||
| $this->dispatch('showNotification', [ | ||
| 'type' => 'success', | ||
| 'title' => trans('general.notification_success'), | ||
| 'message' => trans('general.predefined_filter_saved_successfully'), | ||
| 'tag' => 'predefinedFilter', | ||
| ]); | ||
| } else { | ||
| $this->dispatch('showNotification', [ | ||
| 'type' => 'error', | ||
| 'title' => trans('general.notification_error'), | ||
| 'message' => trans('general.notification_error'), | ||
| 'tag' => 'predefinedFilter', | ||
| ]); | ||
| } | ||
|
|
||
| $this->dispatch("savePredefinedFiltersModalEvent"); | ||
| $this->dispatch("closePredefinedFiltersModal"); | ||
| } | ||
|
|
||
| #[On("updatePredefinedFiltersModal")] | ||
| public function updatePredefinedFiltersModal( | ||
| PredefinedFilterService $predefinedFilterService | ||
| ) { | ||
| $this->validate([ | ||
| 'name' => 'required|string', | ||
| 'filterData' => 'array', | ||
| 'groupSelect' => 'array', | ||
| 'groupSelect.*' => 'required|integer|exists:permission_groups,id', | ||
| ]); | ||
|
|
||
| $predefinedFilter = PredefinedFilter::find($this->filterId); | ||
|
|
||
| $validated = [ | ||
| 'name' => $this->name ?? $predefinedFilter->name, | ||
| 'filter_data' => $this->filterData ?? $predefinedFilter->filter_data, | ||
| 'is_public' => isset($this->visibility) | ||
| ? ($this->visibility === FilterVisibility::Public ? 1 : 0) | ||
| : $predefinedFilter->is_public, | ||
| 'permissions' => self::formatPermissions($this->getGroupSelectArrayAsArray()), | ||
| ]; | ||
|
|
||
| $updateFilterResponse = $predefinedFilterService->updateFilter($predefinedFilter, $validated); | ||
|
|
||
| if ($updateFilterResponse === true) { | ||
| $this->dispatch('showNotification', [ | ||
| 'type' => 'success', | ||
| 'title' => trans('general.notification_success'), | ||
| 'message' => trans('general.predefined_filter_saved_successfully'), | ||
| 'tag' => 'predefinedFilter', | ||
| ]); | ||
| } else { | ||
| $this->dispatch('showNotification', [ | ||
| 'type' => 'error', | ||
| 'title' => trans('general.notification_error'), | ||
| 'message' => trans('general.notification_error'), | ||
| 'tag' => 'predefinedFilter', | ||
| ]); | ||
| } | ||
|
|
||
| $this->dispatch("updatePredefinedFiltersModalEvent"); | ||
| $this->dispatch("closePredefinedFiltersModal"); | ||
| } | ||
|
|
||
| #[On("deletePredefinedFiltersModal")] | ||
| public function deletePredefinedFiltersModal( | ||
| PredefinedFilterService $predefinedFilterService | ||
| ) { | ||
|
|
||
|
|
||
| $predefinedFilter = $predefinedFilterService->getFilterById($this->filterId); | ||
| $deleteFilterResponse = $predefinedFilterService->deleteFilter($predefinedFilter); | ||
|
|
||
| if ($deleteFilterResponse === true) { | ||
| $this->dispatch('showNotification', [ | ||
| 'type' => 'success', | ||
| 'title' => trans('general.notification_success'), | ||
| 'message' => trans('general.predefined_filter_saved_successfully'), | ||
| 'tag' => 'predefinedFilter', | ||
| ]); | ||
| } else { | ||
| $this->dispatch('showNotification', [ | ||
| 'type' => 'error', | ||
| 'title' => trans('general.notification_error'), | ||
| 'message' => trans('general.notification_error'), | ||
| 'tag' => 'predefinedFilter', | ||
| ]); | ||
| } | ||
|
|
||
| $this->dispatch("deletePredefinedFiltersModalEvent"); | ||
| $this->dispatch("closePredefinedFiltersModal"); | ||
| } | ||
|
|
||
| public function updateGroupSelect($values) | ||
| { | ||
| $this->groupSelect = is_array($values) | ||
| ? $values | ||
| : ($values | ||
| ? [$values] | ||
| : []); | ||
| } | ||
|
|
||
| public function render() | ||
| { | ||
| return view("livewire.partials.advancedsearch.modal"); | ||
| } | ||
|
|
||
| private function getGroupSelectArrayAsArray(): array | ||
| { | ||
| if (is_array($this->groupSelect) === true) { | ||
| return $this->groupSelect; | ||
| } | ||
| return [$this->groupSelect]; | ||
| } | ||
|
|
||
| private static function formatPermissions(array $permissions): array | ||
| { | ||
| $result = []; | ||
|
|
||
| foreach ($permissions as $value) { | ||
| $result[] = ["permission_group_id" => $value]; | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Is that really required? When you have a private filter, I don't think you need it.