@@ -5,159 +5,125 @@ import {
55 OnDestroy ,
66 OnInit ,
77} from '@angular/core' ;
8- import { applicationLanguages } from 'src/app/common/const' ;
9- import { PropertyModel , PropertyUpdateModel } from '../../../../models' ;
10- import { AuthStateService } from 'src/app/common/store' ;
11- import { BackendConfigurationPnPropertiesService } from '../../../../services' ;
12- import { MAT_DIALOG_DATA , MatDialogRef } from '@angular/material/dialog' ;
13- import { Subscription } from 'rxjs' ;
14- import { AutoUnsubscribe } from 'ngx-auto-unsubscribe' ;
15- import { selectAuthIsAuth } from 'src/app/state/auth/auth.selector' ;
16- import { Store } from '@ngrx/store' ;
8+ import { FormBuilder , FormGroup , Validators } from '@angular/forms' ;
9+ import { applicationLanguages } from 'src/app/common/const' ;
10+ import { PropertyModel , PropertyUpdateModel } from '../../../../models' ;
11+ import { AuthStateService } from 'src/app/common/store' ;
12+ import { BackendConfigurationPnPropertiesService } from '../../../../services' ;
13+ import { MAT_DIALOG_DATA , MatDialogRef } from '@angular/material/dialog' ;
14+ import { Subscription } from 'rxjs' ;
15+ import { AutoUnsubscribe } from 'ngx-auto-unsubscribe' ;
16+ import { Store } from '@ngrx/store' ;
17+ import { selectAuthIsAuth } from 'src/app/state/auth/auth.selector' ;
1718
1819@AutoUnsubscribe ( )
1920@Component ( {
20- selector : 'app-property-edit-modal' ,
21- templateUrl : './property-edit-modal.component.html' ,
22- styleUrls : [ './property-edit-modal.component.scss' ] ,
23- standalone : false
21+ selector : 'app-property-edit-modal' ,
22+ templateUrl : './property-edit-modal.component.html' ,
23+ styleUrls : [ './property-edit-modal.component.scss' ] ,
24+ standalone : false ,
2425} )
2526export class PropertyEditModalComponent implements OnInit , OnDestroy {
2627 propertyUpdate : EventEmitter < PropertyUpdateModel > = new EventEmitter < PropertyUpdateModel > ( ) ;
27- selectedProperty : PropertyUpdateModel = new PropertyUpdateModel ( ) ;
28+ editPropertyForm : FormGroup ;
29+ propertyIsFarm = false ;
2830 selectedLanguages : { id : number ; checked : boolean } [ ] = [ ] ;
29- propertyIsFarm : boolean = false ;
3031
3132 getChrInformationSub$ : Subscription ;
3233 getCompanyTypeSub$ : Subscription ;
33- public isAuth$ = this . store . select ( selectAuthIsAuth ) ;
3434 public selectAuthIsAdmin$ = this . store . select ( selectAuthIsAuth ) ;
3535
36- get applicationLanguages ( ) {
37- return applicationLanguages ;
38- }
39-
4036 constructor (
37+ private fb : FormBuilder ,
4138 private store : Store ,
4239 public authStateService : AuthStateService ,
4340 private propertiesService : BackendConfigurationPnPropertiesService ,
4441 public dialogRef : MatDialogRef < PropertyEditModalComponent > ,
45- @Inject ( MAT_DIALOG_DATA ) model : PropertyModel = new PropertyModel ( )
42+ @Inject ( MAT_DIALOG_DATA ) public model : PropertyModel
4643 ) {
47- this . selectedLanguages = model . languages . map ( ( x ) => {
48- return { id : x . id , checked : true } ;
44+ this . selectedLanguages = model . languages . map ( ( x ) => ( { id : x . id , checked : true } ) ) ;
45+
46+ this . editPropertyForm = this . fb . group ( {
47+ cvr : [ model . cvr , Validators . required ] ,
48+ mainMailAddress : [ model . mainMailAddress , [ Validators . required , Validators . email ] ] ,
49+ name : [ model . name , Validators . required ] ,
50+ chr : [ model . chr ] ,
51+ address : [ model . address ] ,
52+ workorderEnable : [ model . workorderEnable || false ] ,
53+ isFarm : [ model . isFarm || false ] ,
54+ industryCode : [ model . industryCode || '' ]
4955 } ) ;
50- this . selectedProperty = {
51- ...model ,
52- languagesIds : [ ] ,
53- } ;
54- }
5556
56- ngOnInit ( ) {
57+ this . propertyIsFarm = model . isFarm || false ;
5758 }
5859
60+ ngOnInit ( ) { }
61+
5962 hide ( ) {
6063 this . dialogRef . close ( ) ;
61- this . selectedProperty = new PropertyUpdateModel ( ) ;
64+ this . editPropertyForm . reset ( ) ;
6265 this . selectedLanguages = [ ] ;
66+ this . propertyIsFarm = false ;
6367 }
6468
6569 onUpdateProperty ( ) {
66- this . propertyUpdate . emit ( {
67- ...this . selectedProperty ,
70+ const updatedProperty : PropertyUpdateModel = {
71+ ...this . model ,
72+ ...this . editPropertyForm . value ,
6873 languagesIds : this . selectedLanguages . map ( ( x ) => x . id ) ,
69- } ) ;
74+ } ;
75+ this . propertyUpdate . emit ( updatedProperty ) ;
7076 }
7177
72- onNameFilterChanged ( number : number ) {
73- if ( number === 0 ) {
78+ onNameFilterChanged ( number : string ) {
79+ this . editPropertyForm . patchValue ( { cvr : number } ) ;
80+
81+ if ( + number === 0 ) {
7482 this . propertyIsFarm = false ;
83+ this . editPropertyForm . patchValue ( { isFarm : false } ) ;
7584 }
76- if ( number === 1111111 ) {
85+
86+ if ( + number === 1111111 ) {
7787 this . propertyIsFarm = true ;
78- this . selectedProperty . isFarm = true ;
88+ this . editPropertyForm . patchValue ( { isFarm : true } ) ;
7989 }
80- if ( number > 1111111 ) {
81- if ( number . toString ( ) . length > 7 ) {
82- this . getCompanyTypeSub$ = this . propertiesService . getCompanyType ( number )
83- . subscribe ( ( data ) => {
84- if ( data && data . success ) {
85- if ( data . model . industrycode . toString ( ) . slice ( 0 , 2 ) === '01' ) {
86- this . propertyIsFarm = true ;
87- this . selectedProperty . isFarm = true ;
88- if ( data . model . error !== 'NOT_FOUND' ) {
89- this . selectedProperty . address = data . model . address + ', ' + data . model . city ;
90- this . selectedProperty . name = data . model . name ;
91- this . selectedProperty . industryCode = data . model . industrycode ;
92- }
93- } else {
94- if ( data . model . error === 'REQUIRES_PAID_SUBSCRIPTION' ) {
95- this . propertyIsFarm = true ;
96- this . selectedProperty . isFarm = true ;
97- } else {
98- this . propertyIsFarm = false ;
99- this . selectedProperty . isFarm = false ;
100- if ( data . model . error !== 'NOT_FOUND' ) {
101- this . selectedProperty . address = data . model . address + ', ' + data . model . city ;
102- this . selectedProperty . name = data . model . name ;
103- this . selectedProperty . industryCode = data . model . industrycode ;
104- }
105- }
106- }
107- }
108- } ) ;
109- }
110- } else {
111- // this.selectedProperty.name = '';
112- // this.selectedProperty.address = '';
113- }
114- }
11590
116- // addToArray(e: any, languageId: number) {
117- // if (e.target.checked) {
118- // this.selectedLanguages = [
119- // ...this.selectedLanguages,
120- // { id: languageId, checked: true },
121- // ];
122- // } else {
123- // this.selectedLanguages = this.selectedLanguages.filter(
124- // (x) => x.id !== languageId
125- // );
126- // }
127- // }
128-
129- getLanguageIsChecked ( languageId : number ) : boolean {
130- const language = this . selectedLanguages . find ( ( x ) => x . id === languageId ) ;
131- return language ? language . checked : false ;
132- }
91+ if ( + number > 1111111 && number . toString ( ) . length > 7 ) {
92+ this . getCompanyTypeSub$ = this . propertiesService . getCompanyType ( + number ) . subscribe ( ( data ) => {
93+ if ( data ?. success ) {
94+ const industryPrefix = data . model . industrycode . toString ( ) . slice ( 0 , 2 ) ;
95+ const isFarm = industryPrefix === '01' || data . model . error === 'REQUIRES_PAID_SUBSCRIPTION' ;
13396
134- get isDisabledSaveButton ( ) : boolean {
135- if ( this . selectedProperty /* && this.selectedProperty.languagesIds*/ ) {
136- return (
137- ! this . selectedProperty . name /* ||
138- !this.selectedLanguages.some((x) => x.checked)*/
139- ) ;
97+ this . propertyIsFarm = isFarm ;
98+ this . editPropertyForm . patchValue ( {
99+ isFarm,
100+ name : data . model . name || '' ,
101+ address : data . model . address ? `${ data . model . address } , ${ data . model . city } ` : '' ,
102+ industryCode : data . model . industrycode || '' ,
103+ } ) ;
104+ }
105+ } ) ;
106+ } else {
107+ this . editPropertyForm . patchValue ( { name : '' , address : '' } ) ;
140108 }
141- return false ;
142109 }
143110
144111 onChrNumberChanged ( number : number ) {
145- if ( number > 11111 ) {
146- if ( number . toString ( ) . length > 5 ) {
147- this . getChrInformationSub$ = this . propertiesService . getChrInformation ( number )
148- . subscribe ( ( data ) => {
149- if ( data && data . success ) {
150- if ( data . model . ejendom . byNavn === '' || data . model . ejendom . byNavn === null ) {
151- this . selectedProperty . address = data . model . ejendom . adresse + ', ' + data . model . ejendom . postDistrikt ;
152- } else {
153- this . selectedProperty . address = data . model . ejendom . adresse + ', ' + data . model . ejendom . byNavn ;
154- }
155- }
112+ if ( number > 11111 && number . toString ( ) . length > 5 ) {
113+ this . getChrInformationSub$ = this . propertiesService . getChrInformation ( number ) . subscribe ( ( data ) => {
114+ if ( data ?. success ) {
115+ const address = data . model . ejendom . byNavn || data . model . ejendom . postDistrikt ;
116+ this . editPropertyForm . patchValue ( {
117+ address : `${ data . model . ejendom . adresse } , ${ address } ` ,
156118 } ) ;
157- }
119+ }
120+ } ) ;
158121 }
159122 }
160123
161- ngOnDestroy ( ) : void {
124+ get isDisabledSaveButton ( ) : boolean {
125+ return this . editPropertyForm . invalid ;
162126 }
127+
128+ ngOnDestroy ( ) : void { }
163129}
0 commit comments