Skip to content

Commit e27d276

Browse files
committed
Refactor user profile handling and validation messages for consistency
1 parent 6f15c29 commit e27d276

7 files changed

Lines changed: 16 additions & 14 deletions

File tree

app/Concerns/ProfileValidationRules.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function profileRules(?string $userId = null): array
2222
/**
2323
* @return array<string, array<int, \Illuminate\Contracts\Validation\Rule|array<mixed>|string>>
2424
*/
25-
protected function profileRulesErrorMessage(): array
25+
protected function profileRulesErrorMessages(): array
2626
{
2727
return [
2828
'name.regex' => 'The name field must only contain letters, numbers, underscores, hyphens, and spaces.',

app/Livewire/Home.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace App\Livewire;
44

5+
use App\Models\User;
56
use Illuminate\Support\Facades\Auth;
67
use Livewire\Component;
78

89
class Home extends Component
910
{
10-
public $user;
11+
public ?User $user;
1112

1213
public function mount()
1314
{

app/Livewire/Profile.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Concerns\PasswordValidationRules;
66
use App\Concerns\ProfileValidationRules;
77
use App\Livewire\Actions\Logout;
8+
use App\Models\User;
89
use Illuminate\Contracts\Auth\MustVerifyEmail;
910
use Illuminate\Support\Facades\Auth;
1011
use Illuminate\Support\Facades\Session;
@@ -17,7 +18,7 @@ class Profile extends Component
1718
{
1819
use ProfileValidationRules, PasswordValidationRules;
1920

20-
public mixed $user;
21+
public ?User $user;
2122

2223
public string $name = '';
2324

@@ -105,7 +106,7 @@ public function updateProfileInformation(): void
105106

106107
$this->username = preg_replace('/[\s+]/', '_', strtolower($this->username));
107108

108-
$validated = $this->validate($this->profileRules($this->user->id), $this->profileRulesErrorMessage());
109+
$validated = $this->validate($this->profileRules($this->user->id), $this->profileRulesErrorMessages());
109110

110111
$this->user->fill($validated);
111112

app/Livewire/Users/Profile.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Profile extends Component
2020
{
2121
use ProfileValidationRules, PasswordValidationRules;
2222

23-
public $user;
23+
public ?User $user;
2424

2525
public string $name = '';
2626

@@ -100,10 +100,10 @@ public function hasUnverifiedEmail(): bool
100100
public function updateProfileInformation(): void
101101
{
102102
$this->name = Str::trim($this->name);
103-
103+
104104
$this->username = preg_replace('/[\s+]/', '_', strtolower($this->username));
105105

106-
$validated = $this->validate($this->profileRules($this->user->id), $this->profileRulesErrorMessage());
106+
$validated = $this->validate($this->profileRules($this->user->id), $this->profileRulesErrorMessages());
107107

108108
$this->user->fill($validated);
109109

@@ -130,10 +130,10 @@ public function updateProfileInformation(): void
130130

131131
public function unlinkSocialAccount(string $provider): void
132132
{
133-
$socialAccontsById = Social::getUserBySocialAccoutsEmail($provider, $this->user->email);
133+
$socialAccontByEmail = Social::getUserBySocialAccountEmail($provider, $this->user->email);
134134

135-
if ($socialAccontsById->first()) {
136-
$socialAccontsById->delete();
135+
if ($socialAccontByEmail->first()) {
136+
$socialAccontByEmail->delete();
137137

138138
if ($provider == 'google') {
139139
$this->socials_google = null;
@@ -170,7 +170,7 @@ public function deleteAccount(): void
170170

171171
$this->user->delete();
172172

173-
Session::flash('status', 'User deleted successfully');
173+
Session::flash('status', 'User deleted successfully!');
174174

175175
$this->redirectRoute('users.home', navigate: true);
176176
}

resources/views/layouts/app.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class="hover:underline"
173173
</div>
174174

175175
<script lang="js">
176-
function formatUsername(event) {
176+
const usernameInput = (event) => {
177177
event.target.value = event.target.value.replaceAll(/[^a-zA-Z0-9_]/g, "_").toLowerCase()
178178
}
179179
</script>

resources/views/livewire/profile.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class="cursor-pointer hover:underline"
5757
label="Username"
5858
type="text"
5959
wire:model="username"
60-
x-on:input="formatUsername"
60+
x-on:input="usernameInput"
6161
/>
6262

6363
<x-input-text

resources/views/livewire/users/profile.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class="size-24 rounded-full"
2929
label="Username"
3030
type="text"
3131
wire:model="username"
32-
x-on:input="formatUsername"
32+
x-on:input="usernameInput"
3333
/>
3434

3535
<x-input-text

0 commit comments

Comments
 (0)