1
1
import { Component , OnInit } from '@angular/core' ;
2
2
import { FormBuilder , FormGroup , Validators } from '@angular/forms' ;
3
3
import { ActivatedRoute , Router } from '@angular/router' ;
4
- import { News , Organization , Project } from 'src/app/_entities' ;
5
4
import { NewsModel , Role } from 'src/app/_models' ;
5
+ import { ProjectModel } from 'src/app/_models/project/project.model' ;
6
6
import { AuthenticationService , NewsService , ProjectService , UserService } from 'src/app/_services' ;
7
7
8
8
@Component ( {
@@ -14,14 +14,12 @@ export class EditNewsComponent implements OnInit {
14
14
15
15
// Data
16
16
id : number = 0 ;
17
- private news : News = new News ( ) ;
18
- organizations : Organization [ ] = [ ] ;
19
- projects : Project [ ] = [ ] ;
17
+ idProject : number = 0 ;
18
+ private news : NewsModel = new NewsModel ( ) ;
19
+ project : ProjectModel = new ProjectModel ( ) ;
20
20
21
21
// Form
22
22
form : FormGroup = this . formBuilder . group ( {
23
- organization : [ 0 ] ,
24
- project : [ 0 ] ,
25
23
title : [ '' , [ Validators . required , Validators . maxLength ( 255 ) ] ] ,
26
24
content : [ '' , [ Validators . required ] ]
27
25
} ) ;
@@ -39,56 +37,35 @@ export class EditNewsComponent implements OnInit {
39
37
private formBuilder : FormBuilder ,
40
38
private authenticationService : AuthenticationService ,
41
39
private newsService : NewsService ,
42
- private userService : UserService ) {
43
- this . route . params . subscribe ( params => this . id = params . id ) ;
40
+ private projectService : ProjectService ) {
41
+ this . route . params . subscribe ( params => {
42
+ this . id = params . id ;
43
+ this . idProject = params . idProject ;
44
+ } ) ;
44
45
}
45
46
46
47
ngOnInit ( ) : void {
47
- var organizationNewsFakeProject = new Project ( ) ;
48
- organizationNewsFakeProject . title = '---' ;
49
- this . projects . push ( organizationNewsFakeProject ) ;
50
- if ( this . isAdmin ) {
51
- var systemNewsFakeOrg = new Organization ( ) ;
52
- systemNewsFakeOrg . name = '---' ;
53
- this . organizations . push ( systemNewsFakeOrg ) ;
54
- }
55
48
if ( this . id > 0 ) {
56
49
this . newsService . getById ( this . id )
57
50
. subscribe ( response => {
58
- this . news = News . fromModel ( response ) ;
51
+ this . news = response ;
52
+ this . idProject = this . news . project . id ;
59
53
this . refresh ( ) ;
60
54
} ) ;
61
- this . form . controls [ 'organization' ] . disable ( ) ;
62
- this . form . controls [ 'project' ] . disable ( ) ;
63
55
} else {
64
56
this . refresh ( ) ;
65
57
}
66
58
}
67
59
68
60
refresh ( ) {
69
- this . form . controls [ 'organization' ] . setValue ( 0 ) ;
70
- this . form . controls [ 'project' ] . setValue ( 0 ) ;
71
61
this . form . controls [ 'title' ] . setValue ( this . news . title ) ;
72
62
this . form . controls [ 'content' ] . setValue ( this . news . content ) ;
73
- this . userService . getOrganizations ( this . authenticationService . currentUserValue . id )
74
- . subscribe ( orgs => {
75
- orgs . forEach ( org => this . organizations . push ( Organization . fromModel ( org ) ) ) ;
76
- if ( this . news . organization . id > 0 ) {
77
- this . form . controls [ 'organization' ] . setValue ( this . organizations . findIndex ( org => org . id === this . news . organization . id ) ) ;
78
- } else {
79
- this . form . controls [ 'organization' ] . setValue ( 0 ) ;
80
- }
81
- } ) ;
82
- this . userService . getProjects ( this . authenticationService . currentUserValue . id )
83
- . subscribe ( prjs => {
84
- prjs . forEach ( prj => this . projects . push ( Project . fromModel ( prj ) ) ) ;
85
- if ( this . news . project . id > 0 ) {
86
- this . form . controls [ 'project' ] . setValue ( this . projects . findIndex ( prj => prj . id === this . news . project . id ) ) ;
87
- this . form . controls [ 'project' ] . disable ( ) ;
88
- } else {
89
- this . form . controls [ 'project' ] . setValue ( 0 ) ;
90
- }
91
- } ) ;
63
+ if ( this . idProject > 0 ) {
64
+ this . projectService . getById ( this . idProject )
65
+ . subscribe ( project => {
66
+ this . project = project ;
67
+ } )
68
+ }
92
69
}
93
70
94
71
onSubmit ( ) {
@@ -104,8 +81,8 @@ export class EditNewsComponent implements OnInit {
104
81
var submittedNews = new NewsModel ( ) ;
105
82
submittedNews . title = this . form . controls . title . value ;
106
83
submittedNews . content = this . form . controls . content . value ;
107
- submittedNews . organization . id = this . organizations [ this . form . controls . organization . value ] . id ;
108
- submittedNews . project . id = this . projects [ this . form . controls . project . value ] . id ;
84
+ submittedNews . organization . id = this . authenticationService . currentOrganizationValue . id ;
85
+ submittedNews . project . id = this . project . id ;
109
86
submittedNews . type = 'ARTICLE' ;
110
87
111
88
// Submit item to backend
0 commit comments