From b387c315b604caf2f5f109077d88275998a3b7c3 Mon Sep 17 00:00:00 2001 From: sundayenglish Date: Wed, 25 Jun 2025 14:42:52 +0700 Subject: [PATCH] Done Profile --- app/Components/Profile/Manager.php | 53 +++++++- .../components/profile/manager.blade.php | 114 ++++++++++++++---- .../views/components/skeleton-form.blade.php | 2 +- .../views/layouts/partials/sidebar.blade.php | 31 +---- 4 files changed, 143 insertions(+), 57 deletions(-) diff --git a/app/Components/Profile/Manager.php b/app/Components/Profile/Manager.php index 1263752..b0920a5 100644 --- a/app/Components/Profile/Manager.php +++ b/app/Components/Profile/Manager.php @@ -4,13 +4,26 @@ namespace App\Components\Profile; use Livewire\Component; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Hash; use Spatie\Permission\Models\Role; class Manager extends Component { - public object $user; - public array $roles = []; - public array $allRoles = []; + // Profile & roles + public object $user; + public array $roles = []; + public array $allRoles = []; + + // Đổi mật khẩu + public string $mode = 'view'; // 'view' | 'password' + public string $currentPassword = ''; + public string $password = ''; + public string $password_confirmation = ''; + + protected array $rules = [ + 'currentPassword' => ['required', 'string'], + 'password' => ['required', 'string', 'min:8', 'confirmed'], + ]; public function mount(): void { @@ -19,6 +32,40 @@ class Manager extends Component $this->roles = $this->user->roles->pluck('name')->toArray(); } + public function showPasswordForm(): void + { + $this->mode = 'password'; + $this->resetErrorBag(); + $this->resetValidation(); + } + + public function showView(): void + { + $this->mode = 'view'; + $this->reset(['currentPassword', 'password', 'password_confirmation']); + $this->resetErrorBag(); + } + + public function updatePassword(): void + { + // 1. Validate + $this->validate(); + + // 2. Kiểm tra mật khẩu hiện tại + if (! Hash::check($this->currentPassword, $this->user->password)) { + $this->addError('currentPassword', 'Mật khẩu hiện tại không đúng.'); + return; + } + + // 3. Cập nhật mật khẩu mới + $this->user->password = Hash::make($this->password); + $this->user->save(); + + // 4. Thông báo & quay về view + session()->flash('message', 'Đổi mật khẩu thành công.'); + $this->showView(); + } + public function render() { return view('components.profile.manager', [ diff --git a/resources/views/components/profile/manager.blade.php b/resources/views/components/profile/manager.blade.php index b70945b..7ff0d32 100644 --- a/resources/views/components/profile/manager.blade.php +++ b/resources/views/components/profile/manager.blade.php @@ -1,13 +1,22 @@ -
+ {{-- Header --}}
{{ $title }}
- +
+ @if ($mode === 'view') + + + @else + + @endif +
{{-- Body --}} @@ -16,26 +25,85 @@
{{ session('message') }}
@endif -
-
Họ và tên
-
{{ $user->fullname }}
- -
Email
-
{{ $user->email }}
- -
Vai trò
-
- @if ($roles) - {{ implode(', ', $roles) }} - @else - Chưa có vai trò - @endif -
- -
Ngày tạo tài khoản
-
{{ $user->created_at?->format('d/m/Y H:i') ?? '—' }}
-
+ @if ($mode === 'view') + {{-- Skeleton khi loading dữ liệu profile --}} +
+ {{-- 2 columns (label + value), 4 rows --}} + +
+ + {{-- Actual profile --}} +
+
+
Họ và tên
+
{{ $user->fullname }}
+ +
Email
+
{{ $user->email }}
+ +
Vai trò
+
+ @if ($roles) + {{ implode(', ', $roles) }} + @else + Chưa có vai trò + @endif +
+ +
Ngày tạo tài khoản
+
+ {{ $user->created_at?->format('d/m/Y H:i') ?? '—' }} +
+
+
+ @else + {{-- Skeleton khi loading form đổi mật khẩu --}} +
+ {{-- 3 fields + 2 buttons --}} + +
+ + {{-- Actual đổi mật khẩu --}} +
+
+
+ + + @error('currentPassword') +
{{ $message }}
+ @enderror +
+ +
+ + + @error('password') +
{{ $message }}
+ @enderror +
+ +
+ + +
+ +
+ + +
+
+
+ @endif
+
diff --git a/resources/views/components/skeleton-form.blade.php b/resources/views/components/skeleton-form.blade.php index 3c973d1..7521b2f 100644 --- a/resources/views/components/skeleton-form.blade.php +++ b/resources/views/components/skeleton-form.blade.php @@ -1,7 +1,7 @@ @props(['fields', 'buttonCount'])
-
+
{{-- Title --}}
diff --git a/resources/views/layouts/partials/sidebar.blade.php b/resources/views/layouts/partials/sidebar.blade.php index dcdc421..339b47a 100644 --- a/resources/views/layouts/partials/sidebar.blade.php +++ b/resources/views/layouts/partials/sidebar.blade.php @@ -141,7 +141,7 @@ - - -