-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added DeleteLibraryForm Livewire component - Updated settings with a new section for deleting libraries
- Loading branch information
1 parent
90583f9
commit d4194f6
Showing
5 changed files
with
243 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\Actions\Web; | ||
|
||
use App\Contracts\DeletesLibraries; | ||
use App\Enums\UserLibraryKind; | ||
use App\Models\Anime; | ||
use App\Models\Game; | ||
use App\Models\Manga; | ||
use App\Models\User; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
class DeleteLibrary implements DeletesLibraries | ||
{ | ||
/** | ||
* Delete the given user. | ||
* | ||
* @param User $user | ||
* @param array $input | ||
* | ||
* @return void | ||
*/ | ||
public function delete(User $user, array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'library' => ['required', 'integer', 'in:' . implode(',', UserLibraryKind::getValues())], | ||
])->validateWithBag('deleteUserLibrary'); | ||
|
||
// Get the library type | ||
$libraryKind = UserLibraryKind::fromValue((int) $input['library']); | ||
$type = match ($libraryKind->value) { | ||
UserLibraryKind::Anime => Anime::class, | ||
UserLibraryKind::Manga => Manga::class, | ||
UserLibraryKind::Game => Game::class | ||
}; | ||
|
||
$user->clearLibrary($type); | ||
$user->clearFavorites($type); | ||
$user->clearReminders($type); | ||
$user->clearRatings($type); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace App\Contracts; | ||
|
||
use App\Models\User; | ||
|
||
interface DeletesLibraries | ||
{ | ||
/** | ||
* Delete the given user's library. | ||
* | ||
* @param User $user | ||
* @param array $input | ||
* | ||
* @return void | ||
*/ | ||
public function delete(User $user, array $input): void; | ||
} |
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,107 @@ | ||
<?php | ||
|
||
namespace App\Livewire\Profile; | ||
|
||
use App\Contracts\DeletesLibraries; | ||
use App\Enums\UserLibraryKind; | ||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Contracts\View\Factory; | ||
use Illuminate\Contracts\View\View; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\ValidationException; | ||
use Livewire\Component; | ||
|
||
class DeleteLibraryForm extends Component | ||
{ | ||
/** | ||
* The component's state. | ||
* | ||
* @var array | ||
*/ | ||
public array $state = []; | ||
|
||
/** | ||
* Indicates if library deletion is being confirmed. | ||
* | ||
* @var bool | ||
*/ | ||
public bool $confirmingLibraryDeletion = false; | ||
|
||
/** | ||
* The user's current password. | ||
* | ||
* @var string | ||
*/ | ||
public string $password = ''; | ||
|
||
/** | ||
* Confirm that the user would like to delete their library. | ||
* | ||
* @return void | ||
* @throws ValidationException | ||
*/ | ||
public function confirmLibraryDeletion(): void | ||
{ | ||
Validator::make($this->state, [ | ||
'library' => ['required', 'integer', 'in:' . implode(',', UserLibraryKind::getValues())], | ||
])->validateWithBag('deleteUserLibrary'); | ||
|
||
$this->resetErrorBag(); | ||
|
||
$this->password = ''; | ||
|
||
$this->dispatch('confirming-delete-library'); | ||
|
||
$this->confirmingLibraryDeletion = true; | ||
} | ||
|
||
/** | ||
* Delete the current library. | ||
* | ||
* @param DeletesLibraries $deleter | ||
* | ||
* @throws ValidationException | ||
*/ | ||
public function deleteLibrary(DeletesLibraries $deleter): void | ||
{ | ||
$this->resetErrorBag(); | ||
|
||
if (!Hash::check($this->password, auth()->user()->password)) { | ||
throw ValidationException::withMessages([ | ||
'password' => [__('This password does not match our records.')], | ||
]); | ||
} | ||
|
||
$this->password = ''; | ||
|
||
$this->confirmingLibraryDeletion = false; | ||
|
||
$deleter->delete(auth()->user(), $this->state); | ||
|
||
$this->dispatch('deleted'); | ||
} | ||
|
||
/** | ||
* Returns an instance of UserLibraryKind. | ||
* | ||
* @return null|UserLibraryKind | ||
*/ | ||
public function getLibraryProperty(): ?UserLibraryKind | ||
{ | ||
if (!isset($this->state['library'])) { | ||
return null; | ||
} | ||
return UserLibraryKind::fromValue((int) $this->state['library']); | ||
} | ||
|
||
/** | ||
* Render the component. | ||
* | ||
* @return Application|Factory|View | ||
*/ | ||
public function render(): Application|Factory|View | ||
{ | ||
return view('livewire.profile.delete-library-form'); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
resources/views/livewire/profile/delete-library-form.blade.php
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,70 @@ | ||
<x-action-section> | ||
<x-slot:title> | ||
{{ __('Delete Library') }} | ||
</x-slot:title> | ||
|
||
<x-slot:description> | ||
{{ __('Permanently delete your library.') }} | ||
</x-slot:description> | ||
|
||
<x-slot:content> | ||
<div class="max-w-xl text-sm text-gray-600"> | ||
{{ __('Once your library is deleted, all of its resources and data will be permanently deleted. This includes ratings, favorites, reminders, watched episodes, and You will be asked for your password to confirm the deletion.') }} | ||
</div> | ||
|
||
<div class="mt-5"> | ||
<x-select id="library" wire:model="state.library"> | ||
<option value="-1">{{ __('Select library') }}</option> | ||
@foreach (App\Enums\UserLibraryKind::asSelectArray() as $value => $libraryKind) | ||
<option value="{{ $value }}">{{ $libraryKind }}</option> | ||
@endforeach | ||
</x-select> | ||
|
||
<x-input-error for="library" class="mt-2"/> | ||
</div> | ||
|
||
<div class="flex gap-2 items-center mt-5"> | ||
<x-danger-button wire:click="confirmLibraryDeletion" wire:loading.attr="disabled"> | ||
{{ __('Delete Library') }} | ||
</x-danger-button> | ||
|
||
<x-action-message class="mr-3" on="deleted"> | ||
@if ($this->library) | ||
{{ __(':x library deleted.', ['x' => $this->library->key]) }} | ||
@else | ||
{{ __('Library deleted.') }} | ||
@endif | ||
</x-action-message> | ||
</div> | ||
|
||
<!-- Delete Library Confirmation Modal --> | ||
<x-dialog-modal model="confirmingLibraryDeletion"> | ||
<x-slot:title> | ||
{{ __('Delete Library') }} | ||
</x-slot:title> | ||
|
||
<x-slot:content> | ||
{{ __('Are you sure you want to delete your library? Once your library is deleted, all of its resources and data will be permanently deleted. Please enter your password to confirm you would like to permanently delete your library.') }} | ||
|
||
<div class="mt-4" x-data="{}" x-on:confirming-delete-library.window="setTimeout(() => $refs.password.focus(), 250)"> | ||
<x-input type="password" class="mt-1 block w-3/4" placeholder="{{ __('Password') }}" | ||
x-ref="password" | ||
wire:model="password" | ||
wire:keydown.enter="deleteLibrary" /> | ||
|
||
<x-input-error for="password" class="mt-2" /> | ||
</div> | ||
</x-slot:content> | ||
|
||
<x-slot:footer> | ||
<x-outlined-button wire:click="$toggle('confirmingLibraryDeletion')" wire:loading.attr="disabled"> | ||
{{ __('Nevermind') }} | ||
</x-outlined-button> | ||
|
||
<x-danger-button class="ml-2" wire:click="deleteLibrary" wire:loading.attr="disabled"> | ||
{{ __('Delete Library') }} | ||
</x-danger-button> | ||
</x-slot:footer> | ||
</x-dialog-modal> | ||
</x-slot:content> | ||
</x-action-section> |
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