Skip to content

Commit a2bdb64

Browse files
committed
feat: move isLocalSetup to utils
1 parent 4cdf7a7 commit a2bdb64

File tree

4 files changed

+109
-34
lines changed

4 files changed

+109
-34
lines changed

projects/lib/utils/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './columns-to-gql-fields';
22
export * from './get-value-by-path';
33
export * from './group-name-sanitizer';
4+
export * from './is-local-setup';
45
export * from './resource-field-by-path';
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { isLocalSetup } from './is-local-setup';
2+
3+
describe('isLocalSetup', () => {
4+
let originalLocation: Location;
5+
6+
beforeEach(() => {
7+
originalLocation = window.location;
8+
9+
delete (window as any).location;
10+
(window as any).location = {
11+
...originalLocation,
12+
hostname: '',
13+
};
14+
});
15+
16+
afterEach(() => {
17+
(window as any).location = originalLocation;
18+
});
19+
20+
it('should return true for localhost', () => {
21+
window.location.hostname = 'localhost';
22+
expect(isLocalSetup()).toBe(true);
23+
});
24+
25+
it('should return true for localhost with port', () => {
26+
window.location.hostname = 'localhost:4200';
27+
expect(isLocalSetup()).toBe(true);
28+
});
29+
30+
it('should return false for 127.0.0.1 (localhost IP)', () => {
31+
window.location.hostname = '127.0.0.1';
32+
expect(isLocalSetup()).toBe(false);
33+
});
34+
35+
it('should return true for portal.dev.local', () => {
36+
window.location.hostname = 'portal.dev.local';
37+
expect(isLocalSetup()).toBe(true);
38+
});
39+
40+
it('should return true for subdomain with portal.dev.local', () => {
41+
window.location.hostname = 'app.portal.dev.local';
42+
expect(isLocalSetup()).toBe(true);
43+
});
44+
45+
it('should return true for portal.dev.local with port', () => {
46+
window.location.hostname = 'portal.dev.local:3000';
47+
expect(isLocalSetup()).toBe(true);
48+
});
49+
50+
it('should return false for production domain', () => {
51+
window.location.hostname = 'portal.example.com';
52+
expect(isLocalSetup()).toBe(false);
53+
});
54+
55+
it('should return false for staging domain', () => {
56+
window.location.hostname = 'staging.portal.example.com';
57+
expect(isLocalSetup()).toBe(false);
58+
});
59+
60+
it('should return false for IP address that is not localhost', () => {
61+
window.location.hostname = '192.168.1.100';
62+
expect(isLocalSetup()).toBe(false);
63+
});
64+
65+
it('should return false for empty hostname', () => {
66+
window.location.hostname = '';
67+
expect(isLocalSetup()).toBe(false);
68+
});
69+
70+
it('should return true for domain containing localhost', () => {
71+
window.location.hostname = 'mylocalhost.com';
72+
expect(isLocalSetup()).toBe(true);
73+
});
74+
75+
it('should return true for domain containing portal.dev.local', () => {
76+
window.location.hostname = 'myportal.dev.local.com';
77+
expect(isLocalSetup()).toBe(true);
78+
});
79+
80+
it('should handle case sensitivity correctly', () => {
81+
window.location.hostname = 'LOCALHOST';
82+
expect(isLocalSetup()).toBe(false);
83+
84+
window.location.hostname = 'PORTAL.DEV.LOCAL';
85+
expect(isLocalSetup()).toBe(false);
86+
});
87+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const isLocalSetup = () => {
2+
return window.location.hostname.includes('localhost') || window.location.hostname.includes('portal.dev.local');
3+
};

projects/wc/src/app/components/organization-management/organization-management.component.ts

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
1-
import {
2-
ChangeDetectionStrategy,
3-
Component,
4-
OnInit,
5-
ViewEncapsulation,
6-
effect,
7-
inject,
8-
input,
9-
linkedSignal,
10-
signal,
11-
} from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, OnInit, ViewEncapsulation, effect, inject, input, linkedSignal, signal } from '@angular/core';
122
import { FormsModule } from '@angular/forms';
133
import { LuigiClient } from '@luigi-project/client/luigi-element';
4+
import { EnvConfigService, I18nService, Resource, ResourceDefinition } from '@openmfp/portal-ui-lib';
5+
import { ResourceNodeContext, ResourceService } from '@platform-mesh/portal-ui-lib/services';
146
import {
15-
EnvConfigService,
16-
I18nService,
17-
Resource,
18-
ResourceDefinition,
19-
} from '@openmfp/portal-ui-lib';
20-
import {
21-
ResourceNodeContext,
22-
ResourceService,
23-
} from '@platform-mesh/portal-ui-lib/services';
24-
import { generateGraphQLFields } from '@platform-mesh/portal-ui-lib/utils';
7+
generateGraphQLFields,
8+
isLocalSetup,
9+
} from '@platform-mesh/portal-ui-lib/utils';
2510
import {
2611
ButtonComponent,
2712
InputComponent,
@@ -89,8 +74,7 @@ export class OrganizationManagementComponent implements OnInit {
8974
.subscribe({
9075
next: (result) => {
9176
this.organizations.set(
92-
result['Accounts']
93-
.map((o) => o.metadata.name)
77+
result['Accounts'].map((o) => o.metadata.name),
9478
);
9579
},
9680
});
@@ -120,10 +104,14 @@ export class OrganizationManagementComponent implements OnInit {
120104
]);
121105
this.organizationToSwitch.set(this.newOrganization);
122106
this.newOrganization = '';
123-
this.LuigiClient().uxManager().showAlert({
124-
text: this.getMessageForOrganizationCreation(this.organizationToSwitch()),
125-
type: 'info',
126-
});
107+
this.LuigiClient()
108+
.uxManager()
109+
.showAlert({
110+
text: this.getMessageForOrganizationCreation(
111+
this.organizationToSwitch(),
112+
),
113+
type: 'info',
114+
});
127115
},
128116
error: (_error) => {
129117
this.LuigiClient()
@@ -137,17 +125,13 @@ export class OrganizationManagementComponent implements OnInit {
137125
}
138126

139127
private getMessageForOrganizationCreation(orgName: string) {
140-
if(this.isLocalSetup()) {
141-
return `A new organization has just been onboarded. Since the portal runs on localhost, you need to add the organization to your machine's hosts file in order to switch to it. Add the following entry to your hosts configuration: 127.0.0.1 ${orgName}.portal.dev.local`
128+
if (isLocalSetup()) {
129+
return `A new organization has just been onboarded. Since the portal runs on localhost, you need to add the organization to your machine's hosts file in order to switch to it. Add the following entry to your hosts configuration: 127.0.0.1 ${orgName}.portal.dev.local`;
142130
}
143131

144132
return 'New organization has been created, select it from the list to switch to it.';
145133
}
146134

147-
private isLocalSetup() {
148-
return window.location.hostname.includes('localhost') || window.location.hostname.includes('portal.dev.local');
149-
}
150-
151135
private readTranslations() {
152136
return {
153137
explanation: this.i18nService.getTranslation(
@@ -209,4 +193,4 @@ export class OrganizationManagementComponent implements OnInit {
209193
const port = window.location.port ? `:${window.location.port}` : '';
210194
window.location.href = `${protocol}//${fullSubdomain}${port}`;
211195
}
212-
}
196+
}

0 commit comments

Comments
 (0)