22
33namespace Backstage \Filament \Users \Pages \Auth ;
44
5+ use Filament \Pages \Auth \EditProfile as BaseEditProfile ;
6+ use Filament \Forms \Components \Select ;
57use Backstage \Filament \Users \Models \User ;
8+ use Backstage \Laravel \Users \Eloquent \Models \UserNotificationPreference ;
69use Backstage \Laravel \Users \Enums \NotificationType ;
7- use Filament \Forms \Components \Select ;
8- use Filament \Pages \Auth \EditProfile as BaseEditProfile ;
910
1011class EditProfile extends BaseEditProfile
1112{
@@ -36,31 +37,38 @@ public static function getNotificationFormComponent(): Select
3637 $ options = [];
3738
3839 foreach ($ types as $ type ) {
39- $ options [$ type ->value ] = $ type ->label ();
40+ $ options [$ type ->value ] = $ type ->label ();
4041 }
4142
4243 return Select::make ('notification_preferences ' )
4344 ->label (__ ('Notification preferences ' ))
44- ->options (fn () => $ options )
45+ ->options (fn () => $ options )
4546 ->live ()
46- ->placeholder (fn () => ('Select notification preferences ' ))
47+ ->placeholder (fn () => ('Select notification preferences ' ))
4748 ->searchingMessage (__ ('Searching notification types... ' ))
4849 ->searchPrompt (__ ('Search notification types... ' ))
49- ->saveRelationshipsUsing (function (User $ record , array $ state ) {
50- $ state = collect ($ state )->map (fn ($ value ) => NotificationType::from ($ value ));
50+ ->preload ()
51+ ->multiple ();
52+ }
5153
52- $ state ->each (function (NotificationType $ type ) use ($ record ) {
53- if (! $ record ->notificationPreferences ->contains ('navigation_type ' , $ type ->value )) {
54+ public function saveNotificationPreferences ()
55+ {
56+ $ formState = $ this ->form ->getState ();
5457
55- $ record ->notificationPreferences ()->create ([
56- 'navigation_type ' => $ type ->value ,
57- ]);
58- }
59- });
58+ $ state = $ formState ['notification_preferences ' ];
6059
61- $ record ->notificationPreferences ()->whereNotIn ('navigation_type ' , $ state )->delete ();
62- })
63- ->multiple ();
60+ $ state = collect ($ state )->map (fn ($ value ) => NotificationType::from ($ value ));
61+
62+ $ record = $ this ->getUser ();
63+
64+ $ state ->each (function (NotificationType $ type ) use ($ record ) {
65+ if (!$ record ->notificationPreferences ->contains ('navigation_type ' , $ type ->value )) {
66+
67+ $ record ->notificationPreferences ()->create ([
68+ 'navigation_type ' => $ type ->value ,
69+ ]);
70+ }
71+ });
6472 }
6573
6674 protected function mutateFormDataBeforeFill (array $ data ): array
@@ -70,7 +78,7 @@ protected function mutateFormDataBeforeFill(array $data): array
7078 $ user = $ this ->getUser ();
7179
7280 if ($ user ->notificationPreferences ->isNotEmpty ()) {
73- $ data ['notification_preferences ' ] = $ user ->notificationPreferences ->pluck ('navigation_type ' )->map (fn (NotificationType $ record ) => $ record ->value )->toArray ();
81+ $ data ['notification_preferences ' ] = $ user ->notificationPreferences ->pluck ('navigation_type ' )->map (fn (NotificationType $ record ) => $ record ->value )->toArray ();
7482 }
7583
7684 return $ data ;
0 commit comments