Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getCurrentRoute linkmanager #2739

Merged
merged 32 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
294d2d2
Add initial promise structure
ndricimrr Jun 1, 2022
aaf2699
add handshake
ndricimrr Jun 2, 2022
416c7a3
test comment
ndricimrr Jun 2, 2022
e0e125e
add working implementation
ndricimrr Jun 2, 2022
8d309a0
remove comment
ndricimrr Jun 3, 2022
22c3978
Merge branch 'master' into 2724-getcurrentroute-linkmanager
ndricimrr Jun 3, 2022
a85bcd3
Fix issue tests
ndricimrr Jun 3, 2022
e94c253
fix fromContext implemetation
ndricimrr Jun 3, 2022
03b8394
Merge branch 'master' into 2724-getcurrentroute-linkmanager
ndricimrr Jun 7, 2022
a1741fa
Merge branch 'master' into 2724-getcurrentroute-linkmanager
ndricimrr Jun 7, 2022
a5b3f83
Merge branch 'master' into 2724-getcurrentroute-linkmanager
ndricimrr Jun 7, 2022
d2138fc
remove consoles
ndricimrr Jun 8, 2022
18fa8ba
prettier
ndricimrr Jun 8, 2022
2a673f1
prettier 2
ndricimrr Jun 8, 2022
1697000
prettier 3
ndricimrr Jun 8, 2022
b6a8aa0
prettier 4
ndricimrr Jun 8, 2022
ed5a300
prettier 4
ndricimrr Jun 8, 2022
812d9cb
prettier 5
ndricimrr Jun 8, 2022
af63873
prettier 6
ndricimrr Jun 8, 2022
856267e
prettier 7
ndricimrr Jun 8, 2022
e078057
Merge branch 'master' into 2724-getcurrentroute-linkmanager
ndricimrr Jun 8, 2022
eeb3b9f
Merge branch '2724-getcurrentroute-linkmanager' of github.com:SAP/lui…
ndricimrr Jun 8, 2022
e6136f5
Add tests
ndricimrr Jun 8, 2022
5d724bb
Add e2e tests 1
ndricimrr Jun 8, 2022
56b5889
clean ts file
ndricimrr Jun 8, 2022
298e2d8
svelte error
ndricimrr Jun 9, 2022
f9bfbea
Svelte prettier
stanleychh Jun 9, 2022
72827d5
add comments
ndricimrr Jun 9, 2022
790302d
add newline
ndricimrr Jun 9, 2022
9ccc86a
Merge branch 'master' into 2724-getcurrentroute-linkmanager
ndricimrr Jun 14, 2022
8da0af4
Merge branch 'master' into 2724-getcurrentroute-linkmanager
ndricimrr Jun 15, 2022
7be70cd
Merge branch 'master' into 2724-getcurrentroute-linkmanager
ndricimrr Jun 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions client/luigi-client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,17 @@ export declare interface LinkManager {
* LuigiClient.linkManager().preserveQueryParams(false).navigate('/projects/xy/foobar');
*/
preserveQueryParams: (preserve: boolean) => this;

/**
* Gets the luigi route associated with the current micro frontend.
* @returns {promise} a promise which resolves to a String value specifying the current luigi route
* @since NEXTRELEASE
* @example
* LuigiClient.linkManager().getCurrentRoute();
* LuigiClient.linkManager().fromContext('project').getCurrentRoute();
* LuigiClient.linkManager().fromVirtualTreeRoot().getCurrentRoute();
*/
getCurrentRoute : () => Promise<string>;
}

export declare interface StorageManager {
Expand Down
49 changes: 49 additions & 0 deletions client/src/linkManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,53 @@ export class linkManager extends LuigiClientBase {
this.options.preserveQueryParams = preserve;
return this;
}

/**
* Gets the luigi route associated with the current micro frontend.
* @returns {promise} a promise which resolves to a String value specifying the current luigi route
* @since NEXTRELEASE
* @example
* LuigiClient.linkManager().getCurrentRoute();
* LuigiClient.linkManager().fromContext('project').getCurrentRoute();
* LuigiClient.linkManager().fromVirtualTreeRoot().getCurrentRoute();
*/
getCurrentRoute() {
const currentId = helpers.getRandomId();

const currentRoutePromise = this.getPromise('getCurrentRoute') || {};
currentRoutePromise[currentId] = {
resolveFn: function() {},
then: function(resolveFn) {
this.resolveFn = resolveFn;
}
};

this.setPromise('getCurrentRoute', currentRoutePromise);

helpers.addEventListener(
'luigi.navigation.currentRoute.answer', (e, listenerId) => {

const data = e.data.data;
const currentRoutePromise = this.getPromise('getCurrentRoute') || {};

if (data.correlationId === currentId ) {
if (currentRoutePromise[data.correlationId]) {
currentRoutePromise[data.correlationId].resolveFn(data.route);
delete currentRoutePromise[data.correlationId];
this.setPromise('getCurrentRoute', currentRoutePromise);
}
helpers.removeEventListener(listenerId);
}
helpers.removeEventListener(listenerId);
});

helpers.sendPostMessageToLuigiCore({
msg: 'luigi.navigation.currentRoute',
data: Object.assign(this.options, {
id: currentId,
})
});

return currentRoutePromise[currentId];
}
}
62 changes: 54 additions & 8 deletions core/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@

const buildPath = (params, srcNode, srcNodePathParams) => {
const localNode = srcNode || currentNode;
const localPathParams = srcNodePathParams || pathParams;
const localPathParams = GenericHelpers.isEmptyObject(srcNodePathParams)
? pathParams
: srcNodePathParams;
let localNavPath = navigationPath;
if (srcNode) {
let parent = srcNode.parent;
Expand All @@ -441,6 +443,21 @@
getSubPath(node, localPathParams),
params.link
);

// boolean predicate, changes the path only if getCurrentPath function used
const isGetCurrentPathRequired =
!GenericHelpers.isEmptyObject(localPathParams) &&
!path.includes('virtualSegment_') &&
!params.link &&
params.getCurrentPath &&
Object.keys(localPathParams)[0].includes('virtualSegment_');
if (isGetCurrentPathRequired) {
let virtualPath = '';
Object.entries(localPathParams).forEach((virtualParam) => {
virtualPath += '/' + virtualParam[1];
});
path = virtualPath;
}
} else if (params.fromParent) {
// from direct parent
path = Routing.concatenatePath(
Expand All @@ -462,10 +479,11 @@
const node = [...localNavPath]
.reverse()
.find((n) => navigationContext === n.navigationContext);
path = Routing.concatenatePath(
getSubPath(node, localPathParams),
params.link
);
const pathUpToContext = getSubPath(node, localPathParams);
const fullPath = getSubPath(localNode, localPathParams);
path = params.getCurrentPath
? fullPath.substring(pathUpToContext.length)
: Routing.concatenatePath(pathUpToContext, params.link);
} else if (params.intent) {
path = RoutingHelpers.getIntentPath(params.link);
} else if (params.relative) {
Expand All @@ -474,6 +492,11 @@
getSubPath(localNode, localPathParams),
params.link
);
} else {
// retrieve path for getCurrentPath method when no options used
if (params.getCurrentPath) {
path = getSubPath(localNode, localPathParams);
}
}
if (params.nodeParams && Object.keys(params.nodeParams).length > 0) {
path += path.includes('?') ? '&' : '?';
Expand Down Expand Up @@ -1291,20 +1314,24 @@
const isSpecial = newTab || modal || splitView || drawer;

const resolveRemotePromise = () => {
const remotePromise = GenericHelpers.getRemotePromise(e.data.remotePromiseId);
const remotePromise = GenericHelpers.getRemotePromise(
e.data.remotePromiseId
);
if (remotePromise) {
remotePromise.doResolve();
}
};

const rejectRemotePromise = () => {
const remotePromise = GenericHelpers.getRemotePromise(e.data.remotePromiseId);
const remotePromise = GenericHelpers.getRemotePromise(
e.data.remotePromiseId
);
if (remotePromise) {
remotePromise.doReject();
}
};

const checkResolve = checkLocationChange => {
const checkResolve = (checkLocationChange) => {
if (!checkLocationChange || previousUrl !== window.location.href) {
resolveRemotePromise();
} else {
Expand Down Expand Up @@ -1428,6 +1455,25 @@
}
}

// handle getCurrentRoute message coming from client
if ('luigi.navigation.currentRoute' === e.data.msg) {
const srcNode = iframe.luigi.currentNode;
const srcPathParams = iframe.luigi.pathParams;
const data = e.data.data;
data.getCurrentPath = true;
const path = buildPath(data, srcNode, srcPathParams);

// send answer back to client
const message = {
msg: 'luigi.navigation.currentRoute.answer',
data: {
route: path,
correlationId: data.id,
},
};
IframeHelpers.sendMessageToIframe(iframe, message);
}

if ('luigi.auth.tokenIssued' === e.data.msg) {
sendAuthDataToClient(e.data.authData);
}
Expand Down
11 changes: 10 additions & 1 deletion core/src/utilities/helpers/generic-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ class GenericHelpersClass {
* @returns {boolean}
*/
isObject(objectToCheck) {
return objectToCheck && typeof objectToCheck === 'object' && !Array.isArray(objectToCheck);
return !!(objectToCheck && typeof objectToCheck === 'object' && !Array.isArray(objectToCheck));
}

/**
* Check if object is empty
* @param item object to check
* @returns {boolean}
*/
isEmptyObject(item) {
return this.isObject(item) && Object.keys(item).length === 0;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions core/test/utilities/helpers/generic-helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ describe('Generic-helpers', () => {
assert.equal(GenericHelpers.isObject(12345), false);
});

it('isEmptyObject', () => {
const obj = { foo: 'bar' };
const obj2 = {};
const obj3 = undefined;
assert.equal(GenericHelpers.isEmptyObject(obj), false);
assert.equal(GenericHelpers.isEmptyObject(obj2), true);
assert.equal(GenericHelpers.isEmptyObject(obj3), false);
});

it('removeInternalProperties', () => {
const input = {
some: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,92 @@ describe('Luigi Client linkManager Webcomponent, Drawer', () => {
});
});
});

describe('getCurrentRoute', () => {
let $iframeBody;
beforeEach(() => {
// "clear" variables to make sure they are not reused and throw error in case something goes wrong
$iframeBody = undefined;
cy.visitLoggedIn('/');
});

it('with fromContext option', () => {
const routeToCheck = '/projects/pr1/users/groups/avengers';
const routeFromContext = '/users/groups/avengers';
cy.visitLoggedIn(routeToCheck);

cy.expectPathToBe(routeToCheck);

cy.getIframeBody().then(result => {
$iframeBody = result;
cy.wrap($iframeBody).contains('This is a dynamic node with');

cy.wrap($iframeBody)
.find('[data-testid="curr-button"]')
.click();

cy.wrap($iframeBody)
.find('[data-testid="curr-button"]')
.click();

cy.wrap($iframeBody)
.find('[data-testid="curr-text"]')
.should('contain', routeFromContext);
});
});

it('with fromVirtualTree option', () => {
const routeToCheck = '/projects/pr1/developers';
const routeVirtual = '/internal/virtualTree';
cy.visitLoggedIn(routeToCheck);
cy.expectPathToBe(routeToCheck);

cy.getIframeBody().then(result => {
$iframeBody = result;
cy.wrap($iframeBody).contains('Developers content.');

cy.wrap($iframeBody)
.find('[data-testid="goToVirtualTree"]')
.click();

cy.expectPathToBe(routeToCheck + routeVirtual);

cy.wrap($iframeBody)
.find('[data-testid="curr-link-virtualtree"]')
.click();

cy.wrap($iframeBody)
.find('[data-testid="curr-link-virtualtree"]')
.click();

cy.wrap($iframeBody)
.find('[data-testid="curr-text-virtualtree"]')
.should('contain', routeVirtual);
});
});

it('with no option', () => {
const routeToCheck = '/projects/pr2/settings';
cy.visitLoggedIn(routeToCheck);
cy.expectPathToBe(routeToCheck);

cy.getIframeBody().then(result => {
$iframeBody = result;
// cy.wrap($iframeBody)
// .contains('Developers content.')

cy.wrap($iframeBody)
.find('[data-testid="curr-link-no-option"]')
.click();

cy.wrap($iframeBody)
.find('[data-testid="curr-link-no-option"]')
.click();

cy.wrap($iframeBody)
.find('[data-testid="curr-text-no-option"]')
.should('contain', routeToCheck);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h3 class="fd-section__title">Developers</h3>
<p>
Developers content.
</p>
<a routerLink="/internal/virtualTree"
<a routerLink="/internal/virtualTree" data-testid="goToVirtualTree"
>Navigate internaly using Angular router and LuigiClient's withoutSync to
/internal/virtualTree</a
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,32 @@ <h3 class="fd-layout-panel__title">Navigate</h3>
</div>
<div class="fd-layout-panel__body">
<ul class="fd-list">
<li class="fd-list__item">
<a
href="javascript:void(0)"
data-testid="curr-link-virtualtree"
class="fd-link"
(click)="getCurrentRouteFromVirtualTree()"
>getCurrentRoute fromVirtualTreeRoot</a
>
<app-code-snippet
data="linkManager().fromVirtualTreeRoot().getCurrentRoute().then(...)"
></app-code-snippet>
<p data-testid="curr-text-virtualtree">{{currentRouteVirtual}}</p>
</li>
<li class="fd-list__item">
<a
href="javascript:void(0)"
data-testid="curr-link-no-option"
class="fd-link"
(click)="getCurrentRoute()"
>getCurrentRoute</a
>
<app-code-snippet
data="linkManager().getCurrentRoute().then(...)"
></app-code-snippet>
<p data-testid="curr-text-no-option">{{currentRoute}}</p>
</li>
<li class="fd-list__item">
<a
href="javascript:void(0)"
Expand Down Expand Up @@ -256,6 +282,7 @@ <h3 class="fd-layout-panel__title">Navigate</h3>
<li class="fd-list__item">
<a
href="javascript:void(0)"
data-testid="fromcontext-settings-link"
class="fd-link"
(click)="
linkManager()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class SettingsComponent implements OnInit {
lcSubscription: Subscription;
preservedViewCallbackContext: any;
private testFeatureToggleActive = false;
currentRouteVirtual: string;
currentRoute: string;

constructor(
private activatedRoute: ActivatedRoute,
Expand Down Expand Up @@ -90,4 +92,21 @@ export class SettingsComponent implements OnInit {
// that will be handed over to previous view
linkManager().goBack(this.callbackValue);
}

getCurrentRouteFromVirtualTree() {
linkManager()
.fromVirtualTreeRoot()
.getCurrentRoute()
.then(route => {
this.currentRouteVirtual = route;
});
}

getCurrentRoute() {
linkManager()
.getCurrentRoute()
.then(route => {
this.currentRoute = route;
});
}
}
Loading