Skip to content

Commit 66379d7

Browse files
committed
Migrate status-banner config to AppConfigService. Delete AppConfigModule and all unused occurences
1 parent 39f408b commit 66379d7

19 files changed

+133
-111
lines changed

src/app/app-config.module.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/app/app-config.service.spec.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { TestBed } from "@angular/core/testing";
2+
import { AppConfig, AppConfigService } from "./app-config.service";
3+
import { provideHttpClient } from "@angular/common/http";
4+
import {
5+
HttpTestingController,
6+
provideHttpClientTesting,
7+
} from "@angular/common/http/testing";
8+
9+
describe("AppConfigService", () => {
10+
let service: AppConfigService;
11+
let httpTesting: HttpTestingController;
12+
13+
beforeEach(() => {
14+
TestBed.configureTestingModule({
15+
providers: [
16+
AppConfigService,
17+
provideHttpClient(),
18+
provideHttpClientTesting(),
19+
],
20+
});
21+
httpTesting = TestBed.inject(HttpTestingController);
22+
service = TestBed.inject(AppConfigService);
23+
});
24+
25+
it("should be created", () => {
26+
expect(httpTesting).toBeTruthy();
27+
expect(service).toBeTruthy();
28+
});
29+
30+
it("should load app config from backend", async () => {
31+
const mockConfig = {
32+
production: false,
33+
logoWidth: "500",
34+
facility: "test-facility",
35+
oaiProviderRoute: "test-oai",
36+
doiBaseUrl: "test-doi",
37+
directMongoAccess: false,
38+
accessDataHref: "test-access",
39+
accessInstructions: "test-instructions",
40+
scicatBaseUrl: "test-scicat",
41+
showLogoBanner: true,
42+
logoBanner: "test-logo",
43+
lbBaseUrl: "test-lb",
44+
statusMessage: "test-status",
45+
statusCode: "INFO",
46+
contactEmail: "test-contact",
47+
};
48+
const configPromise = service.loadAppConfig();
49+
const req = httpTesting.expectOne("/config");
50+
expect(req.request.method).toBe("GET");
51+
req.flush(mockConfig);
52+
await configPromise;
53+
console.log("Config loaded:", service.getConfig());
54+
expect(service.getConfig()).toEqual(mockConfig as AppConfig);
55+
});
56+
57+
it("should set defaults for status banner and help if not provided", async () => {
58+
const mockConfig = {
59+
production: false,
60+
logoWidth: "500",
61+
facility: "test-facility",
62+
oaiProviderRoute: "test-oai",
63+
doiBaseUrl: "test-doi",
64+
directMongoAccess: false,
65+
accessDataHref: "test-access",
66+
accessInstructions: "test-instructions",
67+
scicatBaseUrl: "test-scicat",
68+
showLogoBanner: true,
69+
logoBanner: "test-logo",
70+
lbBaseUrl: "test-lb",
71+
};
72+
const configPromise = service.loadAppConfig();
73+
const req = httpTesting.expectOne("/config");
74+
expect(req.request.method).toBe("GET");
75+
req.flush(mockConfig);
76+
await configPromise;
77+
const config = service.getConfig();
78+
expect(config.statusMessage).toBe("");
79+
expect(config.statusCode).toBe("NONE");
80+
expect(config.contactEmail).toBe("");
81+
});
82+
});

src/app/app-config.service.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { timeout } from "rxjs/operators";
44
import { environment } from "../environments/environment";
55
import { InjectionToken } from "@angular/core";
66

7-
export const APP_DYN_CONFIG = new InjectionToken<AppConfig>("app.dyn.config");
7+
export const APP_DYN_CONFIG = new InjectionToken<AppConfigService>(
8+
"app.dyn.config",
9+
);
810

911
export interface AppConfig {
1012
production: boolean;
@@ -20,6 +22,9 @@ export interface AppConfig {
2022
logoWidth?: string;
2123
retrieveToEmail: RetrieveDestinations | undefined;
2224
lbBaseUrl: string | null;
25+
statusMessage: string;
26+
statusCode: "INFO" | "WARN" | "NONE";
27+
contactEmail: string;
2328
}
2429

2530
export class RetrieveDestinations {
@@ -31,32 +36,45 @@ export class RetrieveDestinations {
3136

3237
@Injectable({ providedIn: "root" })
3338
export class AppConfigService {
34-
private appConfig: object = {};
39+
private appConfig: AppConfig = {} as AppConfig;
3540

3641
constructor(private http: HttpClient) {}
3742

3843
async loadAppConfig(): Promise<void> {
3944
try {
40-
this.appConfig = await this.http
45+
this.appConfig = (await this.http
4146
.get("/config")
4247
.pipe(timeout(2000))
43-
.toPromise();
48+
.toPromise()) as AppConfig;
4449
} catch (err) {
4550
console.log("No config available in backend, trying with local config.");
4651
try {
47-
this.appConfig = await this.http.get("/assets/config.json").toPromise();
52+
this.appConfig = (await this.http
53+
.get("/assets/config.json")
54+
.toPromise()) as AppConfig;
4855
} catch (err) {
4956
console.log("No config provided, using environment");
50-
this.appConfig = environment;
57+
this.appConfig = environment as AppConfig;
5158
}
5259
}
5360
// Use old default if not provided
5461

55-
(this.appConfig as AppConfig).logoWidth =
56-
(this.appConfig as AppConfig)?.logoWidth ?? "412";
62+
this.appConfig.logoWidth = this.appConfig?.logoWidth ?? "412";
63+
64+
// Parse status-banner related config if exists or set defaults
65+
this.appConfig = {
66+
...this.appConfig,
67+
statusMessage: this.appConfig["statusMessage"] || "",
68+
statusCode: (["INFO", "WARN", "NONE"].includes(
69+
this.appConfig["statusCode"],
70+
)
71+
? this.appConfig["statusCode"]
72+
: "NONE") as "INFO" | "WARN" | "NONE",
73+
contactEmail: this.appConfig["contactEmail"] || "",
74+
};
5775
}
5876

5977
getConfig(): AppConfig {
60-
return this.appConfig as AppConfig;
78+
return this.appConfig;
6179
}
6280
}

src/app/app.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
<img src="../assets/{{ config.logoBanner }}" width="{{ config.logoWidth }}" alt="logo_banner" />
1818
</mat-toolbar>
1919

20-
<address *ngIf="appConfig.contactEmail">
21-
If you have any questions or need help, please contact <a href="mailto:{{ appConfig.contactEmail }}">{{
22-
appConfig.contactEmail }}</a>.
20+
<address *ngIf="config.contactEmail">
21+
If you have any questions or need help, please contact <a href="mailto:{{ config.contactEmail }}">{{
22+
config.contactEmail }}</a>.
2323
</address>
2424
</footer>

src/app/app.component.spec.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { fakeAsync, TestBed, waitForAsync } from "@angular/core/testing";
22
import { RouterTestingModule } from "@angular/router/testing";
33
import { AppComponent } from "./app.component";
4-
import { APP_CONFIG } from "./app-config.module";
54
import { MatToolbarModule } from "@angular/material/toolbar";
65
import { HttpClientModule } from "@angular/common/http";
76
import { APP_DYN_CONFIG } from "./app-config.service";
@@ -19,15 +18,7 @@ describe("AppComponent", () => {
1918
StatusMessageModule,
2019
],
2120
declarations: [AppComponent],
22-
providers: [
23-
{
24-
provide: APP_CONFIG,
25-
useValue: {
26-
production: false,
27-
},
28-
},
29-
{ provide: APP_DYN_CONFIG, useClass: MockAppConfigService },
30-
],
21+
providers: [{ provide: APP_DYN_CONFIG, useClass: MockAppConfigService }],
3122
}).compileComponents();
3223
}));
3324

src/app/app.component.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { APP_CONFIG, AppConfig } from "./app-config.module";
21
import { Component, Inject, OnInit } from "@angular/core";
32
import { LoopBackConfig } from "./shared/sdk";
43
import { Title } from "@angular/platform-browser";
@@ -20,13 +19,12 @@ export class AppComponent implements OnInit {
2019

2120
constructor(
2221
private titleService: Title,
23-
@Inject(APP_CONFIG) public appConfig: AppConfig,
2422
@Inject(APP_DYN_CONFIG) private appConfigService: AppConfigService,
2523
) {
2624
this.config = this.appConfigService.getConfig();
2725
const facility = this.config.facility ?? "";
2826
let status = "test";
29-
if (this.appConfig.production === true) {
27+
if (this.config.production === true) {
3028
status = "";
3129
}
3230
this.title = facility.toUpperCase() + " Public Data Repository " + status;
@@ -37,6 +35,6 @@ export class AppComponent implements OnInit {
3735
LoopBackConfig.setBaseURL(this.config.lbBaseUrl);
3836
console.log("API Path: ", LoopBackConfig.getPath());
3937
console.log("API Version: ", LoopBackConfig.getApiVersion());
40-
this.showStatusBanner = this.appConfig.statusCode !== "NONE";
38+
this.showStatusBanner = this.config.statusCode !== "NONE";
4139
}
4240
}

src/app/app.module.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { AppRoutingModule } from "./app-routing.module";
55
import { AppComponent } from "./app.component";
66
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
77
import { MatToolbarModule } from "@angular/material/toolbar";
8-
import { AppConfigModule } from "./app-config.module";
98
import { SDKBrowserModule } from "./shared/sdk";
109
import { APP_DYN_CONFIG, AppConfigService } from "./app-config.service";
1110
import { HttpClientModule } from "@angular/common/http";
@@ -23,7 +22,6 @@ const appThemeInitializerFn = (appTheme: AppThemeService) => {
2322
@NgModule({
2423
declarations: [AppComponent],
2524
imports: [
26-
AppConfigModule,
2725
AppRoutingModule,
2826
BrowserAnimationsModule,
2927
BrowserModule,

src/app/dashboard/dashboard.component.spec.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ComponentFixture, TestBed, waitForAsync } from "@angular/core/testing";
22

33
import { DashboardComponent } from "./dashboard.component";
4-
import { APP_CONFIG } from "../app-config.module";
54
import { Router } from "@angular/router";
65
import { MatCardModule } from "@angular/material/card";
76
import { DatePipe } from "@angular/common";
@@ -28,13 +27,6 @@ describe("DashboardComponent", () => {
2827
imports: [MatCardModule, TableModule, HttpClientModule],
2928
providers: [
3029
DatePipe,
31-
{
32-
provide: APP_CONFIG,
33-
useValue: {
34-
facility: "ess",
35-
directMongoAccess: true,
36-
},
37-
},
3830
{ provide: APP_DYN_CONFIG, useClass: MockAppConfigService },
3931
{ provide: Router, useValue: router },
4032
{ provide: DatasourceService, useClass: MockDatasourceService },

src/app/dashboard/dashboard.component.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { APP_CONFIG, AppConfig } from "../app-config.module";
21
import { Component, Inject } from "@angular/core";
32
import { Router } from "@angular/router";
43
import { Observable } from "rxjs";
@@ -61,7 +60,6 @@ export class DashboardComponent {
6160
config: Config;
6261

6362
constructor(
64-
@Inject(APP_CONFIG) private appConfig: AppConfig,
6563
@Inject(APP_DYN_CONFIG) private appConfigService: AppConfigService,
6664
private datasourceService: DatasourceService,
6765
private router: Router,

src/app/dashboard/dashboard.module.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import { TableModule } from "../shared/modules/table/table.module";
88
import { DatasourceService } from "../datasource.service";
99
import { OAIService } from "../oai.service";
1010
import { PublishedDataService } from "../published-data.service";
11-
import { AppConfigModule } from "../app-config.module";
1211
import { HttpClientModule } from "@angular/common/http";
1312

1413
@NgModule({
1514
declarations: [DashboardComponent],
1615
imports: [
17-
AppConfigModule,
1816
CommonModule,
1917
FlexLayoutModule,
2018
DashboardRoutingModule,
@@ -23,7 +21,6 @@ import { HttpClientModule } from "@angular/common/http";
2321
HttpClientModule,
2422
],
2523
providers: [
26-
AppConfigModule,
2724
DatasourceService,
2825
DatePipe,
2926
OAIService,

0 commit comments

Comments
 (0)