@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
48 lines • 2.2 kB
JavaScript
/**
* 2024-09-07: Migrated from the same folder in fps-library-v2/banner/features/FPSWebPartClass/functions/...
*/
import { CurrentPathname } from "../../../components/molecules/source-props/WindowLocationConstants";
// import { CurrentPathname } from "../../../logic/Strings/getSiteCollectionUrlFromLink";
/**
* Based directly from protected _addParamToUrl in FPSBaseClass
* Eventually that could be removed and this could be called directly
*
* @param newParamStr
* @param reRender
* @param newTab
*/
export function setSearchParamToUrl(newParamStr, reRender = true, newTab = false) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const url = new URL(window.location.href); // .href includes search params
const search_params = url.searchParams;
const newParam = newParamStr.split('=');
search_params.set(newParam[0], newParam[1]);
const nextTitle = `addParamToUrl - ${newParamStr}`;
const nextState = { additionalInformation: `Added new paramter to Url and refreshed the page: ${newParam}` };
console.log(`oldWindow`, window.location);
const newUrl = `${CurrentPathname}?${newParamStr === `clearAllParams=true` ? '' : search_params.toString()}`;
window.history.pushState(nextState, nextTitle, newUrl);
console.log(`newWindow`, window.location);
if (reRender === true)
this.render();
if (newTab === true)
window.open(newUrl, '_blank');
}
/**
* Loosely based on rotected _addParamToUrl in FPSBaseClass but modified per bing chat
* @param newParamStr
* @param reRender
*/
export function deleteSearchParamFromUrl(newParamStr, reRender = true) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const url = new URL(window.location.href); // .href includes search params
const search_params = url.searchParams;
const newParam = newParamStr.split('=');
search_params.delete(newParam[0]);
console.log(`oldWindow`, window.location);
window.history.replaceState(null, '', url);
console.log(`newWindow`, window.location);
if (reRender === true)
this.render();
}
//# sourceMappingURL=updateUrlParam.js.map