Skip to content

Commit

Permalink
Fix contact form - prompt user to attach file to the form
Browse files Browse the repository at this point in the history
  • Loading branch information
augustinecyr committed Apr 22, 2023
1 parent ec38c1d commit a13a892
Showing 1 changed file with 56 additions and 42 deletions.
98 changes: 56 additions & 42 deletions frontend/src/app/components/contact.component.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ContactService } from '../contact.service';
import { Contact } from '../models';
import { MatSnackBar } from '@angular/material/snack-bar';


@Component({
selector: 'app-contact',
templateUrl: './contact.component.html',
styleUrls: ['./contact.component.css']
selector: 'app-contact',
templateUrl: './contact.component.html',
styleUrls: ['./contact.component.css'],
})
export class ContactComponent implements OnInit {
@ViewChild('attachment')
attachment!: ElementRef;

export class ContactComponent implements OnInit{

@ViewChild('attachment')
attachment!: ElementRef

form!: FormGroup
form!: FormGroup;

constructor(private fb: FormBuilder, private postSvc: ContactService, private snackBar: MatSnackBar) { }
constructor(
private fb: FormBuilder,
private postSvc: ContactService,
private snackBar: MatSnackBar
) {}

ngOnInit(): void {
this.form = this.createForm()
}
ngOnInit(): void {
this.form = this.createForm();
}

processForm() {
const contact = this.form.value as Contact
contact.attachment = this.attachment.nativeElement.files[0]
console.log('assembling the form.....')
console.info('form: ', contact)
processForm() {
if (!this.attachment.nativeElement.files[0]) {
this.snackBar.open(
'Please attach your screenshots for our reference!',
'X',
{
duration: 3000,
}
);
return;
}
const contact = this.form.value as Contact;
contact.attachment = this.attachment.nativeElement.files[0];
console.log('assembling the form.....');
console.info('form: ', contact);

this.postSvc.postContact(contact)
.then(response => {
this.form = this.createForm()
})
.catch(error => {
console.error('error: ', error)
})
console.info('form sent to backend')
this.snackBar.open('Thank you for contacting us, please check your email!', 'X', {
duration: 5000
});
}
this.postSvc
.postContact(contact)
.then((response) => {
this.form = this.createForm();
})
.catch((error) => {
console.error('error: ', error);
});
console.info('form sent to backend');
this.snackBar.open(
'Thank you for contacting us, please check your email!',
'X',
{
duration: 5000,
}
);
}

createForm(): FormGroup {
return this.fb.group({
email: this.fb.control('', [Validators.required, Validators.email]),
title: this.fb.control('', [Validators.required]),
text: this.fb.control('', [Validators.required, Validators.minLength(2)]),
attachment: this.fb.control('')
})
}
createForm(): FormGroup {
return this.fb.group({
email: this.fb.control('', [Validators.required, Validators.email]),
title: this.fb.control('', [Validators.required]),
text: this.fb.control('', [Validators.required, Validators.minLength(2)]),
attachment: this.fb.control(''),
});
}
}


1 comment on commit a13a892

@vercel
Copy link

@vercel vercel bot commented on a13a892 Apr 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.