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