16005: Open collection and project in new tab feature added
[arvados-workbench2.git] / src / common / redirect-to.ts
index f5ece21be4e35e2aa563e817549cab364b29cf50..baf44711577816d3fa05490a94ffa7341dde2f8f 100644 (file)
@@ -5,14 +5,22 @@
 import { Config } from './config';
 
 const REDIRECT_TO_KEY = 'redirectTo';
+export const REDIRECT_TO_APPLY_TO_PATH = 'redirectToApplyToPath';
 
 export const storeRedirects = () => {
-    if (window.location.href.indexOf(REDIRECT_TO_KEY) > -1) {
-        const { location: { href }, localStorage } = window;
-        const redirectUrl = href.split(`${REDIRECT_TO_KEY}=`)[1];
+    let redirectUrl;
+    const { location: { href }, localStorage } = window;
+    const applyToPath = href.indexOf(REDIRECT_TO_APPLY_TO_PATH) > -1;
 
-        if (localStorage) {
-            localStorage.setItem(REDIRECT_TO_KEY, redirectUrl);
+    if (href.indexOf(REDIRECT_TO_KEY) > -1) {
+        redirectUrl = href.split(`${REDIRECT_TO_KEY}=`)[1];
+    }
+
+    if (localStorage && redirectUrl) {
+        localStorage.setItem(REDIRECT_TO_KEY, redirectUrl);
+
+        if (applyToPath) {
+            localStorage.setItem(REDIRECT_TO_APPLY_TO_PATH, 'true');
         }
     }
 };
@@ -24,9 +32,18 @@ export const handleRedirects = (token: string, config: Config) => {
     if (localStorage && localStorage.getItem(REDIRECT_TO_KEY)) {
         const redirectUrl = localStorage.getItem(REDIRECT_TO_KEY);
         localStorage.removeItem(REDIRECT_TO_KEY);
+        const applyToPath = localStorage.getItem(REDIRECT_TO_APPLY_TO_PATH);
+
         if (redirectUrl) {
-            const sep = redirectUrl.indexOf("?") > -1 ? "&" : "?";
-            window.location.href = `${keepWebServiceUrl}${redirectUrl}${sep}api_token=${token}`;
+            if (applyToPath === 'true') {
+                localStorage.removeItem(REDIRECT_TO_APPLY_TO_PATH);
+                setTimeout(() => {
+                    window.location.pathname = redirectUrl;
+                }, 0);
+            } else {
+                const sep = redirectUrl.indexOf("?") > -1 ? "&" : "?";
+                window.location.href = `${keepWebServiceUrl}${redirectUrl}${sep}api_token=${token}`;
+            }
         }
     }
 };