1- import {
2- Component ,
3- EventEmitter ,
4- OnDestroy ,
5- OnInit ,
6- } from '@angular/core' ;
7- import { applicationLanguages , applicationLanguagesTranslated } from 'src/app/common/const' ;
8- import { PropertyCreateModel } from '../../../../models' ;
9- import { AuthStateService } from 'src/app/common/store' ;
10- import { BackendConfigurationPnPropertiesService } from '../../../../services' ;
11- import { MatDialogRef } from '@angular/material/dialog' ;
12- import { AutoUnsubscribe } from 'ngx-auto-unsubscribe' ;
13- import { Subscription } from 'rxjs' ;
1+ import { Component , EventEmitter , OnDestroy , OnInit } from '@angular/core' ;
2+ import { FormBuilder , FormGroup , Validators } from '@angular/forms' ;
3+ import { applicationLanguages , applicationLanguagesTranslated } from 'src/app/common/const' ;
4+ import { PropertyCreateModel } from '../../../../models' ;
5+ import { AuthStateService } from 'src/app/common/store' ;
6+ import { BackendConfigurationPnPropertiesService } from '../../../../services' ;
7+ import { MatDialogRef } from '@angular/material/dialog' ;
8+ import { AutoUnsubscribe } from 'ngx-auto-unsubscribe' ;
9+ import { Subscription } from 'rxjs' ;
1410
1511@AutoUnsubscribe ( )
1612@Component ( {
17- selector : 'app-property-create-modal' ,
18- templateUrl : './property-create-modal.component.html' ,
19- styleUrls : [ './property-create-modal.component.scss' ] ,
20- standalone : false
13+ selector : 'app-property-create-modal' ,
14+ templateUrl : './property-create-modal.component.html' ,
15+ styleUrls : [ './property-create-modal.component.scss' ] ,
16+ standalone : false
2117} )
2218export class PropertyCreateModalComponent implements OnInit , OnDestroy {
2319 propertyCreate : EventEmitter < PropertyCreateModel > = new EventEmitter < PropertyCreateModel > ( ) ;
20+ newPropertyForm : FormGroup ;
2421 newProperty : PropertyCreateModel = new PropertyCreateModel ( ) ;
2522 selectedLanguages : { id : number ; checked : boolean } [ ] = [ ] ;
2623 propertyIsFarm : boolean = false ;
@@ -33,11 +30,23 @@ export class PropertyCreateModalComponent implements OnInit, OnDestroy {
3330 }
3431
3532 constructor (
33+ private fb : FormBuilder ,
3634 public authStateService : AuthStateService ,
3735 private propertiesService : BackendConfigurationPnPropertiesService ,
38- public dialogRef : MatDialogRef < PropertyCreateModalComponent > ,
36+ public dialogRef : MatDialogRef < PropertyCreateModalComponent >
3937 ) {
4038 this . propertyIsFarm = false ;
39+
40+ // Initialize reactive form
41+ this . newPropertyForm = this . fb . group ( {
42+ cvr : [ '' , Validators . required ] ,
43+ mainMailAddress : [ '' , [ Validators . required , Validators . email ] ] ,
44+ name : [ '' , Validators . required ] ,
45+ chr : [ '' ] ,
46+ address : [ '' ] ,
47+ workorderEnable : [ false ] ,
48+ isFarm : [ false ]
49+ } ) ;
4150 }
4251
4352 ngOnDestroy ( ) : void {
@@ -48,18 +57,18 @@ export class PropertyCreateModalComponent implements OnInit, OnDestroy {
4857
4958 hide ( ) {
5059 this . dialogRef . close ( ) ;
51- this . newProperty = new PropertyCreateModel ( ) ;
60+ this . newPropertyForm . reset ( ) ;
5261 this . selectedLanguages = [ ] ;
62+ this . propertyIsFarm = false ;
5363 }
5464
5565 onCreateProperty ( ) {
56- this . propertyCreate . emit ( {
57- ...this . newProperty ,
58- languagesIds : applicationLanguagesTranslated . map ( ( x ) => x . id ) ,
59- } ) ;
66+ const newProperty : PropertyCreateModel = {
67+ ...this . newPropertyForm . value ,
68+ languagesIds : applicationLanguagesTranslated . map ( x => x . id )
69+ } ;
70+ this . propertyCreate . emit ( newProperty ) ;
6071 }
61-
62-
6372 onNameFilterChanged ( number : string ) {
6473 this . newProperty . cvr = number ;
6574 // if (+number === 0) {
@@ -106,46 +115,20 @@ export class PropertyCreateModalComponent implements OnInit, OnDestroy {
106115 }
107116
108117 onChrNumberChanged ( number : number ) {
109- if ( number > 11111 ) {
110- if ( number . toString ( ) . length > 5 ) {
111- this . getChrInformationSub$ = this . propertiesService . getChrInformation ( number )
112- . subscribe ( ( data ) => {
113- if ( data && data . success ) {
114- if ( data . model . ejendom . byNavn === '' || data . model . ejendom . byNavn === null ) {
115- this . newProperty . address = data . model . ejendom . adresse + ', ' + data . model . ejendom . postDistrikt ;
116- } else {
117- this . newProperty . address = data . model . ejendom . adresse + ', ' + data . model . ejendom . byNavn ;
118- }
119- }
120- } ) ;
121- }
118+ if ( number > 11111 && number . toString ( ) . length > 5 ) {
119+ this . getChrInformationSub$ = this . propertiesService . getChrInformation ( number )
120+ . subscribe ( ( data ) => {
121+ if ( data ?. success ) {
122+ const address = data . model . ejendom . byNavn || '' ;
123+ this . newPropertyForm . patchValue ( {
124+ address : `${ data . model . ejendom . adresse } , ${ address || data . model . ejendom . postDistrikt } `
125+ } ) ;
126+ }
127+ } ) ;
122128 }
123129 }
124130
125- // addToArray(e: any, languageId: number) {
126- // if (e.target.checked) {
127- // this.selectedLanguages = [
128- // ...this.selectedLanguages,
129- // { id: languageId, checked: true },
130- // ];
131- // } else {
132- // this.selectedLanguages = this.selectedLanguages.filter(
133- // (x) => x.id !== languageId
134- // );
135- // }
136- // }
137-
138- getLanguageIsChecked ( languageId : number ) : boolean {
139- const language = this . selectedLanguages . find ( ( x ) => x . id === languageId ) ;
140- return language ? language . checked : false ;
141- }
142-
143131 get isDisabledSaveButton ( ) : boolean {
144- if ( this . newProperty /*&& this.newProperty.languagesIds*/ ) {
145- return (
146- ! this . newProperty . name /* || !this.selectedLanguages.some((x) => x.checked)*/
147- ) ;
148- }
149- return false ;
132+ return this . newPropertyForm . invalid ;
150133 }
151134}
0 commit comments