1- import { Component , input , output , inject } from '@angular/core' ;
1+ import {
2+ Component ,
3+ input ,
4+ output ,
5+ inject ,
6+ HostListener ,
7+ signal ,
8+ } from '@angular/core' ;
29import { CommonModule } from '@angular/common' ;
310import {
411 FormBuilder ,
@@ -7,6 +14,7 @@ import {
714 Validators ,
815} from '@angular/forms' ;
916import { TranslocoDirective } from '@jsverse/transloco' ;
17+ import { environment } from '../../../../environments/environment' ;
1018
1119export interface IContactFormVM {
1220 readonly titleKey : string ;
@@ -177,6 +185,7 @@ const minLengthValidator = (minimumLength: number): ValidatorFn => {
177185export class ContactFormComponent {
178186 vm = input . required < IContactFormVM > ( ) ;
179187 formSubmit = output < IContactFormData > ( ) ;
188+ protected readonly isSubmitting = signal ( false ) ;
180189
181190 private readonly formBuilder = inject ( FormBuilder ) ;
182191
@@ -207,4 +216,44 @@ export class ContactFormComponent {
207216 this . formSubmit . emit ( formValue ) ;
208217 }
209218 } ;
219+
220+ @HostListener ( 'formSubmit' , [ '$event' ] )
221+ protected async onSubmitEvent ( formValue : {
222+ name : string ;
223+ email : string ;
224+ phone : string ;
225+ message : string ;
226+ } ) : Promise < void > {
227+ console . log ( 'Contact Form Submitted:' , formValue ) ;
228+ this . isSubmitting . set ( true ) ;
229+ const accessKey : string = environment . contactFormAccessKey ;
230+ try {
231+ if ( ! accessKey ) {
232+ console . warn (
233+ 'Contact form access key is not configured. Submission aborted.'
234+ ) ;
235+ return ;
236+ }
237+
238+ const response = await fetch ( 'https://api.web3forms.com/submit' , {
239+ method : 'POST' ,
240+ headers : {
241+ 'Content-Type' : 'application/json' ,
242+ Accept : 'application/json' ,
243+ } ,
244+ body : JSON . stringify ( {
245+ access_key : accessKey ,
246+ ...formValue ,
247+ } ) ,
248+ } ) ;
249+ const result = ( await response . json ( ) ) as { success : boolean } ;
250+ if ( result . success ) {
251+ this . contactForm . reset ( ) ;
252+ } else {
253+ console . error ( 'Failed to submit contact form' ) ;
254+ }
255+ } finally {
256+ this . isSubmitting . set ( true ) ;
257+ }
258+ }
210259}
0 commit comments