Skip to content

Commit 3e7c8c8

Browse files
authored
Merge pull request #41 from platform-mesh/organization-local-setup-check
feat: add local-setup message for new ogranization
2 parents 2db27d7 + c844fa0 commit 3e7c8c8

File tree

5 files changed

+113
-9
lines changed

5 files changed

+113
-9
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.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ describe('OrganizationManagementComponent', () => {
115115
component.ngOnInit();
116116

117117
expect(resourceServiceMock.readOrganizations).toHaveBeenCalled();
118-
expect(component.organizations()).toEqual(['org2']);
118+
expect(component.organizations()).toEqual(['org1', 'org2']);
119119
});
120120

121121
it('should set organization to switch', () => {

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import {
2121
ResourceNodeContext,
2222
ResourceService,
2323
} from '@platform-mesh/portal-ui-lib/services';
24-
import { generateGraphQLFields } from '@platform-mesh/portal-ui-lib/utils';
24+
import {
25+
generateGraphQLFields,
26+
isLocalSetup,
27+
} from '@platform-mesh/portal-ui-lib/utils';
2528
import {
2629
ButtonComponent,
2730
InputComponent,
@@ -89,9 +92,7 @@ export class OrganizationManagementComponent implements OnInit {
8992
.subscribe({
9093
next: (result) => {
9194
this.organizations.set(
92-
result['Accounts']
93-
.map((o) => o.metadata.name)
94-
.filter((o) => o !== this.context().organization),
95+
result['Accounts'].map((o) => o.metadata.name),
9596
);
9697
},
9798
});
@@ -121,10 +122,14 @@ export class OrganizationManagementComponent implements OnInit {
121122
]);
122123
this.organizationToSwitch.set(this.newOrganization);
123124
this.newOrganization = '';
124-
this.LuigiClient().uxManager().showAlert({
125-
text: 'New organization has been created, select it from the list to switch to it.',
126-
type: 'info',
127-
});
125+
this.LuigiClient()
126+
.uxManager()
127+
.showAlert({
128+
text: this.getMessageForOrganizationCreation(
129+
this.organizationToSwitch(),
130+
),
131+
type: 'info',
132+
});
128133
},
129134
error: (_error) => {
130135
this.LuigiClient()
@@ -137,6 +142,14 @@ export class OrganizationManagementComponent implements OnInit {
137142
});
138143
}
139144

145+
private getMessageForOrganizationCreation(orgName: string) {
146+
if (isLocalSetup()) {
147+
return `A new organization has 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`;
148+
}
149+
150+
return 'A new organization has been created. Select it from the list to switch.';
151+
}
152+
140153
private readTranslations() {
141154
return {
142155
explanation: this.i18nService.getTranslation(

0 commit comments

Comments
 (0)