add http interceptor

This commit is contained in:
Peter Maquiran
2023-07-11 17:54:08 +01:00
parent 12178d6c3a
commit 9b690c3848
7 changed files with 118 additions and 38 deletions
+30 -15
View File
@@ -21,7 +21,14 @@ export class BackgroundService {
private themeservice: ThemeService,
private storageservice: StorageService,
private http: HttpClient,
) { }
) {
window.addEventListener('focus', (event) => {
if(this.status == 'offline') {
this.tryToReachTheServer()
}
});
}
online() {
if(this.status == 'online') {
@@ -68,28 +75,36 @@ export class BackgroundService {
return false
}
const hasReachedTheServer = await this.tryToReachTheServer()
if(!hasReachedTheServer) {
this.status = 'offline'
document.body.style.setProperty(`--color`, "#ffb703");
document.body.style.setProperty(`--color2`, "#ffb703");
document.body.style.setProperty(`--color3`, "#ffb703");
document.body.style.setProperty(`--color4`, "#ffb703");
document.body.style.setProperty(`--color5`, "#ffb703");
this.storageservice.store('networkCheckStore','offline');
this.callBacks.forEach((e) => {
if (e.type == 'Offline') {
e.funx()
}
})
}
}
async tryToReachTheServer() {
let opts = {
headers: {},
}
try {
await this.http.post(environment.apiURL + "UserAuthentication/Login", '', opts).toPromise();
return true
} catch (error) {
if(error.status != 400) {
this.status = 'offline'
document.body.style.setProperty(`--color`, "#ffb703");
document.body.style.setProperty(`--color2`, "#ffb703");
document.body.style.setProperty(`--color3`, "#ffb703");
document.body.style.setProperty(`--color4`, "#ffb703");
document.body.style.setProperty(`--color5`, "#ffb703");
this.storageservice.store('networkCheckStore','offline');
this.callBacks.forEach((e) => {
if (e.type == 'Offline') {
e.funx()
}
})
return false
}
}
}