@@ -85,6 +85,7 @@ export class CoursesAddComponent implements OnInit, OnDestroy {
8585 private stepsChange$ = new Subject < any [ ] > ( ) ;
8686 private initialState = '' ;
8787 private _steps = [ ] ;
88+ private preserveCoverStateUntilSubmit = false ;
8889 existingCoverAttachments : ExistingAttachment [ ] = [ ] ;
8990 private coverState : AttachmentInputState = { retained : [ ] , removed : [ ] , added : [ ] } ;
9091 savedCourse : any = null ;
@@ -102,6 +103,7 @@ export class CoursesAddComponent implements OnInit, OnDestroy {
102103 languageNames = languages . map ( list => list . name ) ;
103104 mockStep = { stepTitle : $localize `Add title` , description : '!!!' } ;
104105 @ViewChild ( CoursesStepComponent ) coursesStepComponent : CoursesStepComponent ;
106+ @ViewChild ( FileUploadComponent ) coverUploadComponent ?: FileUploadComponent ;
105107 get steps ( ) {
106108 return this . _steps ;
107109 }
@@ -152,15 +154,16 @@ export class CoursesAddComponent implements OnInit, OnDestroy {
152154 this . draftExists = draft !== undefined ;
153155 const doc = draft === undefined ? saved : draft ;
154156 this . setInitialTags ( tags , this . documentInfo , draft ) ;
155- if ( ! continued ) {
157+ if ( continued ) {
158+ this . preserveCoverStateUntilSubmit = ! ! this . coursesService . course . coverState ?. added ?. length ;
159+ this . setFormAndSteps ( this . coursesService . course ) ;
160+ this . setCoverState ( this . coursesService . course . coverState || this . coverState ) ;
161+ this . submitAddedExam ( ) ;
162+ } else {
156163 this . setFormAndSteps ( { form : doc , steps : doc . steps , tags : doc . tags , initialTags : this . coursesService . course . initialTags } ) ;
157164 this . setInitialState ( ) ;
158165 }
159166 } ) ;
160- if ( continued ) {
161- this . setFormAndSteps ( this . coursesService . course ) ;
162- this . submitAddedExam ( ) ;
163- }
164167 const returnRoute = this . router . createUrlTree ( [ '.' , { continue : true } ] , { relativeTo : this . route } ) ;
165168 this . coursesService . returnUrl = this . router . serializeUrl ( returnRoute ) ;
166169 this . coursesService . course = { form : this . courseForm . value , steps : this . steps } ;
@@ -267,7 +270,15 @@ export class CoursesAddComponent implements OnInit, OnDestroy {
267270 }
268271
269272 onCoverStateChange ( state : AttachmentInputState ) {
273+ if ( this . preserveCoverStateUntilSubmit && this . coverState . added . length && state . added . length === 0 ) {
274+ return ;
275+ }
276+ this . setCoverState ( state ) ;
277+ }
278+
279+ setCoverState ( state : AttachmentInputState ) {
270280 this . coverState = state ;
281+ this . coursesService . course = { coverState : state } ;
271282 }
272283
273284 setExistingCover ( course : any ) {
@@ -279,7 +290,7 @@ export class CoursesAddComponent implements OnInit, OnDestroy {
279290 url : couchAttachmentUrl ( environment . couchAddress , this . dbName , course . _id , fileName )
280291 } ] : [ ] ;
281292 // Seed cover state directly so a save can't drop the cover if it fires before the upload child emits.
282- this . coverState = { retained : [ ...this . existingCoverAttachments ] , removed : [ ] , added : [ ] } ;
293+ this . setCoverState ( { retained : [ ...this . existingCoverAttachments ] , removed : [ ] , added : [ ] } ) ;
283294 }
284295
285296 updateCourse ( courseInfo : FormGroup < CourseFormModel > [ 'value' ] , shouldNavigate : boolean ) {
@@ -329,7 +340,9 @@ export class CoursesAddComponent implements OnInit, OnDestroy {
329340 courseRes ;
330341 const message = ( this . pageType === 'Edit' ? $localize `Edited course: ` : $localize `Added course: ` ) + courseInfo . courseTitle ;
331342 this . courseChangeComplete ( message , savedRes , shouldNavigate ) ;
343+ this . preserveCoverStateUntilSubmit = false ;
332344 } , ( err ) => {
345+ this . preserveCoverStateUntilSubmit = false ;
333346 this . planetMessageService . showAlert ( $localize `There was an error saving this course` ) ;
334347 } ) ;
335348 }
@@ -381,6 +394,7 @@ export class CoursesAddComponent implements OnInit, OnDestroy {
381394 if ( ! this . draftExists ) {
382395 return ;
383396 }
397+ this . coverUploadComponent ?. clear ( ) ;
384398 if ( this . savedCourse ) {
385399 this . setFormAndSteps ( {
386400 form : this . savedCourse ,
@@ -395,7 +409,7 @@ export class CoursesAddComponent implements OnInit, OnDestroy {
395409 tags : [ ]
396410 } ) ;
397411 this . existingCoverAttachments = [ ] ;
398- this . coverState = { retained : [ ] , removed : [ ] , added : [ ] } ;
412+ this . setCoverState ( { retained : [ ] , removed : [ ] , added : [ ] } ) ;
399413 }
400414 this . coursesStepComponent . toList ( ) ;
401415 this . setInitialState ( ) ;
0 commit comments