diff --git a/itenium-socks/src/app/socks/sock.component.html b/itenium-socks/src/app/socks/sock.component.html index b40f9d2..be322c5 100644 --- a/itenium-socks/src/app/socks/sock.component.html +++ b/itenium-socks/src/app/socks/sock.component.html @@ -1,8 +1,8 @@ - diff --git a/itenium-socks/src/app/socks/sock.component.ts b/itenium-socks/src/app/socks/sock.component.ts index 1618e41..f98f84f 100644 --- a/itenium-socks/src/app/socks/sock.component.ts +++ b/itenium-socks/src/app/socks/sock.component.ts @@ -3,17 +3,27 @@ import { Observable } from 'rxjs'; import { Sock } from './sock.model'; import { SocksService } from './socks.service'; import { AsyncPipe, NgIf, TitleCasePipe } from '@angular/common'; +import {FormControl, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms"; @Component({ selector: 'app-sock', standalone: true, - imports: [NgIf, AsyncPipe, TitleCasePipe], - templateUrl: './sock.component.html' + imports: [NgIf, AsyncPipe, TitleCasePipe, ReactiveFormsModule], + templateUrl: './sock.component.html', }) export class SockComponent { sock$!: Observable; + reviewForm: FormGroup; + formError: boolean = false; + apiError: boolean = false; + apiSuccess: boolean = false; - constructor(private socksService: SocksService) {} + constructor(private socksService: SocksService) { + this.reviewForm = new FormGroup({ + 'review': new FormControl(null, Validators.required), + 'rating': new FormControl('1') + }); + } ngOnInit(): void { // HACK: This is not the way to get the sockId!! @@ -27,8 +37,26 @@ export class SockComponent { } addReview(): void { - // TODO: Bind the form! - const sockId = +window.location.pathname.split('/')[2]; - this.socksService.addReview(sockId, 'my review', 5).subscribe(); + this.resetNotifications(); + if (this.reviewForm.invalid) { + this.formError = true; + } else { + const sockId = +window.location.pathname.split('/')[2]; + this.socksService.addReview(sockId, this.reviewForm.get('review')?.value, this.reviewForm.get('rating')?.value).subscribe( + () => { + this.reviewForm.reset(); + this.reviewForm.get('rating')?.setValue('1'); + this.apiSuccess = true; + }, + (error) => { + this.apiError = true; + }); + } + } + + resetNotifications(){ + this.apiError = false; + this.apiSuccess = false; + this.formError = false; } }