Skip to content

Commit 76540a2

Browse files
wip
1 parent f12c588 commit 76540a2

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
"require": {
2424
"php": "^8.1",
25-
"backstage/laravel-users": "^0.0.20",
25+
"backstage/laravel-users": "^0.0.21",
2626
"filament/filament": "^3.0",
2727
"laravel/sanctum": ">=3.3.3",
2828
"spatie/laravel-package-tools": "^1.15.0",

src/Pages/Auth/EditProfile.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Backstage\Users\Pages\Auth;
4+
5+
use Filament\Pages\Auth\EditProfile;
6+
use Filament\Forms\Components\Select;
7+
use Backstage\Filament\Users\Models\User;
8+
use Backstage\Laravel\Users\Enums\NotificationType;
9+
10+
class Profile extends EditProfile
11+
{
12+
protected function getForms(): array
13+
{
14+
return [
15+
'form' => $this->form(
16+
$this->makeForm()
17+
->schema([
18+
$this->getNameFormComponent(),
19+
$this->getEmailFormComponent(),
20+
static::getNotificationFormComponent(),
21+
$this->getPasswordFormComponent(),
22+
$this->getPasswordConfirmationFormComponent(),
23+
])
24+
->operation('edit')
25+
->model($this->getUser())
26+
->statePath('data')
27+
->inlineLabel(! static::isSimple()),
28+
),
29+
];
30+
}
31+
32+
public static function getNotificationFormComponent(): Select
33+
{
34+
$types = NotificationType::cases();
35+
36+
$options = [];
37+
38+
foreach ($types as $type) {
39+
$options[$type->value] = $type->label();
40+
}
41+
42+
return Select::make('notification_preferences')
43+
->label(__('Notification preferences'))
44+
->options(fn() => $options)
45+
->live()
46+
->placeholder(fn() => ('Select notification preferences'))
47+
->searchingMessage(__('Searching notification types...'))
48+
->searchPrompt(__('Search notification types...'))
49+
->saveRelationshipsUsing(function (User $record, array $state) {
50+
$state = collect($state)->map(fn($value) => NotificationType::from($value));
51+
52+
$state->each(function (NotificationType $type) use ($record) {
53+
if (!$record->notificationPreferences->contains('navigation_type', $type->value)) {
54+
55+
$record->notificationPreferences()->create([
56+
'navigation_type' => $type->value,
57+
]);
58+
}
59+
});
60+
61+
$record->notificationPreferences()->whereNotIn('navigation_type', $state)->delete();
62+
})
63+
->multiple();
64+
}
65+
66+
protected function mutateFormDataBeforeFill(array $data): array
67+
{
68+
$data = parent::mutateFormDataBeforeFill($data);
69+
70+
$user = $this->getUser();
71+
72+
if ($user->notificationPreferences->isNotEmpty()) {
73+
$data['notification_preferences'] = $user->notificationPreferences->pluck('navigation_type')->map(fn(NotificationType $record) => $record->value)->toArray();
74+
}
75+
76+
return $data;
77+
}
78+
}

0 commit comments

Comments
 (0)