Skip to content

Commit e8357d0

Browse files
authored
feat: create article preview (#334)
1 parent 6dac378 commit e8357d0

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

libs/blog/articles/data-access/src/lib/infrastructure/articles.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ export class ArticlesService {
1717
return this._http.get<Article>(`${this._apiBaseUrl}/articles/${slug}`);
1818
}
1919

20+
getArticlePreviewBySlug(slug: string): Observable<Article> {
21+
return this._http.get<Article>(`${this._apiBaseUrl}/articles/${slug}`, {
22+
headers: {
23+
'Cache-Control': 'no-cache',
24+
},
25+
});
26+
}
27+
2028
getArticleList(
2129
query: ArticlesQuery,
2230
): Observable<ArrayResponse<ArticlePreview>> {

libs/blog/articles/data-access/src/lib/state/article-details.store.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, inject } from '@angular/core';
1+
import { computed, inject, InjectionToken } from '@angular/core';
22
import { tapResponse } from '@ngrx/operators';
33
import {
44
patchState,
@@ -30,6 +30,13 @@ const initialState: ArticleDetailsState = {
3030
slug: null,
3131
};
3232

33+
export const IsArticlePreview = new InjectionToken<boolean>(
34+
'Article Details Store Is Article Preview',
35+
{
36+
factory: () => false,
37+
},
38+
);
39+
3340
export const ArticleDetailsStore = signalStore(
3441
{ providedIn: 'root' },
3542
withSeo(),
@@ -38,6 +45,7 @@ export const ArticleDetailsStore = signalStore(
3845
withLangState(),
3946
withMethods(({ ...store }) => {
4047
const articlesService = inject(ArticlesService);
48+
const isPreview = inject(IsArticlePreview);
4149

4250
return {
4351
fetchArticleDetails: rxMethod<string | undefined>(
@@ -51,7 +59,10 @@ export const ArticleDetailsStore = signalStore(
5159
}),
5260
),
5361
switchMap((slug) =>
54-
articlesService.getArticleBySlug(slug).pipe(
62+
(isPreview
63+
? articlesService.getArticlePreviewBySlug(slug)
64+
: articlesService.getArticleBySlug(slug)
65+
).pipe(
5566
tapResponse({
5667
error: (error) =>
5768
patchState(store, {

libs/blog/articles/feature-shell/src/lib/routes.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Routes } from '@angular/router';
22

3-
import { articleExistsGuard } from '@angular-love/blog/articles/data-access';
3+
import {
4+
ArticleDetailsStore,
5+
articleExistsGuard,
6+
IsArticlePreview,
7+
} from '@angular-love/blog/articles/data-access';
48

59
export const articleRoutes: Routes = [
610
{
@@ -39,6 +43,16 @@ export const articleRoutes: Routes = [
3943
seo: { title: 'Angular In Depth' },
4044
},
4145
},
46+
{
47+
path: 'preview/:articleSlug',
48+
providers: [
49+
{ provide: IsArticlePreview, useValue: true },
50+
ArticleDetailsStore,
51+
],
52+
loadComponent: async () =>
53+
(await import('@angular-love/blog/articles/feature-article'))
54+
.ArticleDetailsContainerComponent,
55+
},
4256
{
4357
path: ':articleSlug',
4458
pathMatch: 'full',

0 commit comments

Comments
 (0)