mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.5 KiB
67 lines
1.5 KiB
/*
|
|
* Squidex Headless CMS
|
|
*
|
|
* @license
|
|
* Copyright (c) Sebastian Stehle. All rights reserved
|
|
*/
|
|
|
|
import * as Ng2 from '@angular/core';
|
|
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
|
|
|
import {
|
|
AppCreateDto,
|
|
AppDto,
|
|
AppsService
|
|
} from './apps.service';
|
|
|
|
import { AuthService } from './auth.service';
|
|
|
|
@Ng2.Injectable()
|
|
export class AppsStoreService {
|
|
private lastApps: AppDto[] = null;
|
|
private readonly apps$ = new BehaviorSubject<AppDto[]>(null);
|
|
|
|
public get apps(): Observable<AppDto[]> {
|
|
return this.apps$;
|
|
}
|
|
|
|
constructor(
|
|
private readonly authService: AuthService,
|
|
private readonly appService: AppsService
|
|
) {
|
|
if (!authService || !appService) {
|
|
return;
|
|
}
|
|
|
|
this.apps$.subscribe(apps => {
|
|
this.lastApps = apps;
|
|
});
|
|
|
|
this.authService.isAuthenticatedChanges.subscribe(isAuthenticated => {
|
|
if (isAuthenticated) {
|
|
this.load();
|
|
}
|
|
});
|
|
}
|
|
|
|
public reload() {
|
|
if (this.authService.isAuthenticated) {
|
|
this.load();
|
|
}
|
|
}
|
|
|
|
private load() {
|
|
this.appService.getApps().subscribe(apps => {
|
|
this.apps$.next(apps);
|
|
});
|
|
}
|
|
|
|
public createApp(appToCreate: AppCreateDto): Observable<any> {
|
|
return this.appService.postApp(appToCreate).do(app => {
|
|
if (this.lastApps && app) {
|
|
this.apps$.next(this.lastApps.concat([app]));
|
|
}
|
|
});
|
|
}
|
|
}
|