1
- import { Component , OnInit , ViewChild , ElementRef , ChangeDetectorRef , AfterViewInit } from '@angular/core' ;
1
+ import { Component , OnInit , ViewChild , ElementRef , ChangeDetectorRef , AfterViewInit , OnDestroy } from '@angular/core' ;
2
2
import { Router , ActivatedRoute } from '@angular/router' ;
3
3
import { TdTextEditorComponent } from '@covalent/text-editor' ;
4
- import { Candidate , CandidateManagementService , CandidateManagementStore } from 'src/app/core/candidate-management' ;
4
+ import { Candidate , CandidateManagementService } from 'src/app/core/candidate-management' ;
5
5
import PatternLanguageSchemaModel from 'src/app/core/model/pattern-language-schema.model' ;
6
6
import PatternSectionSchema from 'src/app/core/model/hal/pattern-section-schema.model' ;
7
7
import { patternLanguageNone } from 'src/app/core/component/pattern-language-picker/pattern-language-picker.component' ;
@@ -18,13 +18,14 @@ import { PrivilegeService } from 'src/app/authentication/_services/privilege.ser
18
18
import { Author , AuthorModel } from 'src/app/core/author-management' ;
19
19
import { environment } from 'src/environments/environment' ;
20
20
import { AuthenticationService } from 'src/app/authentication/_services/authentication.service' ;
21
+ import { Subscription } from 'rxjs' ;
21
22
22
23
@Component ( {
23
24
selector : 'pp-candidate-management-detail' ,
24
25
templateUrl : './candidate-management-detail.component.html' ,
25
26
styleUrls : [ './candidate-management-detail.component.scss' ]
26
27
} )
27
- export class CandidateManagementDetailComponent implements OnInit , AfterViewInit {
28
+ export class CandidateManagementDetailComponent implements OnInit , AfterViewInit , OnDestroy {
28
29
29
30
@ViewChild ( 'textEditor' ) private _textEditor : TdTextEditorComponent ;
30
31
@ViewChild ( 'candidateView' ) candidateDiv : ElementRef ;
@@ -45,13 +46,14 @@ export class CandidateManagementDetailComponent implements OnInit, AfterViewInit
45
46
pattern = false ;
46
47
treshhold = true ;
47
48
treshholdSetting = 4.0 ;
48
- confirmDialog : ConfirmData ;
49
+ private _confirmDialogData : ConfirmData ;
50
+
51
+ private activeRouteSubscription : Subscription | null = null ;
49
52
50
53
constructor (
51
54
private router : Router ,
52
55
private activeRoute : ActivatedRoute ,
53
56
private candidateManagementService : CandidateManagementService ,
54
- public candidateStore : CandidateManagementStore ,
55
57
private patternService : PatternService ,
56
58
public dialog : MatDialog ,
57
59
private p : PrivilegeService ,
@@ -60,47 +62,63 @@ export class CandidateManagementDetailComponent implements OnInit, AfterViewInit
60
62
) { }
61
63
62
64
ngOnInit ( ) : void {
63
-
64
- this . candidateStore . candidate . subscribe ( ( _candidate : Candidate ) => {
65
- if ( _candidate && this . router . url . includes ( 'detail' ) ) {
66
- this . disabled = true ;
67
- this . candidate = _candidate ;
68
- this . contentToMarkdown ( ) ;
69
- this . checkTreshhold ( ) ;
70
-
71
- } else if ( _candidate && this . router . url . includes ( 'edit' ) ) {
72
- this . candidate = _candidate ;
73
- this . contentToMarkdown ( ) ;
74
- this . edit ( ) ;
75
- this . checkTreshhold ( ) ;
76
-
77
- } else if ( ! _candidate && window . history . state . data && window . history . state . data instanceof Candidate ) {
78
- this . candidate = window . history . state . data as Candidate
79
- this . contentToMarkdown ( ) ;
80
- this . edit ( ) ;
81
- this . checkTreshhold ( ) ;
82
-
83
- } else {
84
- this . disabled = false ;
85
- this . candidate = new Candidate ( null , 'New Candidate' , null , null ) ;
86
- this . patternLanguageSelectedChange ( patternLanguageNone ) ;
87
- // Preset author
88
- this . auth . user . subscribe ( _user => {
89
- if ( _user && ! this . candidate . authors ) this . candidate . authors = [ new AuthorModel ( _user . id , Author . OWNER , _user . name ) ] ;
90
- } )
91
- }
92
- this . confirmDialog = {
93
- title : `Change Pattern Language for Candidate ${ this . candidate . name } ` ,
94
- text : 'If you change the language everything writen will be deleted and the'
95
- + ' new pattern schema will be used'
65
+ this . activeRouteSubscription = this . activeRoute . params . subscribe ( params => {
66
+ let candidateUri = `/candidates/${ params . name } ` ;
67
+ switch ( params . action ) {
68
+ case 'detail' : {
69
+ this . disabled = true ;
70
+ this . candidateManagementService . getCandidateByUri ( candidateUri ) . subscribe ( result => {
71
+ this . candidate = result ;
72
+ this . contentToMarkdown ( ) ;
73
+ this . checkTreshhold ( ) ;
74
+ } ) ;
75
+ break ;
76
+ }
77
+ case 'edit' : {
78
+ this . candidateManagementService . getCandidateByUri ( candidateUri ) . subscribe ( result => {
79
+ this . candidate = result ;
80
+ this . contentToMarkdown ( ) ;
81
+ this . edit ( ) ;
82
+ this . checkTreshhold ( ) ;
83
+ } ) ;
84
+ break ;
85
+ }
86
+ case 'create' : {
87
+ this . disabled = false ;
88
+ let candidateName = params . name ? params . name : 'New Candidate' ;
89
+ this . candidate = new Candidate ( null , candidateName , null , null ) ;
90
+ this . patternLanguageSelectedChange ( patternLanguageNone ) ;
91
+ // Preset author
92
+ this . auth . user . subscribe ( _user => {
93
+ if ( _user && ! this . candidate . authors ) this . candidate . authors = [ new AuthorModel ( _user . id , Author . OWNER , _user . name ) ] ;
94
+ } ) ;
95
+ break ;
96
+ }
97
+ default : {
98
+ // Unknown action - show candidate list
99
+ this . router . navigateByUrl ( '/candidate' ) ;
100
+ break ;
101
+ }
96
102
}
97
103
} ) ;
98
104
}
99
105
106
+ ngOnDestroy ( ) : void {
107
+ this . activeRouteSubscription ?. unsubscribe ( ) ;
108
+ }
109
+
100
110
ngAfterViewInit ( ) : void {
101
111
this . setCommentSectionHeight ( ) ;
102
112
}
103
113
114
+ public get confirmDialogData ( ) {
115
+ return {
116
+ title : `Change Pattern Language for Candidate ${ this . candidate . name } ` ,
117
+ text : 'If you change the language everything writen will be deleted and the'
118
+ + ' new pattern schema will be used'
119
+ } ;
120
+ }
121
+
104
122
// CHANGE MARKDOWN
105
123
contentToMarkdown ( ) {
106
124
this . candidateMarkdown = `# ${ this . candidate . name } \n` ;
@@ -218,9 +236,6 @@ export class CandidateManagementDetailComponent implements OnInit, AfterViewInit
218
236
} )
219
237
220
238
this . candidateManagementService . createCandidate ( this . candidate ) . subscribe ( result => {
221
- this . candidate = result ;
222
- this . contentToMarkdown ( ) ;
223
-
224
239
// call update for all additional authors
225
240
for ( let author of authorlist ) {
226
241
if ( author . userId !== first_author ) {
@@ -230,7 +245,7 @@ export class CandidateManagementDetailComponent implements OnInit, AfterViewInit
230
245
}
231
246
}
232
247
233
- this . disabled = true ;
248
+ this . router . navigate ( [ './candidate/detail' , this . candidate . name ] ) ;
234
249
} )
235
250
}
236
251
@@ -239,6 +254,7 @@ export class CandidateManagementDetailComponent implements OnInit, AfterViewInit
239
254
this . candidate = result ;
240
255
this . contentToMarkdown ( ) ;
241
256
this . disabled = true ;
257
+ this . router . navigate ( [ './candidate/detail' , this . candidate . name ] ) ;
242
258
} )
243
259
}
244
260
@@ -390,4 +406,5 @@ export class CandidateManagementDetailComponent implements OnInit, AfterViewInit
390
406
this . candidateHeight = this . candidateDiv . nativeElement . offsetHeight ;
391
407
this . ref . detectChanges ( ) ;
392
408
}
409
+
393
410
}
0 commit comments