11import { CommonModule } from '@angular/common' ;
2- import { Component } from '@angular/core' ;
2+ import { Component , OnInit } from '@angular/core' ;
33import { FormBuilder , FormGroup , ReactiveFormsModule , Validators } from '@angular/forms' ;
44import { MatDialog , MatDialogRef } from '@angular/material/dialog' ;
55import { ConfirmationDialogComponent } from 'src/app/components/confirmation-dialog/confirmation-dialog.component' ;
66import { MatSnackBar } from '@angular/material/snack-bar' ;
77import { Messages } from 'src/app/utils/messages' ;
8+ import { ProjectService } from 'src/app/services/project-services' ;
9+ import { CodeTableServices } from 'src/app/services/code-table-services' ;
10+ import { Project } from 'src/app/components/models' ;
811@Component ( {
912 selector : 'app-create-new-project-dialog' ,
1013 standalone : true ,
@@ -15,7 +18,7 @@ import { Messages } from 'src/app/utils/messages';
1518 templateUrl : './create-new-project-dialog.component.html' ,
1619 styleUrls : [ './create-new-project-dialog.component.scss' ]
1720} )
18- export class CreateNewProjectDialogComponent {
21+ export class CreateNewProjectDialogComponent implements OnInit {
1922 projectForm : FormGroup ;
2023 messages = Messages ;
2124 // Regions and sections mapping
@@ -27,9 +30,9 @@ export class CreateNewProjectDialogComponent {
2730 'West Coast' : [ 'Central Coast/North Island' , 'Haida Gwaii/South Island' ]
2831 } ;
2932
30- businessAreas = [ 'Area 1' , 'Area 2' , 'Area 3' ] ; // Example data
31- forestRegions = [ 'Region 1' , 'Region 2' , 'Region 3' ] ; // Example data
32- forestDistricts = [ 'District 1' , 'District 2' , 'District 3' ] ; // Example data
33+ businessAreas = [ 4 , 5 , 7 ] ; // Example data
34+ forestRegions = [ 1 , 3 , 5 ] ; // Example data
35+ forestDistricts = [ 4 , 66 , 4442 ] ; // Example data
3336 bcParksRegions = Object . keys ( this . regionToSections ) ;
3437 bcParksSections : string [ ] = [ ] ; // Dynamically updated based on the selected region
3538
@@ -38,6 +41,8 @@ export class CreateNewProjectDialogComponent {
3841 private readonly dialog : MatDialog ,
3942 private readonly dialogRef : MatDialogRef < CreateNewProjectDialogComponent > ,
4043 private readonly snackbarService : MatSnackBar ,
44+ private projectService : ProjectService ,
45+ private codeTableService : CodeTableServices
4146
4247 ) {
4348 this . projectForm = this . fb . group ( {
@@ -66,7 +71,65 @@ export class CreateNewProjectDialogComponent {
6671 }
6772 } ) ;
6873 }
74+ ngOnInit ( ) : void {
75+ // Fetch code tables
76+
77+ this . codeTableService . fetchCodeTable ( 'programAreaCodes' ) . subscribe ( {
78+ next : ( data ) => {
79+ this . forestDistricts = data ;
80+ } ,
81+ error : ( err ) => {
82+ console . error ( err ) ;
83+ this . snackbarService . open (
84+ 'Failed to load business area codes. Please try again later.' ,
85+ 'OK' ,
86+ { duration : 5000 , panelClass : 'snackbar-error' }
87+ ) ;
88+ } ,
89+ } ) ;
6990
91+ this . codeTableService . fetchCodeTable ( 'forestRegionCodes' ) . subscribe ( {
92+ next : ( data ) => {
93+ this . forestRegions = data ; // Assign data to the component property
94+ } ,
95+ error : ( err ) => {
96+ console . error ( err ) ;
97+ this . snackbarService . open (
98+ 'Failed to load forest region codes. Please try again later.' ,
99+ 'OK' ,
100+ { duration : 5000 , panelClass : 'snackbar-error' }
101+ ) ;
102+ } ,
103+ } ) ;
104+
105+ this . codeTableService . fetchCodeTable ( 'forestDistrictCodes' ) . subscribe ( {
106+ next : ( data ) => {
107+ this . forestDistricts = data ;
108+ } ,
109+ error : ( err ) => {
110+ console . error ( err ) ;
111+ this . snackbarService . open (
112+ 'Failed to load forest districts codes. Please try again later.' ,
113+ 'OK' ,
114+ { duration : 5000 , panelClass : 'snackbar-error' }
115+ ) ;
116+ } ,
117+ } ) ;
118+
119+ this . codeTableService . fetchCodeTable ( 'bcParksRegionCodes' ) . subscribe ( {
120+ next : ( data ) => {
121+ this . forestDistricts = data ;
122+ } ,
123+ error : ( err ) => {
124+ console . error ( err ) ;
125+ this . snackbarService . open (
126+ 'Failed to load bc parks region codes. Please try again later.' ,
127+ 'OK' ,
128+ { duration : 5000 , panelClass : 'snackbar-error' }
129+ ) ;
130+ } ,
131+ } ) ;
132+ }
70133 getErrorMessage ( controlName : string ) : string | null {
71134 const control = this . projectForm . get ( controlName ) ;
72135 if ( ! control || ! control . errors ) return null ;
@@ -87,24 +150,68 @@ export class CreateNewProjectDialogComponent {
87150 onCreate ( ) : void {
88151 if ( this . projectForm . valid ) {
89152 console . log ( this . projectForm . value ) ;
90- //call POST endpoint,
91- // if return 500 error with duplicate project name error message,
92-
93- // this.dialog.open(ConfirmationDialogComponent, {
94- // data: {
95- // indicator: 'duplicate-project',
96- // projectName: '',
97- // },
98- // width: '500px',
99- // });
100-
101- //OK will return the user to the Modal and allow further editing. just close the Modal for now
102- this . snackbarService . open (
103- this . messages . projectCreatedSuccess ,
104- 'OK' ,
105- { duration : 100000 , panelClass : 'snackbar-success' } ,
106- )
107- this . dialogRef . close ( this . projectForm . value ) ;
153+ const newProject : Project = {
154+ projectName : this . projectForm . get ( 'projectName' ) ?. value ?? '' ,
155+ programAreaGuid : '27602cd9-4b6e-9be0-e063-690a0a0afb50' , // change this to form value once codeTable returns value;
156+ forestRegionOrgUnitId : 11 , // change this to form value once codeTable returns value;
157+ forestDistrictOrgUnitId : 4442 , // change this to form value once codeTable returns value;
158+ bcParksRegionOrgUnitId : 0 , // change this to form value once codeTable returns value;
159+ bcParksSectionOrgUnitId : 10 , // change this to form value once codeTable returns value;
160+ projectLead : this . projectForm . get ( 'projectLead' ) ?. value ?? '' ,
161+ projectLeadEmailAddress : this . projectForm . get ( 'projectLeadEmail' ) ?. value ?? '' ,
162+ siteUnitName : "Vancouver Forest Unit" , // change this to form value once codeTable returns value;
163+ closestCommunityName : this . projectForm . get ( 'closestCommunity' ) ?. value ?? '' ,
164+ fireCentreOrgUnitId : this . projectForm . get ( 'fireCentre' ) ?. value ?? 0 ,
165+ forestAreaCode : {
166+ forestAreaCode : "WEST" // change this to form value once codeTable returns value;
167+ } ,
168+ generalScopeCode : {
169+ generalScopeCode : "SL_ACT" // change this to form value once codeTable returns value;
170+ } ,
171+ projectTypeCode : {
172+ projectTypeCode : "FUEL_MGMT" // change this to form value once codeTable returns value;
173+ } ,
174+ projectDescription : this . projectForm . get ( 'projectDescription' ) ?. value ?? '' ,
175+ projectNumber : this . projectForm . get ( 'projectNumber' ) ?. value ?? 0 ,
176+ totalFundingRequestAmount :
177+ this . projectForm . get ( 'totalFundingRequestAmount' ) ?. value ?? 0 ,
178+ totalAllocatedAmount : this . projectForm . get ( 'totalAllocatedAmount' ) ?. value ?? 0 ,
179+ totalPlannedProjectSizeHa :
180+ this . projectForm . get ( 'totalPlannedProjectSizeHa' ) ?. value ?? 0 ,
181+ totalPlannedCostPerHectare :
182+ this . projectForm . get ( 'totalPlannedCostPerHectare' ) ?. value ?? 0 ,
183+ totalActualAmount : this . projectForm . get ( 'totalActualAmount' ) ?. value ?? 0 ,
184+ isMultiFiscalYearProj : false ,
185+ } ;
186+
187+ this . projectService . createProject ( newProject ) . subscribe ( {
188+ next : ( response ) => {
189+ this . snackbarService . open (
190+ this . messages . projectCreatedSuccess ,
191+ 'OK' ,
192+ { duration : 100000 , panelClass : 'snackbar-success' } ,
193+ ) ;
194+ this . dialogRef . close ( this . projectForm . value ) ;
195+ } ,
196+ error : ( err ) => {
197+ if ( err . status === 500 && err . error . message . includes ( 'duplicate' ) ) {
198+ this . dialog . open ( ConfirmationDialogComponent , {
199+ data : {
200+ indicator : 'duplicate-project' ,
201+ projectName : '' ,
202+ } ,
203+ width : '500px' ,
204+ } ) ;
205+ }
206+ else {
207+ this . snackbarService . open (
208+ "Create project failed" ,
209+ 'OK' ,
210+ { duration : 5000 , panelClass : 'snackbar-error' }
211+ ) ;
212+ }
213+ }
214+ } )
108215 }
109216 }
110217
0 commit comments