Skip to content

Commit 4c2243c

Browse files
committed
Add twitter handle to user
1 parent 55db8ad commit 4c2243c

File tree

8 files changed

+75
-2
lines changed

8 files changed

+75
-2
lines changed

app/Http/Requests/UpdateProfileRequest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public function rules()
1212
'name' => 'required|max:255',
1313
'email' => 'required|email|max:255|unique:users,email,' . Auth::id(),
1414
'username' => 'required|alpha_dash|max:255|unique:users,username,' . Auth::id(),
15+
'twitter_handle' => 'max:255|nullable|unique:users,twitter_handle,' . Auth::id(),
1516
'bio' => 'max:160',
1617
];
1718
}
@@ -35,4 +36,9 @@ public function username(): string
3536
{
3637
return (string) $this->get('username');
3738
}
39+
40+
public function twitterHandle(): ?string
41+
{
42+
return $this->get('twitter_handle');
43+
}
3844
}

app/Jobs/UpdateProfile.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class UpdateProfile
2121
public function __construct(User $user, array $attributes = [])
2222
{
2323
$this->user = $user;
24-
$this->attributes = Arr::only($attributes, ['name', 'email', 'username', 'github_username', 'bio']);
24+
$this->attributes = Arr::only($attributes, ['name', 'email', 'username', 'github_username', 'bio', 'twitter_handle']);
2525
}
2626

2727
public static function fromRequest(User $user, UpdateProfileRequest $request): self
@@ -31,6 +31,7 @@ public static function fromRequest(User $user, UpdateProfileRequest $request): s
3131
'email' => $request->email(),
3232
'username' => strtolower($request->username()),
3333
'bio' => trim(strip_tags($request->bio())),
34+
'twitter_handle' => $request->twitterHandle(),
3435
]);
3536
}
3637

app/User.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ final class User extends Authenticatable
3636
protected $fillable = [
3737
'name',
3838
'email',
39+
'twitter_handle',
3940
'username',
4041
'password',
4142
'ip',
@@ -83,6 +84,11 @@ public function githubUsername(): string
8384
return $this->github_username;
8485
}
8586

87+
public function twitterHandle(): ?string
88+
{
89+
return $this->twitter_handle;
90+
}
91+
8692
public function gravatarUrl($size = 100): string
8793
{
8894
$hash = md5(strtolower(trim($this->email)));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class UpdateUsersTableAddTwitterHandleField extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::table('users', function (Blueprint $table) {
12+
$table->string('twitter_handle')->after('email')->nullable();
13+
});
14+
}
15+
}

resources/views/articles/_form.blade.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,10 @@ class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 hover:t
130130
</span>
131131
@endif
132132
</div>
133+
@if (! Auth::user()->twitterHandle())
134+
<span class="text-gray-600 text-sm mt-4 block">
135+
Articles will be shared on Twitter. <a href="{{ route('settings.profile') }}" class="text-green-darker">Add your Twitter handle</a> and we'll include that too.
136+
</span>
137+
@endif
133138
</div>
134139
</form>

resources/views/users/_user_info.blade.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,26 @@
5353
@if ($user->githubUsername())
5454
<a href="https://github.com/{{ $user->githubUsername() }}"
5555
class="text-green-darker text-3xl block flex items-center">
56-
<span class="flex items-center justify-center">
56+
<span class="flex items-center justify-center mb-1">
5757
<x-icon-github class="h-5 w-5 mr-2" />
5858
<span class="text-base">
5959
{{ '@' . $user->githubUsername() }}
6060
</span>
6161
</span>
6262
</a>
6363
@endif
64+
65+
@if ($user->twitterHandle())
66+
<a href="https://twitter.com/{{ $user->twitterHandle() }}"
67+
class="text-green-darker text-3xl block flex items-center">
68+
<span class="flex items-center justify-center mb-1">
69+
<x-icon-twitter class="h-5 w-5 mr-2" />
70+
<span class="text-base">
71+
{{ '@' . $user->twitterHandle() }}
72+
</span>
73+
</span>
74+
</a>
75+
@endif
6476
</div>
6577
</div>
6678

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
@error('username')
3434
@endFormGroup
3535

36+
@formGroup('twitter_handle')
37+
<label for="twitter_handle">Twitter handle</label>
38+
<input type="text" name="twitter_handle" id="twitter_handle" value="{{ Auth::user()->twitterHandle() }}" />
39+
@error('twitter_handle')
40+
@endFormGroup
41+
3642
@formGroup('bio')
3743
<label for="bio">Bio</label>
3844
<textarea name="bio" rows="3" maxlength="160">{{ Auth::user()->bio() }}</textarea>

tests/Feature/SettingsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ public function users_can_update_their_profile()
2727
'name' => 'Freek Murze',
2828
'email' => 'freek@example.com',
2929
'username' => 'freekmurze',
30+
'twitter_handle' => 'freek-murze',
3031
'bio' => 'My bio',
3132
])
3233
->seePageIs('/settings')
3334
->see('Freek Murze')
3435
->see('freekmurze')
36+
->see('freek-murze')
3537
->see('Settings successfully saved!')
3638
->see('My bio');
3739
}
@@ -110,6 +112,26 @@ public function users_can_set_their_password_when_they_have_none_set_yet()
110112
$this->assertPasswordWasHashedAndSaved();
111113
}
112114

115+
/** @test */
116+
public function twitter_handle_is_optional()
117+
{
118+
$user = $this->createUser(['email' => 'freek@example.com', 'username' => 'freekmurze', 'twitter_handle' => 'freek-murze']);
119+
120+
$this->loginAs($user);
121+
122+
$this->visit('/settings')
123+
->submitForm('Save', [
124+
'name' => 'Freek Murze',
125+
'email' => 'freek@example.com',
126+
'username' => 'freekmurze',
127+
'twitter_handle' => '',
128+
])
129+
->seePageIs('/settings')
130+
->dontSee('freek-murze');
131+
132+
$this->assertNull($user->fresh()->twitterHandle());
133+
}
134+
113135
private function assertPasswordWasHashedAndSaved(): void
114136
{
115137
$this->assertTrue($this->app['hash']->check('newpassword', Auth::user()->getAuthPassword()));

0 commit comments

Comments
 (0)