Skip to content

Commit 2ccb1a0

Browse files
author
Laszlo Simon
committed
feat: contact form
1 parent 15605f7 commit 2ccb1a0

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NG_APP_CONTACT_FORM_ACCESS_KEY=your-web3forms-access-key

.github/workflows/deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ jobs:
3333

3434
- name: Build Angular app for GitHub Pages
3535
run: npm run build -- --configuration production
36+
env:
37+
NG_APP_CONTACT_FORM_ACCESS_KEY: ${{ secrets.WEB3FORMS_ACCESS_KEY }}
3638

3739
- name: Upload artifact
3840
uses: actions/upload-pages-artifact@v3

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/node_modules
1111
npm-debug.log
1212
yarn-error.log
13+
.env
1314

1415
# IDEs and editors
1516
.idea/

src/app/shared/components/contact-form/contact-form.component.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
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';
29
import { CommonModule } from '@angular/common';
310
import {
411
FormBuilder,
@@ -7,6 +14,7 @@ import {
714
Validators,
815
} from '@angular/forms';
916
import { TranslocoDirective } from '@jsverse/transloco';
17+
import { environment } from '../../../../environments/environment';
1018

1119
export interface IContactFormVM {
1220
readonly titleKey: string;
@@ -177,6 +185,7 @@ const minLengthValidator = (minimumLength: number): ValidatorFn => {
177185
export 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
}

src/env.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
interface ImportMetaEnv {
2+
readonly NG_APP_CONTACT_FORM_ACCESS_KEY?: string;
3+
readonly PROD: boolean;
4+
readonly DEV: boolean;
5+
}
6+
7+
interface ImportMeta {
8+
readonly env: ImportMetaEnv;
9+
}

src/environments/environment.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface Environment {
2+
readonly production: boolean;
3+
readonly contactFormAccessKey: string;
4+
}
5+
6+
export const environment: Environment = {
7+
production: import.meta.env.PROD,
8+
contactFormAccessKey: import.meta.env.NG_APP_CONTACT_FORM_ACCESS_KEY ?? '',
9+
};

0 commit comments

Comments
 (0)