Skip to content

Commit

Permalink
Only update url searchParams on Dashboard media pages
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcauliffe committed Sep 10, 2024
1 parent a391638 commit bb8f9b8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
19 changes: 9 additions & 10 deletions src/common/dataHooks/useMediaSearch.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { useEffect, useState } from 'react'
import { useNavigate, useParams, useSearchParams } from 'react-router-dom'
import { useSearchParams } from 'react-router-dom'
import PropTypes from 'prop-types'

// FPCC
import useSearchLoader from 'common/dataHooks/useSearchLoader'
import useIntersectionObserver from 'common/hooks/useIntersectionObserver'
import { AUDIO, IMAGE, VIDEO, TYPES, SORT, SORT_CREATED_DESC, MEDIA } from 'common/constants'
import { AUDIO, IMAGE, VIDEO, TYPES, SORT, SORT_CREATED_DESC } from 'common/constants'
import { getPathForMediaType } from 'common/utils/mediaHelpers'

function useMediaSearch({ type }) {
const navigate = useNavigate()
const { sitename } = useParams()
const [searchParams] = useSearchParams()
const [searchParams, setSearchParams] = useSearchParams()

const path = getPathForMediaType(type)

Expand Down Expand Up @@ -39,11 +37,11 @@ function useMediaSearch({ type }) {
const handleSearchSubmit = (event) => {
event.preventDefault()
setSearchTerm(searchInputValue)
if (searchInputValue) {
navigate(`/${sitename}/dashboard/${MEDIA}/${path}?q=${searchInputValue}`)
} else {
navigate(`/${sitename}/dashboard/${MEDIA}/${path}`)
}
}

const handleSearchSubmitWithUrlSync = (event) => {
handleSearchSubmit(event)
setSearchParams({ q: searchInputValue })
}

useEffect(() => {
Expand Down Expand Up @@ -71,6 +69,7 @@ function useMediaSearch({ type }) {

return {
handleSearchSubmit,
handleSearchSubmitWithUrlSync,
handleTextFieldChange,
infiniteScroll,
isLoadingEntries: isInitialLoading,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,28 @@ function DashboardMediaAudioContainer() {
searchValue,
currentFile,
setCurrentFile,
handleSearchSubmit,
handleSearchSubmitWithUrlSync,
handleTextFieldChange,
infiniteScroll,
isLoadingEntries,
loadRef,
loadLabel,
} = useMediaSearch({ type: TYPE_AUDIO })

const hasResults = !!(
media?.pages !== undefined && media?.pages?.[0]?.results?.length > 0
)
const hasResults = !!(media?.pages !== undefined && media?.pages?.[0]?.results?.length > 0)

return (
<SearchSelector.Presentation
searchQuery={searchValue}
searchPromptText="Search all audio"
setSearchQuery={handleTextFieldChange}
search={handleSearchSubmit}
search={handleSearchSubmitWithUrlSync}
headerSection=""
resultsSection={
<div className="grid grid-cols-3 w-full">
<main className="col-span-2 pt-4 mx-2">
<section className="p-2 h-full" aria-labelledby="results-header">
<h1
id="results-header"
className="capitalize flex text-2xl font-bold text-fv-charcoal mb-4"
>
<h1 id="results-header" className="capitalize flex text-2xl font-bold text-fv-charcoal mb-4">
Audio
</h1>
<DashboardMediaAudioPresentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function DashboardMediaVisualContainer({ type }) {
searchValue,
currentFile,
setCurrentFile,
handleSearchSubmit,
handleSearchSubmitWithUrlSync,
handleTextFieldChange,
infiniteScroll,
isLoadingEntries,
Expand All @@ -23,25 +23,20 @@ function DashboardMediaVisualContainer({ type }) {
typePlural,
} = useMediaSearch({ type })

const hasResults = !!(
media?.pages !== undefined && media?.pages?.[0]?.results?.length > 0
)
const hasResults = !!(media?.pages !== undefined && media?.pages?.[0]?.results?.length > 0)

return (
<SearchSelector.Presentation
searchQuery={searchValue}
searchPromptText={`Search all ${typePlural}`}
setSearchQuery={handleTextFieldChange}
search={handleSearchSubmit}
search={handleSearchSubmitWithUrlSync}
headerSection=""
resultsSection={
<div className="grid grid-cols-3 w-full">
<main className="col-span-2 pt-4 mx-2">
<section className="p-2 h-full" aria-labelledby="results-header">
<h1
id="results-header"
className="capitalize flex text-2xl font-bold text-fv-charcoal mb-4"
>
<h1 id="results-header" className="capitalize flex text-2xl font-bold text-fv-charcoal mb-4">
{typePlural}
</h1>
<DashboardMediaVisualPresentation
Expand Down

0 comments on commit bb8f9b8

Please sign in to comment.