11import {
2+ redirect ,
23 RouterContextProvider ,
4+ useFetcher ,
35 useLoaderData ,
6+ type ActionFunctionArgs ,
47 type LoaderFunctionArgs ,
58} from 'react-router' ;
69import { requireAuthCookie } from '@plone/react-router' ;
710import {
811 ploneClientContext ,
912 ploneUserContext ,
1013} from '@plone/aurora/app/middleware.server' ;
14+ import { Button } from '@plone/components/quanta' ;
1115import { useAppForm } from '../components/Form/Form' ;
1216
1317export async function loader ( {
@@ -21,15 +25,60 @@ export async function loader({
2125 return { user, userschema } ;
2226}
2327
28+ export async function action ( {
29+ request,
30+ context,
31+ } : ActionFunctionArgs < RouterContextProvider > ) {
32+ await requireAuthCookie ( request ) ;
33+
34+ const data = await request . json ( ) ;
35+ const cli = context . get ( ploneClientContext ) ;
36+ const user = context . get ( ploneUserContext ) ;
37+
38+ if ( user ?. id ) {
39+ await cli . updateUser ( { id : user . id , data } ) ;
40+ }
41+
42+ return redirect ( '/@@personal-information' ) ;
43+ }
44+
45+ type LoaderData = Awaited < ReturnType < typeof loader > > ;
46+
2447export default function PersonalInformation ( ) {
2548 const { user, userschema } = useLoaderData < typeof loader > ( ) ;
49+ // reload persisted values
50+ return (
51+ < PersonalInformationForm
52+ key = { JSON . stringify ( user ) }
53+ user = { user }
54+ userschema = { userschema }
55+ />
56+ ) ;
57+ }
58+
59+ function PersonalInformationForm ( {
60+ user,
61+ userschema,
62+ } : {
63+ user : LoaderData [ 'user' ] ;
64+ userschema : LoaderData [ 'userschema' ] ;
65+ } ) {
2666 const properties = userschema . properties as Record < string , any > ;
67+ const fetcher = useFetcher ( ) ;
2768
2869 const form = useAppForm ( {
2970 defaultValues : ( user ?? { } ) as Record < string , unknown > ,
3071 onSubmit : async ( { value } ) => {
31- // eslint-disable-next-line no-console
32- console . log ( 'personal-information submit (not wired yet)' , value ) ;
72+ // skip `portrait`
73+ const data : Record < string , string > = { } ;
74+ userschema . fieldsets . forEach ( ( fieldset ) =>
75+ fieldset . fields . forEach ( ( field ) => {
76+ if ( field === 'portrait' ) return ;
77+ const v = ( value as Record < string , unknown > ) [ field ] ;
78+ if ( typeof v === 'string' && v !== '' ) data [ field ] = v ;
79+ } ) ,
80+ ) ;
81+ fetcher . submit ( data , { method : 'post' , encType : 'application/json' } ) ;
3382 } ,
3483 } ) ;
3584
@@ -59,6 +108,14 @@ export default function PersonalInformation() {
59108 ) ) }
60109 </ div >
61110 ) ) }
111+ < Button
112+ type = "submit"
113+ variant = "primary"
114+ accent
115+ onPress = { ( ) => form . handleSubmit ( ) }
116+ >
117+ Save
118+ </ Button >
62119 </ form >
63120 </ >
64121 ) ;
0 commit comments