Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 9 additions & 2 deletions src/helpers/cleanUrlFilters.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
export const cleanUrlFilters = (filters?: Record<string, any>) => {
export const cleanUrlFilters = (
filters?: Record<string, any>,
options?: { cleanBoolean?: boolean }
) => {
if (!filters) {
return {};
}

const { cleanBoolean } = options ?? {};

return Object.entries(filters).reduce(
(a: Record<string, any>, [k, v]) =>
v === undefined || String(v) === '' ? a : ((a[k] = v), a),
v === undefined || String(v) === '' || (cleanBoolean && v === false)
? a
: ((a[k] = v), a),
{}
);
};
16 changes: 13 additions & 3 deletions src/helpers/getValue/getUrlParam.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { stringIsInteger } from 'lib';

interface UrlParamProps {
checkIsInteger?: boolean;
checkIsBoolean?: boolean;
}

export const getUrlParam =
(searchParams: URLSearchParams) =>
(value: string, checkIsInteger?: boolean) => {
(value: string, options?: UrlParamProps) => {
const { checkIsInteger, checkIsBoolean } = options ?? {};
const param =
searchParams.get(value) !== null ? String(searchParams.get(value)) : '';

if (checkIsInteger) {
return stringIsInteger(param) ? parseInt(param) : undefined;
if (checkIsInteger && stringIsInteger(param)) {
return parseInt(param);
}

if (param && checkIsBoolean) {
return param === 'true';
}

return param;
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/urlFilters/useGetBlockFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const useGetBlockFilters = () => {
const getParam = getUrlParam(searchParams);

const filters = {
shard: getParam(BlockFiltersEnum.shard, true)
shard: getParam(BlockFiltersEnum.shard, { checkIsInteger: true })
};

return cleanUrlFilters(filters);
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/urlFilters/useGetEventFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export const useGetEventFilters = () => {
address: getParam('address'),
identifier: getParam('identifier'),
txHash: getParam('txHash'),
shard: getParam('shard', true),
before: getParam(TransactionFiltersEnum.before, true),
after: getParam(TransactionFiltersEnum.after, true)
shard: getParam('shard', { checkIsInteger: true }),
before: getParam(TransactionFiltersEnum.before, { checkIsInteger: true }),
after: getParam(TransactionFiltersEnum.after, { checkIsInteger: true })
};

return cleanUrlFilters(filters);
Expand Down
18 changes: 13 additions & 5 deletions src/hooks/urlFilters/useGetNodeFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ export const useGetNodeFilters = () => {
status: getParam(NodeFiltersEnum.status),
type: getParam(NodeFiltersEnum.type),
identity: getParam(NodeFiltersEnum.identity),
shard: getParam(NodeFiltersEnum.shard, true),
online: getParam(NodeFiltersEnum.online),
shard: getParam(NodeFiltersEnum.shard, { checkIsInteger: true }),
issues: getParam(NodeFiltersEnum.issues),
fullHistory: getParam(NodeFiltersEnum.fullHistory),
isQualified: getParam(NodeFiltersEnum.isQualified),
isAuctioned: getParam(NodeFiltersEnum.isAuctioned),
isAuctionDangerZone: getParam(NodeFiltersEnum.isAuctionDangerZone)
online: getParam(NodeFiltersEnum.online, {
checkIsBoolean: true
}),
isQualified: getParam(NodeFiltersEnum.isQualified, {
checkIsBoolean: true
}),
isAuctioned: getParam(NodeFiltersEnum.isAuctioned, {
checkIsBoolean: true
}),
isAuctionDangerZone: getParam(NodeFiltersEnum.isAuctionDangerZone, {
checkIsBoolean: true
})
};

return cleanUrlFilters(filters);
Expand Down
12 changes: 6 additions & 6 deletions src/hooks/urlFilters/useGetTransactionFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ export const useGetTransactionFilters = () => {
: '';

const senderShard =
getParam(TransactionFiltersEnum.senderShard, true) ??
getParam('sendershard', true);
getParam(TransactionFiltersEnum.senderShard, { checkIsInteger: true }) ??
getParam('sendershard', { checkIsInteger: true });

const receiverShard =
getParam(TransactionFiltersEnum.receiverShard, true) ??
getParam('receivershard', true);
getParam(TransactionFiltersEnum.receiverShard, { checkIsInteger: true }) ??
getParam('receivershard', { checkIsInteger: true });

const filters = {
senderShard,
receiverShard,
sender: getParam(TransactionFiltersEnum.sender),
receiver: getParam(TransactionFiltersEnum.receiver),
before: getParam(TransactionFiltersEnum.before, true),
after: getParam(TransactionFiltersEnum.after, true),
before: getParam(TransactionFiltersEnum.before, { checkIsInteger: true }),
after: getParam(TransactionFiltersEnum.after, { checkIsInteger: true }),
status: checkStatus(status),
miniBlockHash: getParam(TransactionFiltersEnum.miniBlockHash),
method: getParam(TransactionFiltersEnum.method),
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/urlFilters/useGetTransactionInPoolFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export const useGetTransactionInPoolFilters = () => {
const getParam = getUrlParam(searchParams);

const senderShard =
getParam(TransactionFiltersEnum.senderShard, true) ??
getParam('sendershard', true);
getParam(TransactionFiltersEnum.senderShard, { checkIsInteger: true }) ??
getParam('sendershard', { checkIsInteger: true });

const receiverShard =
getParam(TransactionFiltersEnum.receiverShard, true) ??
getParam('receivershard', true);
getParam(TransactionFiltersEnum.receiverShard, { checkIsInteger: true }) ??
getParam('receivershard', { checkIsInteger: true });

const type = checkType(
searchParams.get('type') ? String(searchParams.get('type')) : ''
Expand Down
Loading