Browse Source

fix(core): convert localization pipe to pure

pull/1838/head
mehmet-erim 7 years ago
parent
commit
bbfc2dc688
  1. 46
      npm/ng-packs/packages/core/src/lib/pipes/localization.pipe.ts
  2. 5
      npm/ng-packs/packages/core/src/lib/states/session.state.ts

46
npm/ng-packs/packages/core/src/lib/pipes/localization.pipe.ts

@ -1,46 +1,20 @@
import { Pipe, PipeTransform, OnDestroy } from '@angular/core'; import { Pipe, PipeTransform } from '@angular/core';
import { Store } from '@ngxs/store'; import { Store } from '@ngxs/store';
import { Config } from '../models';
import { ConfigState } from '../states'; import { ConfigState } from '../states';
import { takeUntilDestroy } from '../utils';
import { distinctUntilChanged, takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
@Pipe({ @Pipe({
name: 'abpLocalization', name: 'abpLocalization',
// tslint:disable-next-line: no-pipe-impure
pure: false // required to update the value
}) })
export class LocalizationPipe implements PipeTransform, OnDestroy { export class LocalizationPipe implements PipeTransform {
initialValue = '';
value: string;
destroy$ = new Subject();
constructor(private store: Store) {} constructor(private store: Store) {}
transform(value: string = '', ...interpolateParams: string[]): string { transform(value: string | Config.LocalizationWithDefault = '', ...interpolateParams: string[]): string {
if (this.initialValue !== value) { return this.store.selectSnapshot(
this.initialValue = value; ConfigState.getLocalization(
this.destroy$.next(); value,
...interpolateParams.reduce((acc, val) => (Array.isArray(val) ? [...acc, ...val] : [...acc, val]), []),
this.store ),
.select( );
ConfigState.getCopy(
value,
...interpolateParams.reduce((acc, val) => (Array.isArray(val) ? [...acc, ...val] : [...acc, val]), [])
)
)
.pipe(
takeUntil(this.destroy$),
takeUntilDestroy(this),
distinctUntilChanged()
)
.subscribe(copy => (this.value = copy));
}
return this.value;
} }
ngOnDestroy() {}
} }

5
npm/ng-packs/packages/core/src/lib/states/session.state.ts

@ -4,6 +4,7 @@ import { ABP, Session } from '../models';
import { GetAppConfiguration } from '../actions/config.actions'; import { GetAppConfiguration } from '../actions/config.actions';
import { LocalizationService } from '../services/localization.service'; import { LocalizationService } from '../services/localization.service';
import { from, combineLatest } from 'rxjs'; import { from, combineLatest } from 'rxjs';
import { switchMap } from 'rxjs/operators';
@State<Session.State>({ @State<Session.State>({
name: 'SessionState', name: 'SessionState',
@ -28,7 +29,9 @@ export class SessionState {
language: payload, language: payload,
}); });
return combineLatest([dispatch(new GetAppConfiguration()), from(this.localizationService.registerLocale(payload))]); return dispatch(new GetAppConfiguration()).pipe(
switchMap(() => from(this.localizationService.registerLocale(payload))),
);
} }
@Action(SetTenant) @Action(SetTenant)

Loading…
Cancel
Save