Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions itenium-socks/src/app/socks/sock.component.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div class="alert alert-success container" role="alert" style="display: none">
<div *ngIf="this.apiSuccess" class="alert alert-success container" role="alert">
Thank you for your review! Be sure to shop with us again soon!
</div>

<div class="alert alert-danger container" role="alert" style="display: none">
<div *ngIf="this.apiError" class="alert alert-danger container" role="alert">
Failed to post the review: <span></span>
</div>

Expand Down Expand Up @@ -68,25 +68,29 @@ <h3 style="text-align: center">
</div>
<div class="col-md-6 col-lg-5 px-0">
<div class="review-form" style="padding-bottom: 0;">
<div>
<input type="text" class="message-box" placeholder="Your review..." />
</div>
<div>
<label>Your rating:</label>
<select style="margin-left: 14px">
<option disabled="" value="">select</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option selected>5</option>
</select>
</div>
<div class="d-flex">
<button type="button" (click)="addReview()">
SEND
</button>
</div>
<form [formGroup]="reviewForm" (ngSubmit)="addReview()">
<div>
@if (this.formError){
<p style="color:red">Please enter a review.</p>
}
<input type="text" class="message-box" placeholder="Your review..." formControlName="review" />
</div>
<div>
<label>Your rating:</label>
<select style="margin-left: 14px" formControlName="rating">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<div class="d-flex">
<button type="submit">
SEND
</button>
</div>
</form>
</div>
</div>
</div>
Expand Down
40 changes: 34 additions & 6 deletions itenium-socks/src/app/socks/sock.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sock>;
reviewForm: FormGroup;
formError: boolean = false;
Copy link
Member

@Laoujin Laoujin May 31, 2024

Choose a reason for hiding this comment

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

Je hebt ook een: this.reviewForm.valid

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')
});
Copy link
Member

Choose a reason for hiding this comment

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

Tegenwoordig kan je dit ook allemaal strongly typed maken (wat vb handig is bij .valueChanges etc

rating: new FormControl<number>(1)

}

ngOnInit(): void {
// HACK: This is not the way to get the sockId!!
Expand All @@ -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;
}
}