Skip to content

Commit

Permalink
fix: MET-1431 finding 1 fix user choose specific filter search (#1462)
Browse files Browse the repository at this point in the history
* fix: MET-1431 finding 1 fix user choose specific filter to search not redirect to detail page

* Handle case redirect to list when have multiple items

* Handle redirect details on all filter if have only one result

* chore(gha): reverting to github-managed runners

---------

Co-authored-by: Roberto C. Morano <[email protected]>
  • Loading branch information
Sotatek-TruongNguyen4 and rcmorano authored Aug 3, 2023
1 parent c7f2bcc commit b22a426
Showing 1 changed file with 73 additions and 16 deletions.
89 changes: 73 additions & 16 deletions src/components/commons/Layout/Header/HeaderSearch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,21 @@ interface IResponseSearchAll {
epoch?: number;
block?: "string";
tx?: "string";
token?: [
{
name: "string";
fingerprint: "string";
}
];
token?: {
name: "string";
fingerprint: "string";
};
validTokenName?: boolean;
address?: {
address: "string";
stakeAddress: true;
paymentAddress: true;
};
pools?: [
{
name: "string";
poolId: "string";
icon: "string";
}
];
pool?: {
name: "string";
poolId: "string";
icon: "string";
};
validPoolName?: true;
policy?: "string";
}
Expand Down Expand Up @@ -172,6 +168,10 @@ const HeaderSearch: React.FC<Props> = ({ home, callback, setShowErrorMobile, his
setLoading(true);
const res = await defaultAxios.get(API.SEARCH_ALL(query));
setDataSearchAll(res?.data);
const keyDetail = getKeyIfOnlyOneNonNullResult(res?.data);
if (keyDetail) {
handleRedirectDetail(keyDetail, res?.data);
}
setShowOption(true);
setLoading(false);
} catch {
Expand All @@ -180,6 +180,50 @@ const HeaderSearch: React.FC<Props> = ({ home, callback, setShowErrorMobile, his
}
};

const handleRedirectDetail = (key: string, data: IResponseSearchAll) => {
switch (key) {
case "epoch":
history.push(details.epoch(data.epoch as number));
return;
case "block":
history.push(details.block(data.block as string));
return;
case "address":
const address = data.address?.address as string;
if (address.startsWith("stake")) {
history.push(details.stake(address));
return;
}
history.push(details.address(address));
return;
case "token":
history.push(details.token(encodeURIComponent(data?.token?.fingerprint as string)));
return;
case "pool":
history.push(details.delegation(data?.pool?.poolId));
return;
case "tx":
history.push(details.transaction(data?.tx as string));
return;
case "policy":
history.push(details.policyDetail(data?.policy as string));
return;
default:
}
};

const getKeyIfOnlyOneNonNullResult = (data: IResponseSearchAll | undefined) => {
let count = 0;
let keyName = "";
for (const key in data) {
if (!["validTokenName", "validPoolName"].includes(key) && !!(data as any)[key]) {
count++;
keyName = key;
}
}
return count === 1 ? keyName : "";
};

const FetchSearchTokensAndPools = async (query: string, filter: FilterParams) => {
try {
setLoading(true);
Expand All @@ -192,11 +236,24 @@ const HeaderSearch: React.FC<Props> = ({ home, callback, setShowErrorMobile, his
const url = `${
filter === "tokens" ? API.TOKEN.LIST : API.DELEGATION.POOL_LIST
}?page=0&size=${RESULT_SIZE}&${stringify(search)}`;

const res = await defaultAxios.get(url);
setTotalResult(res?.data && res.data?.totalItems ? res.data?.totalItems : 0);
setDataSearchTokensAndPools(res?.data && res?.data?.data ? res?.data?.data : undefined);
setShowOption(true);
if (res?.data && res.data?.totalItems > 0) {
if (filter === "tokens") {
res.data?.totalItems === 1
? history.push(details.token(encodeURIComponent((res?.data?.data[0] as TokensSearch)?.fingerprint)))
: history.push(`${routers.TOKEN_LIST}?tokenName=${(search.query || "").toLocaleLowerCase()}`);
} else {
res.data?.totalItems === 1
? history.push(details.delegation((res?.data?.data[0] as DelegationPool)?.poolId))
: history.push(routers.DELEGATION_POOLS, {
tickerNameSearch: (search.search || "").toLocaleLowerCase()
});
}
} else {
setShowOption(true);
}
setLoading(false);
} catch {
showResultNotFound();
Expand Down Expand Up @@ -435,7 +492,7 @@ export const OptionsSearch = ({
Search for a {filter === "tokens" ? "token" : "pool"}{" "}
{filter === "tokens" ? (
<ValueOption>
{(i as TokensSearch)?.displayName.startsWith("asset") && (i as TokensSearch)?.displayName.length > 43
{(i as TokensSearch)?.displayName?.startsWith("asset") && (i as TokensSearch)?.displayName.length > 43
? getShortWallet((i as TokensSearch)?.fingerprint || "")
: (i as TokensSearch)?.displayName}
</ValueOption>
Expand Down

0 comments on commit b22a426

Please sign in to comment.