Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix search functionality to prevent full page reload #455

Merged
merged 3 commits into from
Feb 6, 2025
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
2 changes: 1 addition & 1 deletion src/app/ror.org/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface SearchParams extends Partial<QueryVar> {
filterQuery?: string
}

export default async function SearchDoiPage ({ searchParams }: Props) {
export default async function SearchOrganizationPage ({ searchParams }: Props) {
const { query, filterQuery, ...variables } = searchParams

// Show examply text if there is no query
Expand Down
16 changes: 14 additions & 2 deletions src/components/Header/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export default function Search() {

const [searchInput, setSearchInput] = useState(searchParams?.get('query')?.toString() || '')

// Update searchInput when URL changes (e.g., back button)
React.useEffect(() => {
const queryParam = searchParams?.get('query')?.toString() || ''
if (searchInput !== queryParam) {
setSearchInput(queryParam)
}
}, [searchParams])

function search(query: string) {
const params = new URLSearchParams(searchParams || {});

Expand All @@ -31,7 +39,7 @@ export default function Search() {
router.push(`${base}?${params.toString()}`);
}

const debounceSearch = useDebouncedCallback(search, 300)
const debounceSearch = useDebouncedCallback(search, 500)

return (
<InputGroup className="flex-nowrap align-items-center">
Expand All @@ -47,7 +55,11 @@ export default function Search() {
className={`form-control ${styles.input}`}
type="text"
/>
<Button type="submit" className={`search-submit ${styles.submit}`} onClick={() => search(searchInput)}>
<Button type="submit" className={`search-submit ${styles.submit}`} onClick={(e) => {
search(searchInput)
e.preventDefault();
}}
>
<FontAwesomeIcon icon={faSearch} />
</Button>
{searchInput !== '' && (
Expand Down
1 change: 0 additions & 1 deletion src/data/queries/searchOrganizationQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ async function convertRORToQueryData(
const runningTotal = (page - 1) * ROR_PER_PAGE + currentPageTotal

// Fetch the Providers map
console.log(providersMap)
const nodes = await Promise.all(
(rorResponse?.items || []).map(org => {
const relatedProvider = providersMap[org.id] || { data: { symbol: '', memberType: '' } }
Expand Down
Loading