Skip to content

Conversation

@timbo-92
Copy link

No description provided.

@timbo-92 timbo-92 changed the title Timbo exercise socks shop Socks shop May 31, 2024
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

pageSize = 8;
currentPage = 0;
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(...));

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants