fix(core): convert localization pipe to pure

pull/1838/head
mehmet-erim 6 years ago
parent 8967bb4310
commit bbfc2dc688

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

@ -4,6 +4,7 @@ import { ABP, Session } from '../models';
import { GetAppConfiguration } from '../actions/config.actions';
import { LocalizationService } from '../services/localization.service';
import { from, combineLatest } from 'rxjs';
import { switchMap } from 'rxjs/operators';
@State<Session.State>({
name: 'SessionState',
@ -28,7 +29,9 @@ export class SessionState {
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)

Loading…
Cancel
Save