-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: master
Are you sure you want to change the base?
Socks shop #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
~$*.pptx | ||
node_modules | ||
.idea |
Large diffs are not rendered by default.
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; | ||
socks$!: Observable<Sock[]>; | ||
socks: Sock[] = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Definieer meerdere Observables:
|
||
|
||
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) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Omwille van de Na een page change ga je hier opnieuw de http call uitvoeren. |
||
} | ||
|
||
private getSocksPage(allSocks: Sock[], currentPage: number, pageSize: number) { | ||
const index = currentPage * pageSize | ||
return allSocks.slice(index, index + pageSize); | ||
} | ||
} |
There was a problem hiding this comment.
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
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:
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