mirror of https://github.com/Ombi-app/Ombi
parent
9b1a1062ac
commit
689a869507
@ -0,0 +1,19 @@
|
||||
import { APP_BASE_HREF } from "@angular/common";
|
||||
import { Injectable, Inject } from "@angular/core";
|
||||
|
||||
import { HttpClient } from "@angular/common/http";
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
import { IFeatureEnablement } from "../interfaces";
|
||||
import { ServiceHelpers } from "./service.helpers";
|
||||
|
||||
@Injectable({ providedIn: "root" })
|
||||
export class FeatureService extends ServiceHelpers {
|
||||
constructor(http: HttpClient, @Inject(APP_BASE_HREF) href:string) {
|
||||
super(http, "/api/v2/Features/", href);
|
||||
}
|
||||
|
||||
public getFeatures(): Observable<IFeatureEnablement[]> {
|
||||
return this.http.get<IFeatureEnablement[]>(this.url, {headers: this.headers});
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
import { APP_INITIALIZER } from "@angular/core";
|
||||
import { FeaturesFacade } from "./features.facade";
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
export const FEATURES_INITIALIZER = {
|
||||
provide: APP_INITIALIZER,
|
||||
useFactory: (featureFacade: FeaturesFacade) => (): Observable<unknown> => featureFacade.loadFeatures(),
|
||||
multi: true,
|
||||
deps: [FeaturesFacade],
|
||||
};
|
@ -0,0 +1,3 @@
|
||||
export class LoadFeatures {
|
||||
public static readonly type = '[Features] LoadAll';
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
import { FeaturesSelectors } from "./features.selectors";
|
||||
import { IFeatureEnablement } from "../../interfaces";
|
||||
import { Injectable } from "@angular/core";
|
||||
import { LoadFeatures } from "./features.actions";
|
||||
import { Observable } from "rxjs";
|
||||
import { Store } from "@ngxs/store";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class FeaturesFacade {
|
||||
|
||||
public constructor(private store: Store) {}
|
||||
|
||||
public features$ = (): Observable<IFeatureEnablement[]> => this.store.select(FeaturesSelectors.features);
|
||||
|
||||
public loadFeatures = (): Observable<unknown> => this.store.dispatch(new LoadFeatures());
|
||||
|
||||
public is4kEnabled = (): boolean => this.store.selectSnapshot(FeaturesSelectors.is4kEnabled);
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
import { ICustomizationSettings, IFeatureEnablement } from "../../interfaces";
|
||||
|
||||
import { FEATURES_STATE_TOKEN } from "./types";
|
||||
import { Selector } from "@ngxs/store";
|
||||
|
||||
export class FeaturesSelectors {
|
||||
|
||||
@Selector([FEATURES_STATE_TOKEN])
|
||||
public static features(features: IFeatureEnablement[]): IFeatureEnablement[] {
|
||||
return features;
|
||||
}
|
||||
|
||||
@Selector([FeaturesSelectors.features])
|
||||
public static is4kEnabled(features: IFeatureEnablement[]): boolean {
|
||||
return features.filter(x => x.name === "Movie4KRequests")[0].enabled;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
import { Action, State, StateContext } from "@ngxs/store";
|
||||
|
||||
import { FEATURES_STATE_TOKEN } from "./types";
|
||||
import { FeatureService } from "../../services/feature.service";
|
||||
import { IFeatureEnablement } from "../../interfaces";
|
||||
import { Injectable } from "@angular/core";
|
||||
import { LoadFeatures } from "./features.actions";
|
||||
import { Observable } from "rxjs";
|
||||
import { tap } from "rxjs/operators";
|
||||
|
||||
@State({
|
||||
name: FEATURES_STATE_TOKEN
|
||||
})
|
||||
@Injectable()
|
||||
export class FeatureState {
|
||||
constructor(private featuresService: FeatureService) { }
|
||||
|
||||
@Action(LoadFeatures)
|
||||
public load({ setState }: StateContext<IFeatureEnablement[]>): Observable<IFeatureEnablement[]> {
|
||||
return this.featuresService.getFeatures().pipe(
|
||||
tap(features =>
|
||||
setState(features)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
export * from './features.state';
|
||||
export * from './features.actions';
|
||||
export * from './features.facade';
|
||||
export * from './features.selectors';
|
@ -0,0 +1,4 @@
|
||||
import { IFeatureEnablement } from "../../interfaces";
|
||||
import { StateToken } from "@ngxs/store";
|
||||
|
||||
export const FEATURES_STATE_TOKEN = new StateToken<IFeatureEnablement[]>('featureEnablement');
|
Loading…
Reference in new issue