Skip to content

Commit

Permalink
resolve path of blog images
Browse files Browse the repository at this point in the history
  • Loading branch information
ortwic committed Apr 6, 2024
1 parent 682d0f7 commit 6206b78
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('ImageSliderComponent', () => {
useValue: {
downloadUrl: () => Promise.resolve('')
}
}
}
]
})
.compileComponents();
Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/blog/blog.component.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div class="card" *ngFor="let post of blogPosts$ | async">
<mat-card class="card">
<mat-card-header>
@if (post.images) {
<img class="thumbnail" mat-card-lg-image [src]="post.images[0].value" />
@if (post.imageSrc) {
<img class="thumbnail" mat-card-lg-image [src]="post.imageSrc" />
}
<mat-card-title-group>
<mat-card-title>
Expand Down
7 changes: 7 additions & 0 deletions src/app/pages/blog/blog.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { of } from 'rxjs';

import { BlogComponent } from './blog.component';
import { BlogService } from '../../services/blog.service';
import { StorageService } from '../../services/storage.service';

const params = {
tag: 'foo'
Expand All @@ -29,6 +30,12 @@ describe('BlogComponent', () => {
useValue: {
data$: of([])
}
},
{
provide: StorageService,
useValue: {
downloadUrl: () => Promise.resolve('')
}
}
]
})
Expand Down
23 changes: 20 additions & 3 deletions src/app/pages/blog/blog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Component, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatCardModule } from '@angular/material/card';
import { ActivatedRoute, RouterModule } from '@angular/router';
import { map } from 'rxjs';
import { map, switchMap } from 'rxjs';
import { BlogService } from '../../services/blog.service';
import { StorageService } from '../../services/storage.service';
import { BlogPost } from '../../models/blog.model';

@Component({
selector: 'app-blog',
Expand All @@ -15,15 +17,30 @@ import { BlogService } from '../../services/blog.service';
export class BlogComponent {
readonly route = inject(ActivatedRoute);
readonly service = inject(BlogService);
readonly blogPosts$ = this.service.data$;
readonly storageService = inject(StorageService);
readonly blogPosts$ = this.service.data$.pipe(
switchMap(async (posts) => this.resolveUrl(posts))
);

constructor() {
const tag = this.route.snapshot.params['tag'];
if (tag) {
this.blogPosts$ = this.service.data$.pipe(
map((posts) => posts.filter((post) => post.tags?.includes(tag)))
map((posts) => posts.filter((post) => post.tags?.includes(tag))),
switchMap(async (posts) => this.resolveUrl(posts))
);
document.title = 'Blog - ' + tag + ' | Why App';
}
}

private async resolveUrl(posts: BlogPost[]) {
return Promise.all(posts.map(async (post) => {
const path = post.images[0].value;
const imageUrl = await this.storageService.downloadUrl(path);
return {
...post,
imageSrc: imageUrl
};
}));
}
}

0 comments on commit 6206b78

Please sign in to comment.