Skip to content

Socks shop #14

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
~$*.pptx
node_modules
.idea
5 changes: 5 additions & 0 deletions itenium-socks/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
}
],
"styles": [
"@angular/material/prebuilt-themes/rose-red.css",
"node_modules/@fortawesome/fontawesome-free/css/all.min.css",
"src/styles.scss",
"public/bootstrap.css"
Expand Down Expand Up @@ -93,12 +94,16 @@
}
],
"styles": [
"@angular/material/prebuilt-themes/rose-red.css",
"src/styles.scss"
],
"scripts": []
}
}
}
}
},
"cli": {
"analytics": false
}
}
844 changes: 842 additions & 2 deletions itenium-socks/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion itenium-socks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
"private": true,
"dependencies": {
"@angular/animations": "^18.0.0",
"@angular/cdk": "^18.0.1",
"@angular/common": "^18.0.0",
"@angular/compiler": "^18.0.0",
"@angular/core": "^18.0.0",
"@angular/forms": "^18.0.0",
"@angular/material": "^18.0.1",
"@angular/platform-browser": "^18.0.0",
"@angular/platform-browser-dynamic": "^18.0.0",
"@angular/router": "^18.0.0",
Expand All @@ -37,4 +39,4 @@
"karma-jasmine-html-reporter": "~2.1.0",
"typescript": "~5.4.2"
}
}
}
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 { provideAnimationsAsync } from '@angular/platform-browser/animations/async';

export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideHttpClient(withFetch()),
provideAnimationsAsync(),
]
};
3 changes: 2 additions & 1 deletion itenium-socks/src/app/socks/shop.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ <h2>
</h2>
</div>
<div class="row">
<div class="col-sm-6 col-md-4 col-lg-3" *ngFor="let sock of socks$ | async">
<div class="col-sm-6 col-md-4 col-lg-3" *ngFor="let sock of socks">
<div class="box">
<a href="/socks/{{ sock.id }}">
<div class="img-box">
Expand All @@ -22,6 +22,7 @@ <h6><span>{{ sock.price }}</span></h6>
</a>
</div>
</div>
<mat-paginator [length]="totalItems" [pageSize]="pageSize" [pageIndex]="currentPage" (page)="pageChanged($event)"></mat-paginator>
</div>
</div>
</section>
31 changes: 27 additions & 4 deletions itenium-socks/src/app/socks/shop.component.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
import { Component } from '@angular/core';
import {Component, OnInit} from '@angular/core';
import { SocksService } from './socks.service';
import { Observable } from 'rxjs';
import {last, Observable} from 'rxjs';
import { Sock } from './sock.model';
import { AsyncPipe, NgFor } from '@angular/common';
import {MatPaginator, PageEvent} from "@angular/material/paginator";

@Component({
selector: 'app-shop',
standalone: true,
imports: [NgFor, AsyncPipe],
imports: [NgFor, AsyncPipe, MatPaginator],
templateUrl: './shop.component.html'
})
export class ShopComponent {
export class ShopComponent implements OnInit{
totalItems = 100;
pageSize = 8;
currentPage = 0;
Copy link
Member

Choose a reason for hiding this comment

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

In een prod applicatie zou ik hier ook een apart component van maken

class Paginator {
  @Output() pageChanged = new EventEmitter<PaginationInfo>();
}

Je wil paginatie toevoegen aan de SocksShop, maar later ook op de ReviewsPage en op de UsersPage en op de ...
Maar je wil de paginatie code niet elke keer gaan herhalen!

vb:

class PagedItemComponent {}

Dit is al iets ingewikkelder, je zal vb met een slot moeten werken: <ng-content></ng-content>

Grote voordeel:
Je ShopComponent begaat geen SRP violations: nu gaat de ShopComponent paginatie code bevatten, en later ook Filter code, en sorteer code etc...

En je kan het hergebruiken voor alles waardoor je bugfixes (off by 1 errors?) en changes (de paginatie buttons moeten anders gepositioneed & gestyled worden) maar op 1 plaats moet doen

socks$!: Observable<Sock[]>;
socks: Sock[] = [];
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.

Avoid fields in components.

Het is hier ook niet meer super duidelijk dat socks enkel een bepaalde pagina van socks bevat.

Definieer meerdere Observables:

socks$!: Observable<Sock[]>;
totalCount$ = this.socks$.pipe(map(socks => socks.length))
pagedSocks = combineLatest([this.socks$, this.pagination$]).pipe(map(([socks, pagination]) => socks.slice(...));


constructor(private socksService: SocksService) {}

ngOnInit(): void {
this.socks$ = this.socksService.get();
this.socks$.pipe(last())
.subscribe(allSocks => {
this.socks = this.getSocksPage(allSocks, this.currentPage, this.pageSize)
this.totalItems = allSocks.length
})
}

pageChanged(event: PageEvent) {
this.currentPage = event.pageIndex;
this.socks$.pipe(last())
.subscribe(allSocks => {
this.socks = this.getSocksPage(allSocks, this.currentPage, this.pageSize)
})
Copy link
Member

Choose a reason for hiding this comment

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

Omwille van de .subscribe introduceer je namelijk subtiele fouten

Na een page change ga je hier opnieuw de http call uitvoeren.
Als er sindsdien een sock toegevoegd is, dan is de totalCount daarna incorrect!

}

private getSocksPage(allSocks: Sock[], currentPage: number, pageSize: number) {
const index = currentPage * pageSize
return allSocks.slice(index, index + pageSize);
}
}
2 changes: 2 additions & 0 deletions itenium-socks/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.png">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css" />
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<app-root></app-root>
Expand Down
3 changes: 3 additions & 0 deletions itenium-socks/src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -943,3 +943,6 @@ a:focus {
max-width: 1170px;
}
}

html, body { height: 100%; }
body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }