Skip to content

Commit

Permalink
Use the new control work flow syntax for loops and if else in the tem…
Browse files Browse the repository at this point in the history
…plate
  • Loading branch information
RonanCodes committed Apr 30, 2024
1 parent 0db5b2c commit 37137a5
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ describe('FavouritesStoreService', () => {
service.addFavourite(newJoke);

service.favourites$.subscribe((favourites) => {
expect(favourites[1].value).toBe(newJoke.value);
expect(favourites[0].value).toBe(newJoke.value);
done();
});

// Assert
expect(newJoke.isFavourite).toBe(true);
expect(localStorageService.saveToLocalStorage).toHaveBeenCalledWith(
localStorageKey.favourites,
JSON.stringify([originalJoke, newJoke])
JSON.stringify([newJoke, originalJoke])
);
});
});
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('FavouritesStoreService', () => {
expect(originalJoke.isFavourite).toBe(true);
expect(localStorageService.saveToLocalStorage).toHaveBeenCalledWith(
localStorageKey.favourites,
JSON.stringify([originalJoke, newJoke])
JSON.stringify([newJoke, originalJoke])
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export class FavouritesStoreService {
public addFavourite(joke: Joke): void {
joke.isFavourite = true;
// new jokes are added to the top of the list:
this._favourites$.next([...this._favourites$.value, joke]);
this._favourites$.next([joke, ...this._favourites$.value.splice(0, 9)]);

this.persistToLocalStorage();
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/shared/data/store/joke-store/joke-store.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { ChuckNorrisJokeGeneratorService } from '../../rest/chuck-norris-joke-generator/chuck-norris-joke-generator.service';
import { BehaviorSubject, Observable, concat, tap } from 'rxjs';
import { BehaviorSubject, Observable, concat } from 'rxjs';
import { Joke } from './joke-store.model';

@Injectable({
Expand Down
7 changes: 6 additions & 1 deletion src/app/shared/feature/joke-list/joke-list.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<ul>
<li *ngFor="let joke of jokes">
@for (joke of jokes; track joke){
<li>
<button
mat-icon-button
title="Favourites"
Expand All @@ -13,4 +14,8 @@
</button>
{{ joke.value }}
</li>

} @empty {
<h1>🤡 Loading jokes...</h1>
}
</ul>
11 changes: 8 additions & 3 deletions src/app/views/favourites/favourites.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<cnjg-joke-list
[jokes]="(favouritesStoreService.favourites$ | async) || []"
></cnjg-joke-list>
@if(favouritesStoreService.favourites$ | async; as favourites){
@if(favourites.length > 0){
<cnjg-joke-list [jokes]="favourites"></cnjg-joke-list>
}@else {
<h1>🤡 No jokes to be found...</h1>
} }@else {
<h1>❌ Error loading favourites</h1>
}
5 changes: 5 additions & 0 deletions src/app/views/favourites/favourites.component.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
:host {
height: 100%;
}

h1 {
align-content: center;
height: 100%;
}

0 comments on commit 37137a5

Please sign in to comment.