Skip to content

Commit

Permalink
fix: search
Browse files Browse the repository at this point in the history
  • Loading branch information
Majorfi committed Dec 19, 2024
1 parent bb8ea77 commit 09f7ccc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
33 changes: 23 additions & 10 deletions app/components/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,19 @@ function TokenListContent({list}: {list: TTokenListItem}): ReactElement {
onChange={(e): void => {
set_search(e.target.value || '');
set_currentPage(1);

const searchParams = new URLSearchParams(window.location.search);

if (!e.target.value) {
const {search, ...queryNoSearch} = params;
search;
router.push(`/${queryNoSearch}`);
searchParams.delete('search');
} else {
router.push(`/${params.toString()}&search=${e.target.value}`);
searchParams.set('search', e.target.value);
}

const newSearch = searchParams.toString();
const currentPath = window.location.pathname;
const newPath = newSearch ? `${currentPath}?${newSearch}` : currentPath;
router.push(newPath);
}}
/>
</div>
Expand All @@ -313,15 +319,22 @@ function TokenListContent({list}: {list: TTokenListItem}): ReactElement {
}
value={network}
onChange={(e): void => {
set_network(Number(e.target.value));
const newNetwork = Number(e.target.value);
set_network(newNetwork);
set_currentPage(1);
if (Number(e.target.value) === -1) {
const {network, ...queryNoNetwork} = params;
network;
router.push(`/${queryNoNetwork}`);

const searchParams = new URLSearchParams(window.location.search);

if (newNetwork === -1) {
searchParams.delete('network');
} else {
router.push(`/${params.toString()}&network=${e.target.value}`);
searchParams.set('network', newNetwork.toString());
}

const newSearch = searchParams.toString();
const currentPath = window.location.pathname;
const newPath = newSearch ? `${currentPath}?${newSearch}` : currentPath;
router.push(newPath);
}}>
<option value={-1}>{'All Networks'}</option>
{availableNetworks.map(
Expand Down
35 changes: 23 additions & 12 deletions app/components/Lists.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import React, {Fragment, useMemo, useState} from 'react';
import {useParams, useRouter} from 'next/navigation';
import {useRouter} from 'next/navigation';
import * as chains from 'wagmi/chains';
import axios from 'axios';
import useSWR from 'swr';
Expand Down Expand Up @@ -45,7 +45,6 @@ export function Filters({
set_search: (value: string) => void;
set_network: (value: number) => void;
}): ReactElement {
const params = useParams();
const router = useRouter();

return (
Expand All @@ -59,13 +58,18 @@ export function Filters({
placeholder={'Search'}
onChange={(e): void => {
set_search(e.target.value || '');
const searchParams = new URLSearchParams(window.location.search);

if (!e.target.value) {
const {search, ...queryNoSearch} = params;
search;
router.push(`/${queryNoSearch}`);
searchParams.delete('search');
} else {
router.push(`/${params.toString()}&search=${e.target.value}`);
searchParams.set('search', e.target.value);
}

const newSearch = searchParams.toString();
const currentPath = window.location.pathname;
const newPath = newSearch ? `${currentPath}?${newSearch}` : currentPath;
router.push(newPath);
}}
/>
</div>
Expand All @@ -76,14 +80,21 @@ export function Filters({
}
value={network}
onChange={(e): void => {
set_network(Number(e.target.value));
if (Number(e.target.value) === -1) {
const {network, ...queryNoNetwork} = params;
network;
router.push(`/${queryNoNetwork}`);
const newNetwork = Number(e.target.value);
set_network(newNetwork);

const searchParams = new URLSearchParams(window.location.search);

if (newNetwork === -1) {
searchParams.delete('network');
} else {
router.push(`/${params.toString()}&network=${e.target.value}`);
searchParams.set('network', newNetwork.toString());
}

const newSearch = searchParams.toString();
const currentPath = window.location.pathname;
const newPath = newSearch ? `${currentPath}?${newSearch}` : currentPath;
router.push(newPath);
}}>
<option value={-1}>{'All Networks'}</option>
{allSupportedChains.map(
Expand Down
2 changes: 1 addition & 1 deletion next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.

0 comments on commit 09f7ccc

Please sign in to comment.