@@ -3,6 +3,7 @@ import RequiredTextField from '../../../src/components/RequiredTextField.jsx';
33import InfoOutlinedIcon from '@mui/icons-material/InfoOutlined' ;
44import IconButton from '@mui/material/IconButton' ;
55import AddImapConnectionForm from './AddImapConnectionForm.jsx' ;
6+ import ImageUpload from '../../../src/components/ImageUpload.jsx' ;
67import { useEffect , useState } from 'react' ;
78import { getCookie } from '../../../src/utils.js' ;
89
@@ -17,6 +18,8 @@ function CourseForm({successCallback, failureCallback, cancelCallback, activeOrg
1718 const [ slugHelperText , setSlugHelperText ] = useState ( "" )
1819 const [ descriptionHelperText , setDescriptionHelperText ] = useState ( "" )
1920 const [ errorMessage , setErrorMessage ] = useState ( "" )
21+ const [ imageUrl , setImageUrl ] = useState ( null )
22+ const [ imageServerPath , setImageServerPath ] = useState ( null )
2023
2124 const apiBaseUrl = localStorage . getItem ( 'apiBaseUrl' ) ;
2225
@@ -48,6 +51,8 @@ function CourseForm({successCallback, failureCallback, cancelCallback, activeOrg
4851 setCourseTitle ( data . title ) ;
4952 setCourseSlug ( data . slug ) ;
5053 setCourseDescription ( data . description ) ;
54+ setImageUrl ( data . image ) ;
55+ setImageServerPath ( data . image_path ) ;
5156 if ( data . imap_connection_id ) {
5257 setImapConnectionId ( data . imap_connection_id ) ;
5358 setAddImapConnection ( true ) ;
@@ -102,11 +107,12 @@ function CourseForm({successCallback, failureCallback, cancelCallback, activeOrg
102107 // slug is not updatable
103108 description : courseDescription ,
104109 imap_connection_id : imapConnectionId && addImapConnection ? parseInt ( imapConnectionId ) : null ,
105- reset_imap_connection : ! addImapConnection || imapConnectionId == null
110+ reset_imap_connection : ! addImapConnection || imapConnectionId == null ,
111+ image : imageServerPath ? imageServerPath : null
106112 } ) ,
107113 } )
108114 . then ( response => {
109- if ( ! response . ok ) {
115+ if ( ! response . ok && response . status != 409 ) {
110116 if ( response . status >= 500 ) {
111117 setErrorMessage ( "Server error occurred. Please try again later." ) ;
112118 }
@@ -115,12 +121,16 @@ function CourseForm({successCallback, failureCallback, cancelCallback, activeOrg
115121 return response . json ( ) ;
116122 } )
117123 . then ( data => {
118- console . log ( 'Success:' , data ) ;
119- successCallback ( data ) ;
124+ if ( data . error ) {
125+ setErrorMessage ( data . error ) ;
126+ failureCallback ( data ) ;
127+ } else {
128+ console . log ( 'Success:' , data ) ;
129+ successCallback ( data ) ;
130+ }
120131 } )
121132 . catch ( ( error ) => {
122133 console . error ( 'Error:' , error ) ;
123- if ( error )
124134 failureCallback ( error ) ;
125135 } ) ;
126136 } ;
@@ -141,32 +151,34 @@ function CourseForm({successCallback, failureCallback, cancelCallback, activeOrg
141151 title : courseTitle ,
142152 slug : courseSlug ,
143153 description : courseDescription ,
144- imap_connection_id : imapConnectionId ? parseInt ( imapConnectionId ) : null
154+ imap_connection_id : imapConnectionId ? parseInt ( imapConnectionId ) : null ,
155+ image : imageServerPath ? imageServerPath : null
145156 } ) ,
146157 } )
147158 . then ( response => {
148- if ( ! response . ok ) {
149- if ( response . status === 409 ) {
150- setErrorMessage ( "A course with this title or slug already exists." ) ;
151- }
152- else if ( response . status >= 500 ) {
159+ if ( ! response . ok && response . status != 409 ) {
160+ if ( response . status >= 500 ) {
153161 setErrorMessage ( "Server error occurred. Please try again later." ) ;
154162 }
155163 throw new Error ( 'Network response was not ok' ) ;
156164 }
157165 return response . json ( ) ;
158166 } )
159167 . then ( data => {
160- console . log ( 'Success:' , data ) ;
161- // Optionally reset form fields here
162- setCourseTitle ( "" ) ;
163- setCourseSlug ( "" ) ;
164- setCourseDescription ( "" ) ;
165- successCallback ( data ) ;
168+ if ( data . error ) {
169+ setErrorMessage ( data . error ) ;
170+ failureCallback ( data ) ;
171+ } else {
172+ console . log ( 'Success:' , data ) ;
173+ // Optionally reset form fields here
174+ setCourseTitle ( "" ) ;
175+ setCourseSlug ( "" ) ;
176+ setCourseDescription ( "" ) ;
177+ successCallback ( data ) ;
178+ }
166179 } )
167180 . catch ( ( error ) => {
168181 console . error ( 'Error:' , error ) ;
169- if ( error )
170182 failureCallback ( error ) ;
171183 } ) ;
172184 } ;
@@ -193,7 +205,13 @@ function CourseForm({successCallback, failureCallback, cancelCallback, activeOrg
193205 activeOrganizationId = { activeOrganizationId }
194206 initialImapConnectionId = { imapConnectionId }
195207 />
196- </ Box > }
208+ </ Box > }
209+ < Box >
210+ < ImageUpload initialUrl = { imageUrl } onUploadSuccess = { ( data ) => {
211+ setImageUrl ( data . file_url ) ;
212+ setImageServerPath ( data . file_path ) ;
213+ } } />
214+ </ Box >
197215 < Box mt = { 2 } textAlign = "right" >
198216 < Button onClick = { cancelCallback } sx = { { mr : 1 } } > Cancel</ Button >
199217 { createMode && < Button variant = "contained" onClick = { ( ) => handleCreateCourse ( ) } sx = { { boxShadow : 'none' } } > { localeMessages [ "create" ] } </ Button > }
0 commit comments