1- import { Component , OnInit , ViewChild , ElementRef , ChangeDetectorRef , AfterViewInit } from '@angular/core' ;
1+ import { Component , OnInit , ViewChild , ElementRef , ChangeDetectorRef , AfterViewInit , OnDestroy } from '@angular/core' ;
22import { Router , ActivatedRoute } from '@angular/router' ;
33import { 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' ;
55import PatternLanguageSchemaModel from 'src/app/core/model/pattern-language-schema.model' ;
66import PatternSectionSchema from 'src/app/core/model/hal/pattern-section-schema.model' ;
77import { 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
1818import { Author , AuthorModel } from 'src/app/core/author-management' ;
1919import { environment } from 'src/environments/environment' ;
2020import { AuthenticationService } from 'src/app/authentication/_services/authentication.service' ;
21+ import { Subscription } from 'rxjs' ;
2122
2223@Component ( {
2324 selector : 'pp-candidate-management-detail' ,
2425 templateUrl : './candidate-management-detail.component.html' ,
2526 styleUrls : [ './candidate-management-detail.component.scss' ]
2627} )
27- export class CandidateManagementDetailComponent implements OnInit , AfterViewInit {
28+ export class CandidateManagementDetailComponent implements OnInit , AfterViewInit , OnDestroy {
2829
2930 @ViewChild ( 'textEditor' ) private _textEditor : TdTextEditorComponent ;
3031 @ViewChild ( 'candidateView' ) candidateDiv : ElementRef ;
@@ -45,13 +46,14 @@ export class CandidateManagementDetailComponent implements OnInit, AfterViewInit
4546 pattern = false ;
4647 treshhold = true ;
4748 treshholdSetting = 4.0 ;
48- confirmDialog : ConfirmData ;
49+ private _confirmDialogData : ConfirmData ;
50+
51+ private activeRouteSubscription : Subscription | null = null ;
4952
5053 constructor (
5154 private router : Router ,
5255 private activeRoute : ActivatedRoute ,
5356 private candidateManagementService : CandidateManagementService ,
54- public candidateStore : CandidateManagementStore ,
5557 private patternService : PatternService ,
5658 public dialog : MatDialog ,
5759 private p : PrivilegeService ,
@@ -60,47 +62,63 @@ export class CandidateManagementDetailComponent implements OnInit, AfterViewInit
6062 ) { }
6163
6264 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+ }
96102 }
97103 } ) ;
98104 }
99105
106+ ngOnDestroy ( ) : void {
107+ this . activeRouteSubscription ?. unsubscribe ( ) ;
108+ }
109+
100110 ngAfterViewInit ( ) : void {
101111 this . setCommentSectionHeight ( ) ;
102112 }
103113
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+
104122 // CHANGE MARKDOWN
105123 contentToMarkdown ( ) {
106124 this . candidateMarkdown = `# ${ this . candidate . name } \n` ;
@@ -218,9 +236,6 @@ export class CandidateManagementDetailComponent implements OnInit, AfterViewInit
218236 } )
219237
220238 this . candidateManagementService . createCandidate ( this . candidate ) . subscribe ( result => {
221- this . candidate = result ;
222- this . contentToMarkdown ( ) ;
223-
224239 // call update for all additional authors
225240 for ( let author of authorlist ) {
226241 if ( author . userId !== first_author ) {
@@ -230,7 +245,7 @@ export class CandidateManagementDetailComponent implements OnInit, AfterViewInit
230245 }
231246 }
232247
233- this . disabled = true ;
248+ this . router . navigate ( [ './candidate/detail' , this . candidate . name ] ) ;
234249 } )
235250 }
236251
@@ -239,6 +254,7 @@ export class CandidateManagementDetailComponent implements OnInit, AfterViewInit
239254 this . candidate = result ;
240255 this . contentToMarkdown ( ) ;
241256 this . disabled = true ;
257+ this . router . navigate ( [ './candidate/detail' , this . candidate . name ] ) ;
242258 } )
243259 }
244260
@@ -390,4 +406,5 @@ export class CandidateManagementDetailComponent implements OnInit, AfterViewInit
390406 this . candidateHeight = this . candidateDiv . nativeElement . offsetHeight ;
391407 this . ref . detectChanges ( ) ;
392408 }
409+
393410}
0 commit comments