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

Commit

Permalink
chore: extract search and category into hook
Browse files Browse the repository at this point in the history
  • Loading branch information
josephgregoryii committed Jul 11, 2022
1 parent 5afc5b1 commit 89e122f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 98 deletions.
49 changes: 11 additions & 38 deletions src/components/IOBanner.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import React from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/react';
import PropTypes from 'prop-types';
import bannerOverlayRight from '../images/io-banner/banner-style-right.svg';
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,
setSearch,
setIsSearchInputEmpty,
handleSearch,
}) => {
const handleSearchInput = (e) => {
const searchInputValue = e.target.value;
setSearch(searchInputValue);
searchInputValue.length > 0
? setIsSearchInputEmpty(false)
: setIsSearchInputEmpty(true);
const BannerHeaderContent = ({ search, handleSearch }) => {
const updateSearch = (e) => {
const value = e.target.value;
handleSearch(value);
};

return (
Expand Down Expand Up @@ -80,11 +72,10 @@ const BannerHeaderContent = ({
value={search || ''}
placeholder="Search"
onClear={() => {
setSearch('');
setIsSearchInputEmpty(true);
handleSearch('');
}}
onSubmit={(value) => handleSearch(value)}
onChange={handleSearchInput}
onChange={updateSearch}
css={css`
box-shadow: none;
max-width: 816px;
Expand Down Expand Up @@ -134,19 +125,7 @@ const BannerHeaderContent = ({
);
};

BannerHeaderContent.propTypes = {
search: PropTypes.string,
setSearch: PropTypes.func,
setIsSearchInputEmpty: PropTypes.func,
handleSearch: PropTypes.func,
};

const IOBanner = ({
search,
setSearch,
setIsSearchInputEmpty,
handleSearch,
}) => {
const IOBanner = ({ search, handleSearch }) => {
return (
<div
css={css`
Expand Down Expand Up @@ -209,12 +188,7 @@ const IOBanner = ({
loading="lazy"
/>
</div>
<BannerHeaderContent
search={search}
setSearch={setSearch}
setIsSearchInputEmpty={setIsSearchInputEmpty}
handleSearch={handleSearch}
/>
<BannerHeaderContent search={search} handleSearch={handleSearch} />
<div
css={css`
margin-left: auto;
Expand All @@ -240,11 +214,10 @@ const IOBanner = ({
);
};

IOBanner.propTypes = {
BannerHeaderContent.propTypes = {
search: PropTypes.string,
setSearch: PropTypes.func,
setIsSearchInputEmpty: PropTypes.func,
handleSearch: PropTypes.func,
};
IOBanner.propTypes = { search: PropTypes.string, handleSearch: PropTypes.func };

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

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

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

// used to update search and category values
useEffect(() => {
const params = new URLSearchParams(location.search);
const searchParam = params.get('search');
const categoryParam = params.get('category');
const validCategory = CATEGORIES.some((cat) => cat.value === categoryParam);

setSearch(searchParam);
setCategory(categoryParam && validCategory ? categoryParam : '');
if (searchParam || categoryParam) {
tessen.track({
eventName: 'instantObservability',
category: 'QuickstartCatalogSearch',
search: searchParam,
quickstartCategory: categoryParam,
});
}
}, [location.search, tessen]);

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

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

navigate(location.pathname);
}
};

/**
* 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);
}
};

return { search, category, handleSearch, handleCategory };
};

export default useSearchAndCategory;
73 changes: 13 additions & 60 deletions src/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import React, { useEffect, useState } from 'react';

import CATEGORIES from '@data/instant-observability-categories';
import useSearchAndCategory from '@hooks/useSearchAndCategory';
import IOBanner from '@components/IOBanner';
import IOSeo from '@components/IOSeo';
import Overlay from '@components/Overlay';
Expand Down Expand Up @@ -79,35 +80,17 @@ const filterByCategory = (category) => {
};

const QuickstartsPage = ({ data, location }) => {
const tessen = useTessen();

const [search, setSearch] = useState('');
const [category, setCategory] = useState('');
const {
search,
category,
handleCategory,
handleSearch,
} = useSearchAndCategory();

const [isCategoriesOverlayOpen, setIsCategoriesOverlayOpen] = useState(false);
const [isSearchInputEmpty, setIsSearchInputEmpty] = useState(true);
const [isSelectCategory, setIsSelectCategory] = useState(true);
// variable to check if the page load completed
const [loadComplete, setLoadComplete] = useState(false);

useEffect(() => {
const params = new URLSearchParams(location.search);
const searchParam = params.get('search');
const categoryParam = params.get('category');
const validCategory = CATEGORIES.some((cat) => cat.value === categoryParam);

setSearch(searchParam);
setCategory(categoryParam && validCategory ? categoryParam : '');
if (searchParam || categoryParam) {
tessen.track({
eventName: 'instantObservability',
category: 'QuickstartCatalogSearch',
search: searchParam,
quickstartCategory: categoryParam,
});
}
}, [location.search, tessen]);

// mark the value as true, if the page is loaded
useEffect(() => {
setLoadComplete(true);
Expand All @@ -117,30 +100,6 @@ const QuickstartsPage = ({ data, location }) => {
setIsCategoriesOverlayOpen(false);
};

const handleSearch = (value) => {
if (value !== null && value !== undefined) {
const params = new URLSearchParams(location.search);
params.set('search', value);

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

const handleCategory = (value) => {
setIsSelectCategory(true);
if (value !== null && value !== undefined) {
const params = new URLSearchParams(location.search);
params.set('category', value);

navigate(`?${params.toString()}`);
if (value !== '') {
setIsSelectCategory(false);
}
}

closeCategoriesOverlay();
};

const quickstarts = data.allQuickstarts.nodes;

const featuredQuickStarts = quickstarts?.filter((product) =>
Expand Down Expand Up @@ -206,7 +165,7 @@ const QuickstartsPage = ({ data, location }) => {
adaptiveWidth: true,
mobileFirst: true, // necessary for breakpoints to work as expected
prevArrow: (
<button>
<button type="button">
<LeftArrowSVG
className="slick-prev"
css={css`
Expand All @@ -218,7 +177,7 @@ const QuickstartsPage = ({ data, location }) => {
</button>
),
nextArrow: (
<button>
<button type="button">
<RightArrowSVG
className="slick-next"
css={css`
Expand Down Expand Up @@ -287,7 +246,7 @@ const QuickstartsPage = ({ data, location }) => {
const renderGoToTopButton = () => {
return (
<Button
onClick={topFunction}
onClick={() => topFunction()}
css={css`
display: none;
flex-direction: row;
Expand Down Expand Up @@ -318,12 +277,7 @@ const QuickstartsPage = ({ data, location }) => {
location={location}
type="quickstarts"
/>
<IOBanner
search={search}
setSearch={setSearch}
setIsSearchInputEmpty={setIsSearchInputEmpty}
handleSearch={handleSearch}
/>
handleSearch && <IOBanner search={search} handleSearch={handleSearch} />
<div
css={css`
--sidebar-width: 300px;
Expand Down Expand Up @@ -573,7 +527,7 @@ const QuickstartsPage = ({ data, location }) => {
</Overlay>
</div>

{isSelectCategory && isSearchInputEmpty && (
{Boolean(category) && Boolean(!search) && (
<>
{mostPopularQuickStarts.length > 0 && (
<>
Expand Down Expand Up @@ -738,14 +692,13 @@ const QuickstartsPage = ({ data, location }) => {
}
`}
>
{!isSearchInputEmpty && <SuperTiles />}
{Boolean(search) && <SuperTiles />}
{filteredQuickstarts.map((pack) => (
<QuickstartTile key={pack.id} featured={false} {...pack} />
))}
</div>
</div>
</div>

{renderGoToTopButton()}
</>
);
Expand Down

0 comments on commit 89e122f

Please sign in to comment.