Skip to content

Commit

Permalink
feat(contact): send contact form to server
Browse files Browse the repository at this point in the history
  • Loading branch information
Penkie committed Feb 9, 2024
1 parent d63ada1 commit df75d91
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/app/common/models/contact.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface Contact {
collectionId: string;
collectionName: string;
created: Date;
email: string;
id: string;
message: string;
name: string;
updated: Date;
}
21 changes: 21 additions & 0 deletions src/app/common/services/contact.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment.development';
import { Contact } from '../models/contact.model';

@Injectable({
providedIn: 'root'
})
export class ContactService {

private API_URL = `${environment.API_URL}/api/collections/contact/records`;

constructor(
private http: HttpClient
) { }

public newContactForm(name: string, email: string, message: string): Observable<Contact> {
return this.http.post<Contact>(`${this.API_URL}`, { name, message, email });
}
}
10 changes: 10 additions & 0 deletions src/app/pages/contact/contact.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,15 @@
<div class="actions">
<button type="submit">Envoyer</button>
</div>
@if (sentSuccessfully) {
<div class="sent">
Votre message a bien été envoyé.
</div>
}
@if (errorWhileSending) {
<div class="error-sending">
Une erreur est survenue lors de l'envoi des données.
</div>
}
</form>
</div>
4 changes: 4 additions & 0 deletions src/app/pages/contact/contact.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
font-size: 12px;
}

.error-sending {
color: rgb(218, 31, 31);
}

.actions {
align-self: flex-end;

Expand Down
21 changes: 21 additions & 0 deletions src/app/pages/contact/contact.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { catchError } from 'rxjs';
import { ContactService } from 'src/app/common/services/contact.service';

@Component({
selector: 'app-contact',
Expand All @@ -9,6 +11,12 @@ import { FormControl, FormGroup, Validators } from '@angular/forms';
export class ContactComponent {

public submitted = false;
public sentSuccessfully = false;
public errorWhileSending = false;

constructor(
private contactService: ContactService
) {}

public contactForm = new FormGroup({
name: new FormControl('', Validators.required),
Expand All @@ -22,5 +30,18 @@ export class ContactComponent {
if (!this.contactForm.valid) {
return;
}

this.contactService.newContactForm(this.contactForm.controls.name.value!, this.contactForm.controls.email.value!, this.contactForm.controls.message.value!)
.subscribe({
next: (res) => {
if (res.id) {
this.sentSuccessfully = true;
this.contactForm.reset(this.contactForm.value);
}
},
error: (err) => {
this.errorWhileSending = true;
}
})
}
}
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>Oscarprince</title>
<title>Oscar Prince</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
Expand Down

0 comments on commit df75d91

Please sign in to comment.