Skip to content

Commit

Permalink
refactor apieService to remove getWindowHref()
Browse files Browse the repository at this point in the history
  • Loading branch information
mwood77 committed Feb 25, 2024
1 parent 26bc693 commit 46ce7f5
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 42 deletions.
47 changes: 14 additions & 33 deletions src/angular/osww-frontend/src/app/api.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Injectable, isDevMode } from '@angular/core';
import { environment } from '../environments/environment';
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';


Expand Down Expand Up @@ -41,40 +41,21 @@ export class ApiService {

constructor(private http: HttpClient) { }

static getWindowHref(_window: typeof window): string {
if (_window.location.href.includes('192')) {

// remove single trailing '/' from ip
const sanitizedHref =
_window.location.href.substring(_window.location.href.length - 1) === '/' ?
_window.location.href.substring(0, _window.location.href.length - 1) :
_window.location.href

return sanitizedHref;
}

return 'http://winderoo.local';
}

private constructURL(URL: string): string {
if (URL != this.DEFUALT_URL) {
return URL + "/api/";
}

return this.DEFUALT_URL + '/api/';
static constructURL(): string {
return environment.apiUrl + "/api/";
}

getShouldRefresh() {
return this.shouldRefresh$.asObservable();
}

getStatus(URL: string) {
return this.http.get<Status>(this.constructURL(URL) + 'status');
getStatus() {
return this.http.get<Status>(ApiService.constructURL() + 'status');
}

updatePowerState(URL: string, powerState: boolean) {
updatePowerState(powerState: boolean) {
let powerStateToNum;
const baseURL = this.constructURL(URL);
const baseURL = ApiService.constructURL();

if (powerState) {
powerStateToNum = 1;
Expand All @@ -89,9 +70,9 @@ export class ApiService {
return this.http.post(constructedURL, null, { observe:'response' });
}

updateTimerState(URL: string, timerState: boolean) {
updateTimerState(timerState: boolean) {
let timerStateToNum;
const baseURL = this.constructURL(URL);
const baseURL = ApiService.constructURL();
console.log(timerState)
if (timerState) {
timerStateToNum = 1;
Expand All @@ -106,8 +87,8 @@ export class ApiService {
return this.http.post(constructedURL, null, { observe: 'response' });
}

updateState(URL: string, update: Update) {
const baseURL = this.constructURL(URL);
updateState(update: Update) {
const baseURL = ApiService.constructURL();

const constructedURL = baseURL
+ 'update?action=' + update.action + '&'
Expand All @@ -119,7 +100,7 @@ export class ApiService {
return this.http.post(constructedURL, null, { observe: 'response' });
}

resetDevice(URL: string) {
return this.http.get<any>(this.constructURL(URL) + 'reset');
resetDevice() {
return this.http.get<any>(ApiService.constructURL() + 'reset');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<button mat-menu-item (click)="changeLanguage('de-DE')" [class.active]="translateService.currentLang === 'de-DE'">Deutsch</button>
<button mat-menu-item (click)="changeLanguage('en-US')" [class.active]="!translateService.currentLang || translateService.currentLang === 'en-US'">English</button>
<button mat-menu-item (click)="changeLanguage('es-ES')" [class.active]="translateService.currentLang === 'es-ES'">Español</button>
<button mat-menu-item class="btn btn-sm btn-outline-secondary" (click)="changeLanguage('fr-FR')" [class.active]="translateService.currentLang === 'fr-FR'">Francés</button>
<button mat-menu-item (click)="changeLanguage('fr-FR')" [class.active]="translateService.currentLang === 'fr-FR'">Francés</button>
</mat-menu>

</mat-toolbar-row>
Expand Down
6 changes: 3 additions & 3 deletions src/angular/osww-frontend/src/app/header/header.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit, ViewContainerRef } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { ApiService } from '../api.service';
import { TranslateService, TranslateModule } from '@ngx-translate/core';
Expand Down Expand Up @@ -50,7 +50,7 @@ export class HeaderComponent implements OnInit {
}

updateWinderEnabledState($state: any) {
this.apiService.updatePowerState(ApiService.getWindowHref(window), $state).subscribe(
this.apiService.updatePowerState($state).subscribe(
(data) => {
this.apiService.isWinderEnabled$.next($state);

Expand Down Expand Up @@ -78,7 +78,7 @@ export class ResetDialog {
public translateService: TranslateService) {}

confirmReset(): void {
this.apiService.resetDevice(ApiService.getWindowHref(window)).subscribe();
this.apiService.resetDevice().subscribe();
}

closeDialog(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class SettingsComponent implements OnInit, AfterViewChecked {
}

getData(): void {
this.apiService.getStatus(ApiService.getWindowHref(window)).subscribe((data) => {
this.apiService.getStatus().subscribe((data) => {
this.upload.activityState = data.status;
this.upload.rpd = data.rotationsPerDay;
this.upload.direction = data.direction;
Expand Down Expand Up @@ -213,7 +213,7 @@ export class SettingsComponent implements OnInit, AfterViewChecked {
timerEnabled: this.upload.isTimerEnabledNum,
}

this.apiService.updateState(ApiService.getWindowHref(window), body).subscribe((response) => {
this.apiService.updateState(body).subscribe((response) => {
if (response.status == 204) {
this.getData();
}
Expand Down Expand Up @@ -273,7 +273,7 @@ export class SettingsComponent implements OnInit, AfterViewChecked {

updateTimerEnabledState($state: any) {
this.upload.isTimerEnabledNum = $state;
this.apiService.updateTimerState(ApiService.getWindowHref(window), $state).subscribe(
this.apiService.updateTimerState($state).subscribe(
(data) => {
this.mapTimerEnabledState($state)
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const environment = {
production: true
production: true,
apiUrl: 'http://winderoo.local',
};
3 changes: 2 additions & 1 deletion src/angular/osww-frontend/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// The list of file replacements can be found in `angular.json`.

export const environment = {
production: false
production: false,
apiUrl: 'http://winderoo.local',
};

/*
Expand Down

0 comments on commit 46ce7f5

Please sign in to comment.