77use App \Actions \UpdateUser ;
88use App \Models \User ;
99use App \Rules \UserUsername ;
10+ use App \Support \InlineFormHandler ;
1011use Illuminate \Console \Command ;
1112use Illuminate \Container \Attributes \CurrentUser ;
13+ use Illuminate \Support \Facades \Validator ;
1214
1315use function Laravel \Prompts \info ;
14- use function Laravel \Prompts \text ;
15- use function Laravel \Prompts \textarea ;
1616
1717final class EditProfileCommand extends Command
1818{
@@ -23,24 +23,46 @@ final class EditProfileCommand extends Command
2323
2424 public function handle (#[CurrentUser] User $ user , UpdateUser $ action ): void
2525 {
26- info ( ' Edit your profile ' ) ;
26+ $ form = new InlineFormHandler ;
2727
28- $ username = text (
28+ $ form ->addTextField (
29+ name: 'username ' ,
2930 label: 'Username ' ,
30- placeholder: 'Enter your username ' ,
3131 default: $ user ->username ,
32- required: true ,
33- validate: [ ' required ' , ' string ' , ' max: 255' , new UserUsername ( $ user )],
32+ placeholder: ' Enter your username ' ,
33+ maxLength: 255
3434 );
3535
36- $ bio = textarea (
36+ $ form ->addTextareaField (
37+ name: 'bio ' ,
3738 label: 'Bio ' ,
38- placeholder: 'Tell us about yourself... ' ,
3939 default: $ user ->bio ?? '' ,
40- validate: ['nullable ' , 'string ' , 'max:500 ' ],
40+ placeholder: 'Tell us about yourself... ' ,
41+ maxLength: 500
4142 );
4243
43- $ action ->handle ($ user , $ username , $ bio );
44+ $ values = $ form ->run ('Edit your profile ' );
45+
46+ if ($ values === null ) {
47+ info ('Profile editing cancelled. ' );
48+
49+ return ;
50+ }
51+
52+ // Validate the input
53+ $ validator = Validator::make ($ values , [
54+ 'username ' => ['required ' , 'string ' , 'max:255 ' , new UserUsername ($ user )],
55+ 'bio ' => ['nullable ' , 'string ' , 'max:500 ' ],
56+ ]);
57+
58+ if ($ validator ->fails ()) {
59+ $ errors = $ validator ->errors ()->all ();
60+ info ('Validation failed: ' .implode (', ' , $ errors ));
61+
62+ return ;
63+ }
64+
65+ $ action ->handle ($ user , $ values ['username ' ], $ values ['bio ' ]);
4466
4567 info ('Profile updated successfully! ' );
4668 }
0 commit comments