As you can see we are assigning the values of the inputs to an object in our component class (contactFormValues) with the help of template forms, and we have a button being used to submit the form as well as we have (ng-container) and (ng-template) inside the button tag to display whether the text (send) or show a spinning animated (icon) that indicates the form is being submitted.
Let's take a look at the component class properties and methods:
Class properties:
Class methods:
Class properties explanation:
showAlert: boolean = false; To display alert message (success or fail).
alertMessage: string = ''; To hold the alert message.
onSubmit: boolean = false; To set and track submit state.
iconLoad = faArrowRotateForward; To define and rename the icon from (fontAwesome library).
To hold the inputs' values with the help of the template forms of Angular.
Class methods explanation:
hideAlert() To hide the alert message after 5 seconds.
submitEmail(contactForm) An async function that accepts an instance of the NgForm to handle the submit form and in this method let's explain 3 parts of it:
Create a formData instance and append values (contactFormValues) because it is the way that Web3Forms accepts the form.
formData.append('name', this.contactFormValues.name); add input value (name) from template to formData, follow the same steps to add the other inputs' values (email, body).
grab your access key from email you received earlier and add it into the environment variables, in our case we called it (form_access_key) see example below:
try | catch blocks, in the try block we are calling the mailService.sendEmail() method to submit the form and in case the form was successfully submitted we show a success message and reset the values to null by the help of the instance NgForm that has reset() method, in our case it is contactForm.reset(), and in case there is an error we added a guard clause and inside its block we throw an error to force the code to jump to the catch block and show an error message.
and at the end of the method we reset onSubmit property to false, showAlert to true and call the hideAlert() method.
Congratulations! You are all set up and ready to go.