From 6497c0ac30cff374fbd63d6e7e8fd291d3bef178 Mon Sep 17 00:00:00 2001 From: rubikscraft Date: Mon, 14 Mar 2022 17:17:19 +0100 Subject: [PATCH] add demo identifier to footer --- frontend/src/app/api/api.module.ts | 2 ++ frontend/src/app/api/info.service.ts | 35 +++++++++++++++++++ .../components/footer/footer.component.html | 1 + .../app/components/footer/footer.component.ts | 23 ++++++++++-- .../app/components/footer/footer.module.ts | 3 +- frontend/src/app/models/server-info.ts | 4 +++ 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 frontend/src/app/api/info.service.ts create mode 100644 frontend/src/app/models/server-info.ts diff --git a/frontend/src/app/api/api.module.ts b/frontend/src/app/api/api.module.ts index e090ed7..29f0579 100644 --- a/frontend/src/app/api/api.module.ts +++ b/frontend/src/app/api/api.module.ts @@ -2,6 +2,7 @@ import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; import { ApiService } from './api.service'; import { ImageService } from './image.service'; +import { InfoService } from './info.service'; import { KeyService } from './key.service'; import { PermissionService } from './permission.service'; import { UserService } from './user.service'; @@ -13,6 +14,7 @@ import { UserService } from './user.service'; UserService, PermissionService, KeyService, + InfoService, ], imports: [CommonModule], }) diff --git a/frontend/src/app/api/info.service.ts b/frontend/src/app/api/info.service.ts new file mode 100644 index 0000000..36a74f2 --- /dev/null +++ b/frontend/src/app/api/info.service.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@angular/core'; +import { InfoResponse } from 'picsur-shared/dist/dto/api/info.dto'; +import { AsyncFailable, HasFailed } from 'picsur-shared/dist/types'; +import { BehaviorSubject } from 'rxjs'; +import { ServerInfo } from '../models/server-info'; +import { ApiService } from './api.service'; + +@Injectable({ + providedIn: 'root', +}) +export class InfoService { + private readonly logger = console; + + public get live() { + return this.infoSubject; + } + + private infoSubject = new BehaviorSubject(new ServerInfo()); + + constructor(private api: ApiService) { + this.pollInfo().catch(this.logger.error); + } + + public async pollInfo(): AsyncFailable { + const response = await this.api.get(InfoResponse, '/api/info'); + if (HasFailed(response)) return response; + + const info = new ServerInfo(); + info.production = response.production; + info.demo = response.demo; + + this.infoSubject.next(info); + return info; + } +} diff --git a/frontend/src/app/components/footer/footer.component.html b/frontend/src/app/components/footer/footer.component.html index 28abaed..41f6e48 100644 --- a/frontend/src/app/components/footer/footer.component.html +++ b/frontend/src/app/components/footer/footer.component.html @@ -8,5 +8,6 @@ Source Code + {{ isDemo ? " - Demo Version" : "" }}

diff --git a/frontend/src/app/components/footer/footer.component.ts b/frontend/src/app/components/footer/footer.component.ts index 0f39497..dfaf28c 100644 --- a/frontend/src/app/components/footer/footer.component.ts +++ b/frontend/src/app/components/footer/footer.component.ts @@ -1,8 +1,25 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; +import { AutoUnsubscribe } from 'ngx-auto-unsubscribe-decorator'; +import { InfoService } from 'src/app/api/info.service'; @Component({ selector: 'app-footer', templateUrl: './footer.component.html', - styleUrls: ['./footer.component.scss'] + styleUrls: ['./footer.component.scss'], }) -export class FooterComponent {} +export class FooterComponent implements OnInit { + constructor(private infoService: InfoService) {} + + isDemo: boolean = false; + + ngOnInit(): void { + this.subscribeInfo(); + } + + @AutoUnsubscribe() + subscribeInfo() { + return this.infoService.live.subscribe((info) => { + this.isDemo = info.demo; + }); + } +} diff --git a/frontend/src/app/components/footer/footer.module.ts b/frontend/src/app/components/footer/footer.module.ts index dec2cb3..ea4ecce 100644 --- a/frontend/src/app/components/footer/footer.module.ts +++ b/frontend/src/app/components/footer/footer.module.ts @@ -1,11 +1,12 @@ import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; +import { ApiModule } from 'src/app/api/api.module'; import { FooterComponent } from './footer.component'; @NgModule({ declarations: [FooterComponent], - imports: [CommonModule], + imports: [CommonModule, ApiModule], exports: [FooterComponent], }) export class FooterModule {} diff --git a/frontend/src/app/models/server-info.ts b/frontend/src/app/models/server-info.ts new file mode 100644 index 0000000..729c922 --- /dev/null +++ b/frontend/src/app/models/server-info.ts @@ -0,0 +1,4 @@ +export class ServerInfo { + production: boolean = false; + demo: boolean = false; +}