Skip to content

Exercise buying socks #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions itenium-socks/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions itenium-socks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@angular/router": "^18.0.0",
"@fortawesome/angular-fontawesome": "^0.15.0",
"@fortawesome/fontawesome-free": "^6.5.2",
"ng-angular-popup": "^0.6.1",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
Expand Down
1 change: 1 addition & 0 deletions itenium-socks/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<router-outlet />
<ng-toast [position]="ToasterPosition.TOP_CENTER"></ng-toast>
6 changes: 5 additions & 1 deletion itenium-socks/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import {NgToastModule, ToasterPosition} from 'ng-angular-popup';

@Component({
selector: 'app-root',
standalone: true,
imports: [
NgToastModule,
RouterOutlet,
],
templateUrl: './app.component.html',
})
export class AppComponent {}
export class AppComponent {
ToasterPosition = ToasterPosition;
}
2 changes: 2 additions & 0 deletions itenium-socks/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { provideRouter } from '@angular/router';
import { provideHttpClient, withFetch } from '@angular/common/http';

import { routes } from './app.routes';
import { provideAnimations } from '@angular/platform-browser/animations';

export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideHttpClient(withFetch()),
provideAnimations()
]
};
7 changes: 7 additions & 0 deletions itenium-socks/src/app/socks/shop.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.soldOutItem {
position: absolute;
top: 0;
right: 0;
background-color: red;
padding: 0.5em;
}
3 changes: 3 additions & 0 deletions itenium-socks/src/app/socks/shop.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ <h2>
<div class="detail-box">
<h6>{{ sock.name }}</h6>
<h6><span>{{ sock.price }}</span></h6>
@if (sock.inventory == 0) {
<h6 class="soldOutItem">SOLD OUT</h6>
}
</div>
<div class="new">
<span class="favourite" [style.color]="sock.color"><i class="fa fa-star"></i></span>
Expand Down
3 changes: 2 additions & 1 deletion itenium-socks/src/app/socks/shop.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { AsyncPipe, NgFor } from '@angular/common';
selector: 'app-shop',
standalone: true,
imports: [NgFor, AsyncPipe],
templateUrl: './shop.component.html'
templateUrl: './shop.component.html',
styleUrls: ['./shop.component.css'],
})
export class ShopComponent {
socks$!: Observable<Sock[]>;
Expand Down
6 changes: 6 additions & 0 deletions itenium-socks/src/app/socks/sock.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.soldOutBadge {
background-color: red;
color: white;
padding: 0.2em;
margin: 0.2em;
}
8 changes: 7 additions & 1 deletion itenium-socks/src/app/socks/sock.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ <h6><span>€</span><span>{{ sock.price }}</span></h6>
</div>
<div class="col-sm-6 col-md-6 col-lg-6">
<h1 style="margin-top: 25px;">{{ sock.name }}</h1>
@if (sock.inventory == 0) {
<span class="soldOutBadge">SOLD OUT!! 😭</span>
}
@else {
{{ sock.inventory }} in stock. FREE Shipping!
}

<br>
Color: {{ sock.variant | titlecase }}

<br>
<br>

<button type="button" class="buy-socks" (click)="buy()">
<button id="buyBtn" *ngIf="sock.inventory > 0" type="button" class="buy-socks" (click)="buy()">
Copy link
Member

Choose a reason for hiding this comment

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

Avoid <HTMLInputElement> document.getElementById("buyBtn"); --> it's mixing together pure JavaScript & Angular.

In this case you could use:

 <button [disabled]="buttonDisabled" *ngIf="sock.inventory > 0" type="button" class="buy-socks" (click)="buy()">

If you really need to manipulate a DOM object manually (this should be rare)

constructor(elementRef: ElementRef) {}

See: https://angular.dev/guide/components/dom-apis#

BUY NOW
</button>
</div>
Expand Down
25 changes: 22 additions & 3 deletions itenium-socks/src/app/socks/sock.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,46 @@ import { Observable } from 'rxjs';
import { Sock } from './sock.model';
import { SocksService } from './socks.service';
import { AsyncPipe, NgIf, TitleCasePipe } from '@angular/common';
import { NgToastService } from 'ng-angular-popup';

@Component({
selector: 'app-sock',
standalone: true,
imports: [NgIf, AsyncPipe, TitleCasePipe],
templateUrl: './sock.component.html'
templateUrl: './sock.component.html',
styleUrl: './sock.component.css'
})
export class SockComponent {
sock$!: Observable<Sock>;

constructor(private socksService: SocksService) {}
constructor(private socksService: SocksService,
private ngToastService: NgToastService
) {}

ngOnInit(): void {
// HACK: This is not the way to get the sockId!!
const sockId = +window.location.pathname.split('/')[2];
this.sock$ = this.socksService.getById(sockId);
}


buy(): void {
const sockId = +window.location.pathname.split('/')[2];
this.socksService.buySocks(sockId).subscribe();
const btn = <HTMLInputElement> document.getElementById("buyBtn");
btn.disabled = true;
let observer = {
next: (value:any) => {
btn.disabled = false;
console.log("Bought!");
this.ngToastService.success("Bought a pair of socks!", "Congrats!", 5000);
},
error: (err:any) => {
btn.disabled = false;
console.log("Uh oh!");
this.ngToastService.danger("Whoops, something went wrong...", "Uh oh!", 5000);
},
};
this.socksService.buySocks(sockId).subscribe( observer );
}

addReview(): void {
Expand Down
6 changes: 3 additions & 3 deletions socks-backend/db/socks.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"variant": "blue",
"color": "#23A3AC",
"price": 20,
"inventory": 3
"inventory": 1
},
{
"id": 3,
Expand All @@ -25,7 +25,7 @@
"variant": "blue",
"color": "#27384F",
"price": 30,
"inventory": 9
"inventory": 6
},
{
"id": 4,
Expand Down Expand Up @@ -79,7 +79,7 @@
"variant": "Saffron",
"color": "#f6c93a",
"price": 12.5,
"inventory": 9
"inventory": 6
},
{
"id": 10,
Expand Down