Skip to content

Commit d403c70

Browse files
committed
chore: update appearance initialization
1 parent 5653f9c commit d403c70

File tree

17 files changed

+3942
-2276
lines changed

17 files changed

+3942
-2276
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ yarn-error.log
2323
/.vscode
2424
/.zed
2525
resources/js/ziggy.js
26+
.phpstorm.meta.php
27+
_ide_helper.php

app/Data/UserData.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Data;
6+
7+
use Spatie\LaravelData\Data;
8+
use Spatie\TypeScriptTransformer\Attributes\TypeScript;
9+
10+
#[TypeScript]
11+
final class UserData extends Data
12+
{
13+
public int $id;
14+
15+
public string $firstName;
16+
17+
public string $lastName;
18+
19+
public string $fullName;
20+
21+
public string $initials;
22+
23+
public string $email;
24+
25+
public ?string $profileImage = null;
26+
27+
public ?string $emailVerifiedAt = null;
28+
29+
public string $createdAt;
30+
31+
public string $updatedAt;
32+
}

app/Http/Middleware/HandleInertiaRequests.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Http\Middleware;
66

7+
use App\Data\UserData;
78
use Illuminate\Foundation\Inspiring;
89
use Illuminate\Http\Request;
910
use Inertia\Middleware;
@@ -55,7 +56,7 @@ public function share(Request $request): array
5556
'name' => config('app.name'),
5657
'quote' => ['message' => trim($message), 'author' => trim($author)],
5758
'auth' => [
58-
'user' => $request->user(),
59+
'user' => UserData::from($request->user()),
5960
],
6061
'ziggy' => [
6162
...(new Ziggy)->toArray(),

app/Models/User.php

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,62 @@
44

55
namespace App\Models;
66

7+
use Carbon\CarbonImmutable;
78
use Database\Factories\UserFactory;
9+
use Eloquent;
810
use Illuminate\Contracts\Auth\MustVerifyEmail;
11+
use Illuminate\Database\Eloquent\Builder;
12+
use Illuminate\Database\Eloquent\Casts\Attribute;
913
use Illuminate\Database\Eloquent\Factories\HasFactory;
1014
use Illuminate\Foundation\Auth\User as Authenticatable;
15+
use Illuminate\Notifications\DatabaseNotification;
16+
use Illuminate\Notifications\DatabaseNotificationCollection;
1117
use Illuminate\Notifications\Notifiable;
18+
use Illuminate\Support\Facades\Storage;
1219

20+
/**
21+
* @property int $id
22+
* @property string $first_name
23+
* @property string $last_name
24+
* @property string|null $avatar
25+
* @property string $email
26+
* @property CarbonImmutable|null $email_verified_at
27+
* @property string $password
28+
* @property string|null $remember_token
29+
* @property CarbonImmutable|null $created_at
30+
* @property CarbonImmutable|null $updated_at
31+
* @property-read DatabaseNotificationCollection<int, DatabaseNotification> $notifications
32+
* @property-read int|null $notifications_count
33+
*
34+
* @method static UserFactory factory($count = null, $state = [])
35+
* @method static Builder<static>|User newModelQuery()
36+
* @method static Builder<static>|User newQuery()
37+
* @method static Builder<static>|User query()
38+
* @method static Builder<static>|User whereAvatar($value)
39+
* @method static Builder<static>|User whereCreatedAt($value)
40+
* @method static Builder<static>|User whereEmail($value)
41+
* @method static Builder<static>|User whereEmailVerifiedAt($value)
42+
* @method static Builder<static>|User whereFirstName($value)
43+
* @method static Builder<static>|User whereId($value)
44+
* @method static Builder<static>|User whereLastName($value)
45+
* @method static Builder<static>|User wherePassword($value)
46+
* @method static Builder<static>|User whereRememberToken($value)
47+
* @method static Builder<static>|User whereUpdatedAt($value)
48+
*
49+
* @mixin Eloquent
50+
*/
1351
final class User extends Authenticatable implements MustVerifyEmail
1452
{
1553
/** @use HasFactory<UserFactory> */
1654
use HasFactory, Notifiable;
1755

1856
/**
19-
* The attributes that are mass assignable.
20-
*
2157
* @var list<string>
2258
*/
23-
protected $fillable = [
24-
'name',
25-
'email',
26-
'password',
59+
protected $appends = [
60+
'full_name',
61+
'initials',
62+
'profile_image',
2763
];
2864

2965
/**
@@ -48,4 +84,35 @@ protected function casts(): array
4884
'password' => 'hashed',
4985
];
5086
}
87+
88+
/**
89+
* @return Attribute<string, string>
90+
*/
91+
protected function fullName(): Attribute
92+
{
93+
return Attribute::make(fn (): string => "$this->first_name $this->last_name");
94+
}
95+
96+
/**
97+
* @return Attribute<string, string>
98+
*/
99+
protected function initials(): Attribute
100+
{
101+
$firstNameInitial = substr($this->first_name ?? '', 0, 1);
102+
$lastNameInitial = substr($this->last_name ?? '', 0, 1);
103+
104+
return Attribute::make(fn (): string => $firstNameInitial.$lastNameInitial);
105+
}
106+
107+
/**
108+
* @return Attribute<string, string>
109+
*/
110+
protected function profileImage(): Attribute
111+
{
112+
$avatar = $this->avatar !== null
113+
? Storage::url($this->avatar)
114+
: null;
115+
116+
return Attribute::make(fn (): ?string => $avatar);
117+
}
51118
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Support;
6+
7+
use RuntimeException;
8+
use Spatie\TypeScriptTransformer\Formatters\Formatter;
9+
10+
final class StylisticEslintFormatter implements Formatter
11+
{
12+
public function format(string $file): void
13+
{
14+
exec('npm run lint:fix', $output, $resultCode);
15+
16+
if ($resultCode !== 0) {
17+
throw new RuntimeException(
18+
sprintf(
19+
'Failed to format file %s. Output: %s',
20+
$file,
21+
implode("\n", $output)
22+
)
23+
);
24+
}
25+
}
26+
}

composer.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"inertiajs/inertia-laravel": "^2.0",
1414
"laravel/framework": "^12.0",
1515
"laravel/tinker": "^2.10.1",
16+
"spatie/laravel-data": "^4.15",
17+
"spatie/laravel-typescript-transformer": "^2.5",
1618
"tightenco/ziggy": "^2.4"
1719
},
1820
"require-dev": {
@@ -71,6 +73,12 @@
7173
"Composer\\Config::disableProcessTimeout",
7274
"npx concurrently -c \"#c4b5fd,#fb7185,#fdba74\" \"php artisan queue:listen --tries=1\" \"php artisan pail --timeout=0\" \"php artisan inertia:start-ssr\" --names=queue,logs,ssr"
7375
],
76+
"generate:ide-helpers": [
77+
"php artisan ide-helper:generate",
78+
"php artisan ide-helper:models -RW",
79+
"php artisan ide-helper:meta",
80+
"@fmt"
81+
],
7482
"test:unit": "vendor/bin/pest --parallel",
7583
"test:watch": "vendor/bin/pest --parallel --watch",
7684
"test:coverage": "vendor/bin/pest --coverage --parallel",
@@ -108,6 +116,11 @@
108116
"@refactor:test",
109117
"@test",
110118
"@typos"
119+
],
120+
"transform": "php artisan typescript:transform --format",
121+
"reschema": [
122+
"php artisan migrate:fresh --seed",
123+
"@generate:ide-helpers"
111124
]
112125
},
113126
"extra": {

0 commit comments

Comments
 (0)