11import { CommonModule } from '@angular/common' ;
2- import { Component , Inject , OnInit , ViewChild } from '@angular/core' ;
2+ import { Component , DestroyRef , Inject , OnInit , ViewChild } from '@angular/core' ;
33import { FormControl , FormGroup , Validators } from '@angular/forms' ;
44import { MAT_DIALOG_DATA , MatDialogRef } from '@angular/material/dialog' ;
55import { TranslocoModule } from '@ngneat/transloco' ;
66import { SFProjectProfile } from 'realtime-server/lib/esm/scriptureforge/models/sf-project' ;
77import { Chapter , TextInfo } from 'realtime-server/lib/esm/scriptureforge/models/text-info' ;
88import { TextInfoPermission } from 'realtime-server/lib/esm/scriptureforge/models/text-info-permission' ;
9- import { BehaviorSubject , map } from 'rxjs' ;
9+ import { BehaviorSubject , map , switchMap } from 'rxjs' ;
10+ import { ActivatedProjectService } from 'xforge-common/activated-project.service' ;
1011import { I18nService } from 'xforge-common/i18n.service' ;
1112import { OnlineStatusService } from 'xforge-common/online-status.service' ;
1213import { UICommonModule } from 'xforge-common/ui-common.module' ;
1314import { SFUserProjectsService } from 'xforge-common/user-projects.service' ;
1415import { UserService } from 'xforge-common/user.service' ;
15- import { filterNullish } from 'xforge-common/util/rxjs-util' ;
16+ import { filterNullish , quietTakeUntilDestroyed } from 'xforge-common/util/rxjs-util' ;
1617import { XForgeCommonModule } from 'xforge-common/xforge-common.module' ;
1718import { SFProjectProfileDoc } from '../../../core/models/sf-project-profile-doc' ;
1819import { TextDoc , TextDocId } from '../../../core/models/text-doc' ;
@@ -23,15 +24,16 @@ import { ProjectSelectComponent } from '../../../project-select/project-select.c
2324import { CustomValidatorState as CustomErrorState , SFValidators } from '../../../shared/sfvalidators' ;
2425import { SharedModule } from '../../../shared/shared.module' ;
2526import { compareProjectsForSorting } from '../../../shared/utils' ;
27+ import { DraftGenerationService } from '../draft-generation.service' ;
2628
2729export interface DraftApplyDialogResult {
2830 projectId : string ;
31+ chapters : number [ ] ;
2932}
3033
3134export interface DraftApplyDialogConfig {
3235 initialParatextId ?: string ;
3336 bookNum : number ;
34- chapters : number [ ] ;
3537}
3638
3739@Component ( {
@@ -69,21 +71,25 @@ export class DraftApplyDialogComponent implements OnInit {
6971 bookName : this . bookName
7072 } )
7173 } ;
74+ isValid : boolean = false ;
7275
7376 // the project id to add the draft to
7477 private targetProjectId ?: string ;
7578 private paratextIdToProjectId : Map < string , string > = new Map < string , string > ( ) ;
76- isValid : boolean = false ;
79+ private chaptersWithDrafts : number [ ] = [ ] ;
7780
7881 constructor (
7982 @Inject ( MAT_DIALOG_DATA ) private data : DraftApplyDialogConfig ,
8083 @Inject ( MatDialogRef ) private dialogRef : MatDialogRef < DraftApplyDialogComponent , DraftApplyDialogResult > ,
8184 private readonly userProjectsService : SFUserProjectsService ,
8285 private readonly projectService : SFProjectService ,
86+ private readonly activatedProjectService : ActivatedProjectService ,
87+ private readonly draftGenerationService : DraftGenerationService ,
8388 private readonly textDocService : TextDocService ,
8489 readonly i18n : I18nService ,
8590 private readonly userService : UserService ,
86- private readonly onlineStatusService : OnlineStatusService
91+ private readonly onlineStatusService : OnlineStatusService ,
92+ private readonly destroyRef : DestroyRef
8793 ) {
8894 this . targetProject$ . pipe ( filterNullish ( ) ) . subscribe ( async project => {
8995 const chapters : number = await this . chaptersWithTextAsync ( project ) ;
@@ -149,6 +155,18 @@ export class DraftApplyDialogComponent implements OnInit {
149155 this . _projects = projects ;
150156 this . isLoading = false ;
151157 } ) ;
158+
159+ this . activatedProjectService . projectId$
160+ . pipe (
161+ quietTakeUntilDestroyed ( this . destroyRef ) ,
162+ filterNullish ( ) ,
163+ switchMap ( projectId => {
164+ return this . draftGenerationService . getDraftChaptersForBook ( projectId , this . data . bookNum ) ;
165+ } )
166+ )
167+ . subscribe ( draftChapters => {
168+ this . chaptersWithDrafts = draftChapters ?? [ ] ;
169+ } ) ;
152170 }
153171
154172 addToProject ( ) : void {
@@ -157,7 +175,7 @@ export class DraftApplyDialogComponent implements OnInit {
157175 if ( ! this . isAppOnline || ! this . isFormValid || this . targetProjectId == null || ! this . canEditProject ) {
158176 return ;
159177 }
160- this . dialogRef . close ( { projectId : this . targetProjectId } ) ;
178+ this . dialogRef . close ( { projectId : this . targetProjectId , chapters : this . chaptersWithDrafts } ) ;
161179 }
162180
163181 projectSelected ( paratextId : string ) : void {
@@ -185,7 +203,7 @@ export class DraftApplyDialogComponent implements OnInit {
185203 const bookIsEmpty : boolean = targetBook ?. chapters . length === 1 && targetBook ?. chapters [ 0 ] . lastVerse < 1 ;
186204 const targetBookChapters : number [ ] = targetBook ?. chapters . map ( c => c . number ) ?? [ ] ;
187205 this . projectHasMissingChapters =
188- bookIsEmpty || this . data . chapters . filter ( c => ! targetBookChapters . includes ( c ) ) . length > 0 ;
206+ bookIsEmpty || this . chaptersWithDrafts . filter ( c => ! targetBookChapters . includes ( c ) ) . length > 0 ;
189207 if ( this . projectHasMissingChapters ) {
190208 this . createChaptersControl . addValidators ( Validators . requiredTrue ) ;
191209 this . createChaptersControl . updateValueAndValidity ( ) ;
0 commit comments