Skip to content
Snippets Groups Projects
Commit 1ddae254 authored by Lukas's avatar Lukas
Browse files

Update comments search url build function and create global function for all url builders

parent e2ba46ca
No related branches found
No related tags found
1 merge request!499#392_part2_simplify_URL_route_build
import {
convertBooleanArrayToIds,
convertIdsToBooleanArray,
getPageUrlParams,
} from "@/lib/utils";
import { dataToURL } from "@/utils/URLutils";
import Explorer from "@/types/Explorer";
import { setParamIfPositive } from "./globalSearchHelpers";
export async function startCommentSearch(
commentSearchParams: Explorer.CommentSearchParams,
......@@ -27,48 +27,48 @@ export async function startCommentSearch(
}
export function getCommentPageLink(
commentSearchProps: Explorer.CommentSearchProps | undefined,
searchRanges: any
commentSearchProps: Explorer.CommentSearchProps | undefined
): string {
if (!commentSearchProps) return "#";
const urlParams: Explorer.UrlParam[] = [
{
paramName: "fromBlock",
paramValue: dataToURL(commentSearchProps.fromBlock),
},
{ paramName: "toBlock", paramValue: dataToURL(commentSearchProps.toBlock) },
{
paramName: "rangeSelectKey",
paramValue: dataToURL(searchRanges.rangeSelectKey),
},
{
paramName: "lastTime",
paramValue: dataToURL(searchRanges.lastTimeUnitValue),
},
{
paramName: "lastBlocks",
paramValue: dataToURL(searchRanges.lastBlocksValue),
},
{
paramName: "timeUnit",
paramValue: dataToURL(searchRanges.timeUnitSelectKey),
},
{
paramName: "permlink",
paramValue: dataToURL(commentSearchProps.permlink),
},
];
if (commentSearchProps.operationTypes) {
urlParams.push({
paramName: "filters",
paramValue: dataToURL(
convertIdsToBooleanArray(commentSearchProps.operationTypes)
),
});
const {
fromBlock,
toBlock,
permlink,
operationTypes,
accountName,
rangeSelectKey,
lastTimeUnitValue,
lastBlocksValue,
timeUnitSelectKey,
} = commentSearchProps;
const searchParams = new URLSearchParams();
setParamIfPositive(searchParams, "fromBlock", fromBlock);
setParamIfPositive(searchParams, "toBlock", toBlock);
setParamIfPositive(searchParams, "rangeSelectKey", rangeSelectKey);
setParamIfPositive(searchParams, "permlink", permlink);
if (operationTypes) {
setParamIfPositive(
searchParams,
"filters",
convertIdsToBooleanArray(operationTypes)
);
}
return `/comments/@${dataToURL(
commentSearchProps.accountName
)}${getPageUrlParams(urlParams)}`;
if (rangeSelectKey === "lastTime") {
setParamIfPositive(searchParams, "lastTime", lastTimeUnitValue);
setParamIfPositive(searchParams, "timeUnit", timeUnitSelectKey);
} else if (rangeSelectKey === "lastBlocks") {
setParamIfPositive(searchParams, "lastBlocks", lastBlocksValue);
}
const queryString = searchParams.toString();
const urlPath = `/comments/@${dataToURL(accountName)}${
queryString ? `?${queryString}` : ""
}`;
return urlPath;
}
import { dataToURL } from "@/utils/URLutils";
export function setParamIfPositive(
searchParams: URLSearchParams,
key: string,
value: any
) {
const encodedValue = dataToURL(value);
if (!encodedValue) return;
searchParams.set(key, encodedValue);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment