11import { useState } from 'react' ;
2- import { Alert , Box , Button } from '@mui/material' ;
2+ import { Alert , Box , Button , Typography } from '@mui/material' ;
33import RequiredTextField from '../../../src/components/RequiredTextField' ;
4+ import ImageUpload from '../../../src/components/ImageUpload.jsx' ;
45import { useAppContext } from '../../../src/render.jsx' ;
56import { getCookie } from '../../../src/utils' ;
67
78
89const CreateInstructorForm = ( { onSuccess, activeOrganizationId } ) => {
910 const [ email , setEmail ] = useState ( '' ) ;
1011 const [ emailHelperText , setEmailHelperText ] = useState ( '' ) ;
12+ const [ displayName , setDisplayName ] = useState ( '' ) ;
13+ const [ displayNameHelperText , setDisplayNameHelperText ] = useState ( '' ) ;
14+ const [ photoPath , setPhotoPath ] = useState ( null ) ;
15+ const [ photoUrl , setPhotoUrl ] = useState ( null ) ;
1116 const [ errorMessage , setErrorMessage ] = useState ( '' ) ;
1217 const { localeMessages, apiBaseUrl } = useAppContext ( ) ;
1318
1419 const isValidEmail = ( value ) => / ^ [ ^ \s @ ] + @ [ ^ \s @ ] + \. [ ^ \s @ ] + $ / . test ( value ) ;
1520
1621 const handleSubmit = ( ) => {
1722 const trimmedEmail = email . trim ( ) ;
23+ const trimmedDisplayName = displayName . trim ( ) ;
24+ let valid = true ;
25+
1826 if ( ! trimmedEmail ) {
1927 setEmailHelperText ( localeMessages [ 'email_required_helper_text' ] ) ;
20- return ;
21- }
22- if ( ! isValidEmail ( trimmedEmail ) ) {
28+ valid = false ;
29+ } else if ( ! isValidEmail ( trimmedEmail ) ) {
2330 setEmailHelperText ( localeMessages [ 'invalid_email_helper_text' ] ) ;
24- return ;
31+ valid = false ;
32+ } else {
33+ setEmailHelperText ( '' ) ;
34+ }
35+
36+ if ( ! trimmedDisplayName ) {
37+ setDisplayNameHelperText ( localeMessages [ 'instructor_display_name_required' ] ) ;
38+ valid = false ;
39+ } else {
40+ setDisplayNameHelperText ( '' ) ;
2541 }
26- setEmailHelperText ( '' ) ;
42+
43+ if ( ! valid ) return ;
2744 setErrorMessage ( '' ) ;
2845
2946 fetch ( `${ apiBaseUrl } /users/get-or-create-by-email/` , {
@@ -47,7 +64,12 @@ const CreateInstructorForm = ({ onSuccess, activeOrganizationId }) => {
4764 'Content-Type' : 'application/json' ,
4865 'X-CSRFToken' : getCookie ( 'csrftoken' ) ,
4966 } ,
50- body : JSON . stringify ( { user_id : userData . id , role : 'instructor' } ) ,
67+ body : JSON . stringify ( {
68+ user_id : userData . id ,
69+ role : 'instructor' ,
70+ display_name : trimmedDisplayName ,
71+ photo : photoPath ,
72+ } ) ,
5173 } )
5274 )
5375 . then ( ( response ) => {
@@ -57,6 +79,9 @@ const CreateInstructorForm = ({ onSuccess, activeOrganizationId }) => {
5779 . then ( ( orgUserData ) => {
5880 if ( onSuccess ) onSuccess ( orgUserData ) ;
5981 setEmail ( '' ) ;
82+ setDisplayName ( '' ) ;
83+ setPhotoPath ( null ) ;
84+ setPhotoUrl ( null ) ;
6085 } )
6186 . catch ( ( error ) => {
6287 console . error ( 'Error adding instructor:' , error ) ;
@@ -76,9 +101,33 @@ const CreateInstructorForm = ({ onSuccess, activeOrganizationId }) => {
76101 onChange = { ( e ) => setEmail ( e . target . value ) }
77102 type = "email"
78103 />
79- < Button variant = "contained" onClick = { handleSubmit } sx = { { mt : 1 , boxShadow : 'none' } } >
104+ < RequiredTextField
105+ label = { localeMessages [ 'instructor_display_name' ] }
106+ helperText = { displayNameHelperText }
107+ fullWidth
108+ margin = "normal"
109+ value = { displayName }
110+ onChange = { ( e ) => {
111+ setDisplayName ( e . target . value ) ;
112+ if ( displayNameHelperText ) setDisplayNameHelperText ( '' ) ;
113+ } }
114+ />
115+ < Typography variant = "body2" sx = { { mt : 1 , mb : 0.5 } } >
116+ { localeMessages [ 'instructor_photo' ] }
117+ </ Typography >
118+ < ImageUpload
119+ initialUrl = { photoUrl }
120+ onUploadSuccess = { ( data ) => {
121+ setPhotoUrl ( data . file_url ) ;
122+ setPhotoPath ( data . file_path ) ;
123+ } }
124+ onUploadError = { ( ) => setErrorMessage ( localeMessages [ 'instructor_add_failed' ] ) }
125+ />
126+
127+ < Button variant = "contained" onClick = { handleSubmit } sx = { { mt : 1 , boxShadow : 'none' , display : 'block' , ml : 'auto' } } >
80128 { localeMessages [ 'add_instructor' ] }
81129 </ Button >
130+
82131 </ Box >
83132 ) ;
84133} ;
0 commit comments