Skip to content

Commit

Permalink
Get similar posts only in the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillermo Gonzalez committed Aug 23, 2024
1 parent 804825e commit 776baf5
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions apps/blog/src/app/features/post/post-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Injectable } from '@angular/core';
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve } from '@angular/router';
import { isPlatformBrowser } from "@angular/common";

import { Store } from '@ngxs/store';
import { Observable } from 'rxjs';
import { Observable, of } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';

import {
Expand All @@ -14,9 +15,16 @@ import {
RecentCommentAction
} from '@dcs-libs/shared';

@Injectable({ providedIn: 'root' })
@Injectable({providedIn: 'root'})
export class PostResolver implements Resolve<Post> {
constructor(private store: Store) {

isBrowser: boolean;

constructor(
private store: Store,
@Inject(PLATFORM_ID) platformId: string,
) {
this.isBrowser = isPlatformBrowser(platformId);
}

resolve(route: ActivatedRouteSnapshot): Observable<Post> | Promise<Post> | Post {
Expand All @@ -25,8 +33,12 @@ export class PostResolver implements Resolve<Post> {
.pipe(
mergeMap(() => this.store.dispatch(new FetchPostUserLikeAction(route.paramMap.get('id'), user.id))),
mergeMap(() => this.store.dispatch(new RecentCommentAction())),
mergeMap( () => {
return this.store.dispatch(new FetchSimilarPostsAction(this.store.selectSnapshot(PostState.post).id));
mergeMap(() => {
// get similar posts only in the browser
if (this.isBrowser) {
return this.store.dispatch(new FetchSimilarPostsAction(this.store.selectSnapshot(PostState.post).id));
}
return of({});
}),
map(() => this.store.selectSnapshot(PostState.post))
);
Expand Down

0 comments on commit 776baf5

Please sign in to comment.