Files
pedwfrontend/lib/routing/routeState.js
T

95 lines
2.1 KiB
JavaScript

export const normalizeRouteStateQuery = (query = {}) => {
const viewAll = query?.va === "true";
const advanced = query?.adv === "true";
const address = query?.ads === "true";
return {
viewAll,
advanced,
address,
key: query?.key
};
};
export const resolveSearchResultsHref = ({
query = {},
hasSession = false,
includeViewAll = true,
fallbackToMyPortalWhenNoFlags = false,
isDnsRoute = false
} = {}) => {
const routeState = normalizeRouteStateQuery(query);
const { viewAll, advanced, address, key } = routeState;
const base = hasSession ? "/myportal" : "";
if (fallbackToMyPortalWhenNoFlags && !viewAll && !advanced && !address) {
return {
pathname: "/myportal"
};
}
if (isDnsRoute) {
return {
pathname: `${base}/dnsapplications`
};
}
if (includeViewAll && viewAll) {
return {
pathname: "/myportal/viewall",
query: { key }
};
}
if (advanced) {
return {
pathname: `${base}/advancedsearchresults`,
query
};
}
if (address) {
return {
pathname: `${base}/addresssearchresults`,
query
};
}
return {
pathname: `${base}/searchresults`,
query
};
};
export const resolveSearchBreadcrumbLabel = ({
query = {},
getViewAllLabel = () => null,
advancedLabel = "",
addressLabel = "",
defaultLabel = ""
} = {}) => {
const routeState = normalizeRouteStateQuery(query);
const { viewAll, advanced, address, key } = routeState;
if (viewAll) {
return getViewAllLabel(key);
}
const keyedLabel = getViewAllLabel(key);
if (keyedLabel) return keyedLabel;
if (advanced) return advancedLabel;
if (address) return addressLabel;
return defaultLabel;
};
export const isDnsRoutePath = (pathname = "") => {
return (
pathname === "/dns" ||
pathname.startsWith("/dns/") ||
pathname === "/myportal/dns" ||
pathname.startsWith("/myportal/dns/")
);
};