diff --git a/frontend/src/app/search/SearchForm.tsx b/frontend/src/app/search/SearchForm.tsx index 68fedfc26..ad9733d05 100644 --- a/frontend/src/app/search/SearchForm.tsx +++ b/frontend/src/app/search/SearchForm.tsx @@ -1,7 +1,5 @@ "use client"; -import React, { useRef } from "react"; - import { ConvertedSearchParams } from "../../types/requestURLTypes"; import { SearchAPIResponse } from "../../types/searchTypes"; import SearchBar from "../../components/search/SearchBar"; @@ -11,8 +9,7 @@ import SearchOpportunityStatus from "../../components/search/SearchOpportunitySt import SearchPagination from "../../components/search/SearchPagination"; import SearchResultsHeader from "../../components/search/SearchResultsHeader"; import SearchResultsList from "../../components/search/SearchResultsList"; -import { updateResults } from "./actions"; -import { useFormState } from "react-dom"; +import { useSearchFormState } from "../../hooks/useSearchFormState"; interface SearchFormProps { initialSearchResults: SearchAPIResponse; @@ -23,34 +20,40 @@ export function SearchForm({ initialSearchResults, requestURLQueryParams, }: SearchFormProps) { - const [searchResults, updateSearchResultsAction] = useFormState( - updateResults, - initialSearchResults, - ); - - const formRef = useRef(null); // allows us to submit form from child components - - const { status, query, sortby, page } = requestURLQueryParams; - - // TODO: move this to server-side calculation? - const maxPaginationError = - searchResults.pagination_info.page_offset > - searchResults.pagination_info.total_pages; + // Capture top level logic, including useFormState in useSearhcFormState hook + const { + searchResults, // result of calling server action + updateSearchResultsAction, // server action function alias + formRef, // used in children to submit the form + maxPaginationError, + statusQueryParams, + queryQueryParams, + sortbyQueryParams, + pageQueryParams, + agencyQueryParams, + fundingInstrumentQueryParams, + } = useSearchFormState(initialSearchResults, requestURLQueryParams); return (