-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtogglz.service.ts
56 lines (51 loc) · 1.73 KB
/
togglz.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Injectable } from '@angular/core'
import { environment } from 'src/environments/environment'
import { HttpClient } from '@angular/common/http'
import { Config } from 'src/app/types/togglz.endpoint'
import { Observable, timer } from 'rxjs'
import { switchMapTo, shareReplay, map } from 'rxjs/operators'
import { MaintenanceMessage } from 'src/app/types/togglz.local'
@Injectable({
providedIn: 'root',
})
export class TogglzService {
togglz: Observable<Config>
constructor(private _http: HttpClient) {}
private getConfig() {
return this._http.get<Config>(environment.API_WEB + 'config.json', {
withCredentials: true,
})
}
getTogglz(): Observable<Config> {
if (this.togglz) {
return this.togglz
} else {
return (this.togglz = timer(0, 60 * 1000).pipe(
switchMapTo(this.getConfig()),
shareReplay(1)
))
}
}
getStateOf(togglzFeatureName: string): Observable<boolean> {
this.getTogglz()
return this.togglz.pipe(
map(data => data.messages[togglzFeatureName] === 'true')
)
}
getMessageOf(togglzFeatureName: string): Observable<string> {
this.getTogglz()
return this.togglz.pipe(map(data => data.messages[togglzFeatureName]))
}
getMaintenanceMessages(): Observable<MaintenanceMessage> {
return this.getMessageOf('MAINTENANCE_MESSAGE').pipe(
map(value => {
const plainHtml = value
const parser = new DOMParser()
const htmlElement = parser.parseFromString(plainHtml, 'text/html')
const closableElements = htmlElement.querySelectorAll('div.closable')
const nonClosableElements = htmlElement.querySelectorAll('div.regular')
return { plainHtml, closableElements, nonClosableElements }
})
)
}
}