Skip to content

Commit 159769c

Browse files
committed
Add twitter handle to user
1 parent 1dc5b53 commit 159769c

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
@@ -22,7 +22,7 @@ final class UpdateProfile
2222
public function __construct(User $user, array $attributes = [])
2323
{
2424
$this->user = $user;
25-
$this->attributes = Arr::only($attributes, ['name', 'email', 'username', 'github_username', 'bio']);
25+
$this->attributes = Arr::only($attributes, ['name', 'email', 'username', 'github_username', 'bio', 'twitter_handle']);
2626
}
2727

2828
public static function fromRequest(User $user, UpdateProfileRequest $request): self
@@ -32,6 +32,7 @@ public static function fromRequest(User $user, UpdateProfileRequest $request): s
3232
'email' => $request->email(),
3333
'username' => strtolower($request->username()),
3434
'bio' => trim(strip_tags($request->bio())),
35+
'twitter_handle' => $request->twitterHandle(),
3536
]);
3637
}
3738

app/User.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ final class User extends Authenticatable implements MustVerifyEmail
3737
protected $fillable = [
3838
'name',
3939
'email',
40+
'twitter_handle',
4041
'username',
4142
'password',
4243
'ip',
@@ -82,6 +83,11 @@ public function githubUsername(): string
8283
return $this->github_username;
8384
}
8485

86+
public function twitterHandle(): ?string
87+
{
88+
return $this->twitter_handle;
89+
}
90+
8591
public function gravatarUrl($size = 100): string
8692
{
8793
$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
@@ -41,6 +41,12 @@
4141
@error('username')
4242
@endFormGroup
4343

44+
@formGroup('twitter_handle')
45+
<label for="twitter_handle">Twitter handle</label>
46+
<input type="text" name="twitter_handle" id="twitter_handle" value="{{ Auth::user()->twitterHandle() }}" />
47+
@error('twitter_handle')
48+
@endFormGroup
49+
4450
@formGroup('bio')
4551
<label for="bio">Bio</label>
4652
<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)