This commit is contained in:
Eugene Pankov
2021-06-25 21:55:40 +02:00
parent 663615fe06
commit 0689c984ff
24 changed files with 573 additions and 320 deletions

View File

@@ -4,6 +4,7 @@ import { debounceTime } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { LoginService } from '../services/login.service'
import { Config, Version } from '../api'
export class SocketProxy {
connect$ = new Subject<void>()
@@ -86,10 +87,9 @@ export class SocketProxy {
@Injectable({ providedIn: 'root' })
export class AppConnectorService {
config: any
version: any
user: any
private configUpdate = new Subject<string>()
private config: Config
private version: Version
constructor (
private http: HttpClient,
@@ -101,6 +101,11 @@ export class AppConnectorService {
})
}
setState (config: Config, version: Version) {
this.config = config
this.version = version
}
async loadConfig (): Promise<string> {
return this.config.content
}

View File

@@ -0,0 +1,83 @@
import * as semverGT from 'semver/functions/gt'
import { AsyncSubject, Subject } from 'rxjs'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { Config, User, Version } from '../api'
import { LoginService } from './login.service'
@Injectable({ providedIn: 'root' })
export class ConfigService {
activeConfig$ = new Subject<Config>()
activeVersion$ = new Subject<Version>()
user: User
configs: Config[] = []
versions: Version[] = []
ready$ = new AsyncSubject<void>()
get activeConfig (): Config { return this._activeConfig }
get activeVersion (): Version { return this._activeVersion }
private _activeConfig: Config|null = null
private _activeVersion: Version|null = null
constructor (
private http: HttpClient,
private loginService: LoginService,
) {
this.init()
}
async updateUser () {
await this.http.put('/api/1/user', this.user).toPromise()
}
async createNewConfig () {
this.configs.push(await this.http.post('/api/1/configs', {
content: '{}',
last_used_with_version: this._activeVersion.version,
}).toPromise())
}
async duplicateActiveConfig () {
const copy = {...this._activeConfig, pk: undefined}
this.configs.push(await this.http.post('/api/1/configs', copy).toPromise())
}
async selectVersion (version: Version) {
this._activeVersion = version
this.activeVersion$.next(version)
}
async selectConfig (config: Config) {
let matchingVersion = this.versions.find(x => x.version === config.last_used_with_version)
if (!matchingVersion) {
// TODO ask to upgrade
matchingVersion = this.versions[0]
}
this._activeConfig = config
this.activeConfig$.next(config)
this.selectVersion(matchingVersion)
}
async selectDefaultConfig () {
await this.ready$.toPromise()
await this.loginService.ready$.toPromise()
this.selectConfig(this.configs.find(c => c.id === this.loginService.user.active_config) ?? this.configs[0])
}
private async init () {
this.configs = await this.http.get('/api/1/configs').toPromise()
this.versions = await this.http.get('/api/1/versions').toPromise()
this.versions.sort((a, b) => semverGT(a, b))
if (!this.configs.length) {
await this.createNewConfig()
}
this.ready$.next()
this.ready$.complete()
}
}

View File

@@ -1,20 +1,29 @@
import { AsyncSubject } from 'rxjs'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { User } from '../api'
@Injectable({ providedIn: 'root' })
export class LoginService {
user: any
user: User
ready$ = new AsyncSubject<void>()
constructor (private http: HttpClient) {
this.init()
}
async updateUser () {
await this.http.put('/api/1/user', this.user).toPromise()
}
private async init () {
const user = await this.http.get('/api/1/user').toPromise()
this.user = user
try {
this.user = await this.http.get('/api/1/user').toPromise()
} catch {
this.user = null
}
this.ready$.next()
this.ready$.complete()
}