Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
feat: move search and category to custom hook
Browse files Browse the repository at this point in the history
  • Loading branch information
josephgregoryii committed Jul 12, 2022
1 parent 89e122f commit 0d6bda0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 53 deletions.
29 changes: 19 additions & 10 deletions src/components/IOBanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import bannerOverlayLeft from '../images/io-banner/banner-style-left.svg';
import { SearchInput } from '@newrelic/gatsby-theme-newrelic';
import { QUICKSTARTS_COLLAPSE_BREAKPOINT } from '@data/constants';

const BannerHeaderContent = ({ search, handleSearch }) => {
const updateSearch = (e) => {
const value = e.target.value;
handleSearch(value);
};

const BannerHeaderContent = ({
search,
updateSearch,
handleSearch,
clearParam,
}) => {
return (
<div
css={css`
Expand Down Expand Up @@ -72,10 +72,11 @@ const BannerHeaderContent = ({ search, handleSearch }) => {
value={search || ''}
placeholder="Search"
onClear={() => {
clearParam('search');
handleSearch('');
}}
onSubmit={(value) => handleSearch(value)}
onChange={updateSearch}
onChange={(e) => updateSearch(e.target.value)}
css={css`
box-shadow: none;
max-width: 816px;
Expand Down Expand Up @@ -125,7 +126,7 @@ const BannerHeaderContent = ({ search, handleSearch }) => {
);
};

const IOBanner = ({ search, handleSearch }) => {
const IOBanner = (props) => {
return (
<div
css={css`
Expand Down Expand Up @@ -188,7 +189,7 @@ const IOBanner = ({ search, handleSearch }) => {
loading="lazy"
/>
</div>
<BannerHeaderContent search={search} handleSearch={handleSearch} />
<BannerHeaderContent {...props} />
<div
css={css`
margin-left: auto;
Expand All @@ -214,10 +215,18 @@ const IOBanner = ({ search, handleSearch }) => {
);
};

IOBanner.propTypes = {
search: PropTypes.string,
updateSearch: PropTypes.func,
handleSearch: PropTypes.func,
clearParam: PropTypes.func,
};

BannerHeaderContent.propTypes = {
search: PropTypes.string,
updateSearch: PropTypes.func,
handleSearch: PropTypes.func,
clearParam: PropTypes.func,
};
IOBanner.propTypes = { search: PropTypes.string, handleSearch: PropTypes.func };

export default IOBanner;
60 changes: 26 additions & 34 deletions src/hooks/useSearchAndCategory.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { useEffect, useState } from 'react';
import { useEffect, useCallback } from 'react';
import { navigate } from 'gatsby';
import { useTessen } from '@newrelic/gatsby-theme-newrelic';
import { navigate, useLocation } from '@reach/router';
import CATEGORIES from '@data/instant-observability-categories';

const useSearchAndCategory = () => {
const useSearchAndCategory = (setSearch, setCategory, location) => {
const tessen = useTessen();
const location = useLocation();

const [search, setSearch] = useState('');
const [category, setCategory] = useState('');

// used to update search and category values
useEffect(() => {
Expand All @@ -17,7 +13,7 @@ const useSearchAndCategory = () => {
const categoryParam = params.get('category');
const validCategory = CATEGORIES.some((cat) => cat.value === categoryParam);

setSearch(searchParam);
setSearch(searchParam || '');
setCategory(categoryParam && validCategory ? categoryParam : '');
if (searchParam || categoryParam) {
tessen.track({
Expand All @@ -27,45 +23,41 @@ const useSearchAndCategory = () => {
quickstartCategory: categoryParam,
});
}
}, [location.search, tessen]);
}, [location.search, tessen, setSearch, setCategory]);

/**
* Updates search parameter from location
* @param {String} value to update search term
* @param {String} parameter to set
* @param {Function => void} callback function to update search term
*/
const handleSearch = (value) => {
if (value !== null && value !== undefined && value !== '') {
const handleParam = (param) => (value) => {
if (value !== null && value !== undefined) {
const params = new URLSearchParams(location.search);
params.set('search', value);

navigate(`?${params.toString()}`);
} else {
const params = new URLSearchParams(location.search);
params.delete('search');
if (value === '') {
params.delete(param);
} else {
params.set(param, value);
}

navigate(location.pathname);
navigate(`?${params.toString()}`);
}
};

/**
* Updates category parameter from location
* @param {String} value to update category term
*/
const handleCategory = (value) => {
if (value !== null && value !== undefined && value !== '') {
const params = new URLSearchParams(location.search);
params.set('category', value);

navigate(`?${params.toString()}`);
} else {
const params = new URLSearchParams(location.search);
params.delete('category');

navigate(location.pathname);
const clearParam = (param) => {
if (param === 'search') {
setSearch('');
}
if (param === 'category') {
setCategory('');
}
};

const updateSearch = (value) => {
setSearch((s) => s + value);
};

return { search, category, handleSearch, handleCategory };
return { handleParam, updateSearch, clearParam };
};

export default useSearchAndCategory;
26 changes: 17 additions & 9 deletions src/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ const filterByCategory = (category) => {
};

const QuickstartsPage = ({ data, location }) => {
const {
search,
category,
handleCategory,
handleSearch,
} = useSearchAndCategory();
const [search, setSearch] = useState('');
const [category, setCategory] = useState('');
const { handleParam, clearParam } = useSearchAndCategory(
setSearch,
setCategory,
location
);

const [isCategoriesOverlayOpen, setIsCategoriesOverlayOpen] = useState(false);
// variable to check if the page load completed
Expand Down Expand Up @@ -277,7 +278,14 @@ const QuickstartsPage = ({ data, location }) => {
location={location}
type="quickstarts"
/>
handleSearch && <IOBanner search={search} handleSearch={handleSearch} />
{handleParam && (
<IOBanner
handleSearch={handleParam('search')}
clearParam={clearParam}
updateSearch={setSearch}
search={search}
/>
)}
<div
css={css`
--sidebar-width: 300px;
Expand Down Expand Up @@ -357,7 +365,7 @@ const QuickstartsPage = ({ data, location }) => {
key={value}
disabled={count === 0}
variant={Button.VARIANT.PRIMARY}
onClick={() => handleCategory(value)}
onClick={() => handleParam('category')(value)}
css={css`
padding: 8px 12px;
font-family: 'Söhne-Leicht';
Expand Down Expand Up @@ -527,7 +535,7 @@ const QuickstartsPage = ({ data, location }) => {
</Overlay>
</div>

{Boolean(category) && Boolean(!search) && (
{!category && !search && (
<>
{mostPopularQuickStarts.length > 0 && (
<>
Expand Down

0 comments on commit 0d6bda0

Please sign in to comment.