Updated how emails are handled

This commit is contained in:
Zakary Timson 2022-05-03 19:37:01 -04:00
parent 7c398632f7
commit ec27c881b1
5 changed files with 61 additions and 47 deletions

View File

@ -1,3 +1,4 @@
import {HttpClientModule} from '@angular/common/http';
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {ContactFormComponent} from './components/contact-form/contact-form.component';
@ -25,38 +26,7 @@ import {ConsoleComponent} from './components/console/console.component';
BrowserModule,
BrowserAnimationsModule,
FormsModule,
MaterialModule,
],
bootstrap: [AppComponent]
})
export class AppModule { }
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {ContactFormComponent} from './components/contact-form/contact-form.component';
import {HomeComponent} from './views/home/home.component';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MaterialModule} from './material.module';
import {FormsModule} from '@angular/forms';
import {TypewriterComponent} from './components/typewriter/typewriter.component';
import {SlideShowComponent} from './components/slideShow/slideShow.component';
import {AppComponent} from './views/app/app.component';
import {AppRouting} from './app.routing';
import {ConsoleComponent} from './components/console/console.component';
@NgModule({
declarations: [
AppComponent,
ConsoleComponent,
ContactFormComponent,
HomeComponent,
SlideShowComponent,
TypewriterComponent
],
imports: [
AppRouting,
BrowserModule,
BrowserAnimationsModule,
FormsModule,
HttpClientModule,
MaterialModule,
],
bootstrap: [AppComponent]

View File

@ -1,4 +1,6 @@
<form>
<div *ngIf="success" class="alert alert-success">Email sent!</div>
<div *ngIf="error" class="alert alert-danger">Email failed to send</div>
<div class="form-group">
<label for="emailInput">Email</label>
<input type="email" class="form-control" id="emailInput" name="email" placeholder="username@example.com" [(ngModel)]="email">

View File

@ -1,5 +1,5 @@
import {Component} from '@angular/core';
import {formEncode} from '../../misc/utils';
import {ChangeDetectorRef, Component} from '@angular/core';
import {EmailService} from '../../services/email.service';
@Component({
selector: 'contact-form',
@ -7,26 +7,38 @@ import {formEncode} from '../../misc/utils';
})
export class ContactFormComponent {
email = '';
error = false;
loading = false;
message = '';
subject = '';
success = false;
constructor(private changeRef: ChangeDetectorRef, private emailService: EmailService) { }
async send() {
this.error = false;
this.success = false;
if(this.loading || !this.email || !this.subject || !this.message) return;
this.loading = true;
await fetch('https://postmail.invotes.com/send', {
method: 'POST',
mode: 'no-cors',
cache: 'no-cache',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: formEncode({
access_token: 's7uhce84sx6fayy5xlq0nrtx',
subject: `ZaksCode: ${this.subject}`,
text: `From: ${this.email}\n\n${this.message}`
})
this.emailService.send(`ZaksCode: ${this.subject}`, `From: ${this.email}\n\n${this.message}`
).then(() => {
this.email = '';
this.message = '';
this.success = true;
this.subject = '';
}).catch(err => {
// Postmail seems to always return an error message
if(200 <= err.status && err.status < 300) {
this.email = '';
this.message = '';
this.success = true;
this.subject = '';
} else {
this.error = true;
}
}).finally(() => {
this.loading = false;
this.changeRef.detectChanges();
});
this.loading = false;
}
}

View File

@ -0,0 +1,25 @@
import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {formEncode} from '../misc/utils';
@Injectable({providedIn: 'root'})
export class EmailService {
constructor(private http: HttpClient) { }
/**
* Send an email to website admin.
*
* @param {string} subject - Email subject line
* @param {string} message - Email body
* @returns {Promise<Object | undefined>} - Response from Postmail API
*/
send(subject: string, message: string) {
return this.http.post('https://postmail.invotes.com/send', formEncode({
access_token: 's7uhce84sx6fayy5xlq0nrtx',
subject: subject,
text: message
}), {
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).toPromise();
}
}

View File

@ -16,6 +16,11 @@ export class QuoteService {
'Either we are alone in the universe or we are not. both are terrifying'
];
/**
* Return random quote
*
* @returns {string} - Quote
*/
random() {
return this.quotes[Math.round(Math.random() * this.quotes.length)];
}