Skip to content

Commit

Permalink
[Add] Delete Library Settings
Browse files Browse the repository at this point in the history
- Added DeleteLibraryForm Livewire component
- Updated settings with a new section for deleting libraries
  • Loading branch information
kiritokatklian committed Jul 6, 2024
1 parent 90583f9 commit d4194f6
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 0 deletions.
42 changes: 42 additions & 0 deletions app/Actions/Web/DeleteLibrary.php
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);
}
}
18 changes: 18 additions & 0 deletions app/Contracts/DeletesLibraries.php
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;
}
107 changes: 107 additions & 0 deletions app/Livewire/Profile/DeleteLibraryForm.php
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 resources/views/livewire/profile/delete-library-form.blade.php
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>
6 changes: 6 additions & 0 deletions resources/views/profile/settings.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@

<x-hr class="hidden sm:block" />

<div class="sm:mt-0">
<livewire:profile.delete-library-form />
</div>

<x-hr class="hidden sm:block" />

<div class="sm:mt-0">
<livewire:profile.delete-user-form />
</div>
Expand Down

0 comments on commit d4194f6

Please # to comment.