Skip to content

Commit

Permalink
uniform code-style for effects to check for ssr (#1502)
Browse files Browse the repository at this point in the history
Co-authored-by: LucasHengelhaupt <[email protected]>
  • Loading branch information
LucasHengelhaupt and LucasHengelhaupt authored Sep 13, 2023
1 parent db19f20 commit 76d1e97
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 108 deletions.
12 changes: 5 additions & 7 deletions src/app/core/store/core/configuration/configuration.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ export class ConfigurationEffects {
)
);

setDeviceType$ = createEffect(() =>
iif(
() => !SSR,
setDeviceType$ =
!SSR &&
createEffect(() =>
defer(() =>
merge(this.actions$.pipe(ofType(ROOT_EFFECTS_INIT)), fromEvent(window, 'resize')).pipe(
map<unknown, DeviceType>(() => {
Expand All @@ -132,10 +132,8 @@ export class ConfigurationEffects {
distinctCompareWith(this.store.pipe(select(getDeviceType))),
map(deviceType => applyConfiguration({ _deviceType: deviceType }))
)
),
EMPTY
)
);
)
);

loadSingleServerTranslation$ = createEffect(() =>
this.actions$.pipe(
Expand Down
72 changes: 31 additions & 41 deletions src/app/core/store/core/messages/messages.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { routerRequestAction } from '@ngrx/router-store';
import { Store, select } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import { ActiveToast, IndividualConfig, ToastrService } from 'ngx-toastr';
import { EMPTY, OperatorFunction, Subject, combineLatest, iif } from 'rxjs';
import { OperatorFunction, Subject, combineLatest } from 'rxjs';
import { filter, map, switchMap, take, tap, withLatestFrom } from 'rxjs/operators';

import { getDeviceType } from 'ish-core/store/core/configuration';
Expand Down Expand Up @@ -32,10 +32,10 @@ export class MessagesEffects {

private applyStyle$ = new Subject<void>();

infoToast$ = createEffect(
() =>
iif(
() => !SSR,
infoToast$ =
!SSR &&
createEffect(
() =>
this.actions$.pipe(
ofType(displayInfoMessage),
mapToPayload(),
Expand All @@ -46,15 +46,13 @@ export class MessagesEffects {
}),
this.closeToastOnRouting()
),
EMPTY
),
{ dispatch: false }
);
{ dispatch: false }
);

errorToast$ = createEffect(
() =>
iif(
() => !SSR,
errorToast$ =
!SSR &&
createEffect(
() =>
this.actions$.pipe(
ofType(displayErrorMessage),
mapToPayload(),
Expand All @@ -65,15 +63,13 @@ export class MessagesEffects {
}),
this.closeToastOnRouting()
),
EMPTY
),
{ dispatch: false }
);
{ dispatch: false }
);

warningToast$ = createEffect(
() =>
iif(
() => !SSR,
warningToast$ =
!SSR &&
createEffect(
() =>
this.actions$.pipe(
ofType(displayWarningMessage),
mapToPayload(),
Expand All @@ -84,15 +80,13 @@ export class MessagesEffects {
}),
this.closeToastOnRouting()
),
EMPTY
),
{ dispatch: false }
);
{ dispatch: false }
);

successToast$ = createEffect(
() =>
iif(
() => !SSR,
successToast$ =
!SSR &&
createEffect(
() =>
this.actions$.pipe(
ofType(displaySuccessMessage),
mapToPayload(),
Expand All @@ -103,15 +97,13 @@ export class MessagesEffects {
this.applyStyle$.next();
})
),
EMPTY
),
{ dispatch: false }
);
{ dispatch: false }
);

setToastStickyClass$ = createEffect(
() =>
iif(
() => !SSR,
setToastStickyClass$ =
!SSR &&
createEffect(
() =>
combineLatest([this.store.pipe(select(isStickyHeader)), this.applyStyle$]).pipe(
tap(([sticky]) => {
const container = this.domService.getElementById('toast-container');
Expand All @@ -125,10 +117,8 @@ export class MessagesEffects {
}
})
),
EMPTY
),
{ dispatch: false }
);
{ dispatch: false }
);

private closeToastOnRouting() {
return switchMap((activeToast: ActiveToast<unknown>) =>
Expand Down
14 changes: 6 additions & 8 deletions src/app/core/store/core/viewconf/viewconf.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { routerNavigatedAction, routerRequestAction } from '@ngrx/router-store';
import { Store, select } from '@ngrx/store';
import { EMPTY, defer, fromEvent, iif } from 'rxjs';
import { defer, fromEvent } from 'rxjs';
import { bufferToggle, concatMap, delay, distinctUntilChanged, filter, first, map } from 'rxjs/operators';

import { BreadcrumbItem } from 'ish-core/models/breadcrumb-item/breadcrumb-item.interface';
Expand All @@ -15,19 +15,17 @@ import { setBreadcrumbData, setStickyHeader } from './viewconf.actions';
export class ViewconfEffects {
constructor(private store: Store, private actions$: Actions) {}

toggleStickyHeader$ = createEffect(() =>
iif(
() => !SSR,
toggleStickyHeader$ =
!SSR &&
createEffect(() =>
defer(() =>
fromEvent(window, 'scroll').pipe(
map(() => window.scrollY >= 170),
distinctUntilChanged(),
map(sticky => setStickyHeader({ sticky }))
)
),
EMPTY
)
);
)
);

retrieveBreadcrumbDataFromRouting$ = createEffect(() =>
this.actions$.pipe(
Expand Down
12 changes: 5 additions & 7 deletions src/app/core/store/customer/basket/basket.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ export class BasketEffects {
/**
* Reload basket information on basket route to ensure that rendered page is correct.
*/
loadBasketOnBasketPage$ = createEffect(() =>
iif(
() => !SSR,
loadBasketOnBasketPage$ =
!SSR &&
createEffect(() =>
this.actions$.pipe(
ofType(personalizationStatusDetermined),
take(1),
Expand All @@ -372,10 +372,8 @@ export class BasketEffects {
map(() => loadBasket())
)
)
),
EMPTY
)
);
)
);

/**
* Creates a requisition based on the given basket, if approval is required
Expand Down
14 changes: 6 additions & 8 deletions src/app/core/store/customer/orders/orders.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { routerNavigatedAction } from '@ngrx/router-store';
import { Store, select } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import { isEqual } from 'lodash-es';
import { EMPTY, from, iif, merge, race } from 'rxjs';
import { EMPTY, from, merge, race } from 'rxjs';
import {
concatMap,
distinctUntilChanged,
Expand Down Expand Up @@ -163,18 +163,16 @@ export class OrdersEffects {
/**
* Selects and loads an order.
*/
loadOrderForSelectedOrder$ = createEffect(() =>
iif(
() => !SSR,
loadOrderForSelectedOrder$ =
!SSR &&
createEffect(() =>
this.actions$.pipe(
ofType(selectOrder),
mapToPayloadProperty('orderId'),
whenTruthy(),
map(orderId => loadOrder({ orderId }))
),
EMPTY
)
);
)
);

/**
* Triggers a SelectOrder action if route contains orderId query or route parameter.
Expand Down
34 changes: 12 additions & 22 deletions src/app/core/store/customer/user/user.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@ import { Actions, createEffect, ofType } from '@ngrx/effects';
import { routerNavigatedAction } from '@ngrx/router-store';
import { Store, select } from '@ngrx/store';
import { from } from 'rxjs';
import {
concatMap,
delay,
exhaustMap,
filter,
map,
mergeMap,
sample,
switchMap,
takeWhile,
withLatestFrom,
} from 'rxjs/operators';
import { concatMap, delay, exhaustMap, filter, map, mergeMap, sample, switchMap, withLatestFrom } from 'rxjs/operators';

import { CustomerRegistrationType } from 'ish-core/models/customer/customer.model';
import { PaymentService } from 'ish-core/services/payment/payment.service';
Expand Down Expand Up @@ -141,16 +130,17 @@ export class UserEffects {
* redirects to the returnUrl after successful login
* does not redirect at all, if no returnUrl is defined
*/
redirectAfterLogin$ = createEffect(
() =>
this.store.pipe(select(selectQueryParam('returnUrl'))).pipe(
takeWhile(() => !SSR),
whenTruthy(),
sample(this.actions$.pipe(ofType(loginUserSuccess))),
concatMap(navigateTo => from(this.router.navigateByUrl(navigateTo)))
),
{ dispatch: false }
);
redirectAfterLogin$ =
!SSR &&
createEffect(
() =>
this.store.pipe(select(selectQueryParam('returnUrl'))).pipe(
whenTruthy(),
sample(this.actions$.pipe(ofType(loginUserSuccess))),
concatMap(navigateTo => from(this.router.navigateByUrl(navigateTo)))
),
{ dispatch: false }
);

createUser$ = createEffect(() =>
this.actions$.pipe(
Expand Down
30 changes: 15 additions & 15 deletions src/app/core/store/shopping/search/search.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
map,
sample,
switchMap,
takeWhile,
withLatestFrom,
} from 'rxjs/operators';

Expand Down Expand Up @@ -114,22 +113,23 @@ export class SearchEffects {
)
);

suggestSearch$ = createEffect(() =>
this.actions$.pipe(
takeWhile(() => !SSR),
ofType(suggestSearch),
mapToPayloadProperty('searchTerm'),
debounceTime(400),
distinctUntilChanged(),
whenTruthy(),
switchMap(searchTerm =>
this.suggestService.search(searchTerm).pipe(
map(suggests => suggestSearchSuccess({ searchTerm, suggests })),
catchError(() => EMPTY)
suggestSearch$ =
!SSR &&
createEffect(() =>
this.actions$.pipe(
ofType(suggestSearch),
mapToPayloadProperty('searchTerm'),
debounceTime(400),
distinctUntilChanged(),
whenTruthy(),
switchMap(searchTerm =>
this.suggestService.search(searchTerm).pipe(
map(suggests => suggestSearchSuccess({ searchTerm, suggests })),
catchError(() => EMPTY)
)
)
)
)
);
);

redirectIfSearchProductFail$ = createEffect(
() =>
Expand Down

0 comments on commit 76d1e97

Please sign in to comment.