92 lines
2.3 KiB
PHP
92 lines
2.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Livewire\SocialAccounts;
|
||
|
|
|
||
|
|
use App\Models\SocialAccount;
|
||
|
|
use Livewire\Attributes\Layout;
|
||
|
|
use Livewire\Component;
|
||
|
|
|
||
|
|
#[Layout('components.layouts.app')]
|
||
|
|
class Index extends Component
|
||
|
|
{
|
||
|
|
public bool $showForm = false;
|
||
|
|
|
||
|
|
public ?int $editingId = null;
|
||
|
|
|
||
|
|
public ?int $revealedId = null;
|
||
|
|
|
||
|
|
public string $network = '';
|
||
|
|
|
||
|
|
public string $username = '';
|
||
|
|
|
||
|
|
public string $email = '';
|
||
|
|
|
||
|
|
public string $password = '';
|
||
|
|
|
||
|
|
public ?string $type = null;
|
||
|
|
|
||
|
|
public function render()
|
||
|
|
{
|
||
|
|
return view('livewire.social-accounts.index', [
|
||
|
|
'accounts' => SocialAccount::orderBy('username')->get(),
|
||
|
|
'networks' => SocialAccount::NETWORKS,
|
||
|
|
'types' => SocialAccount::TYPES,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function create(): void
|
||
|
|
{
|
||
|
|
$this->resetForm();
|
||
|
|
$this->showForm = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function edit(int $id): void
|
||
|
|
{
|
||
|
|
$account = SocialAccount::findOrFail($id);
|
||
|
|
$this->editingId = $account->id;
|
||
|
|
$this->network = $account->network;
|
||
|
|
$this->username = $account->username;
|
||
|
|
$this->email = $account->email;
|
||
|
|
$this->password = $account->password;
|
||
|
|
$this->type = $account->type;
|
||
|
|
$this->showForm = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function save(): void
|
||
|
|
{
|
||
|
|
$validated = $this->validate([
|
||
|
|
'network' => ['required', 'string', 'in:' . implode(',', SocialAccount::NETWORKS)],
|
||
|
|
'username' => ['required', 'string', 'max:255'],
|
||
|
|
'email' => ['required', 'string', 'max:255'],
|
||
|
|
'password' => ['required', 'string', 'max:255'],
|
||
|
|
'type' => ['nullable', 'string', 'in:' . implode(',', SocialAccount::TYPES)],
|
||
|
|
]);
|
||
|
|
|
||
|
|
SocialAccount::updateOrCreate(['id' => $this->editingId], $validated);
|
||
|
|
|
||
|
|
$this->resetForm();
|
||
|
|
$this->showForm = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function delete(int $id): void
|
||
|
|
{
|
||
|
|
SocialAccount::findOrFail($id)->delete();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function toggleReveal(int $id): void
|
||
|
|
{
|
||
|
|
$this->revealedId = $this->revealedId === $id ? null : $id;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function resetForm(): void
|
||
|
|
{
|
||
|
|
$this->editingId = null;
|
||
|
|
$this->network = '';
|
||
|
|
$this->username = '';
|
||
|
|
$this->email = '';
|
||
|
|
$this->password = '';
|
||
|
|
$this->type = null;
|
||
|
|
$this->resetValidation();
|
||
|
|
}
|
||
|
|
}
|