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

Commit

Permalink
chore: move state to hook
Browse files Browse the repository at this point in the history
  • Loading branch information
josephgregoryii committed Jul 12, 2022
1 parent 0d6bda0 commit 5060c4a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
18 changes: 14 additions & 4 deletions src/hooks/useSearchAndCategory.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useEffect, useCallback } from 'react';
import { useState, useEffect } from 'react';
import { navigate } from 'gatsby';
import { useTessen } from '@newrelic/gatsby-theme-newrelic';
import CATEGORIES from '@data/instant-observability-categories';

const useSearchAndCategory = (setSearch, setCategory, location) => {
const useSearchAndCategory = (location) => {
const [search, setSearch] = useState('');
const [category, setCategory] = useState('');
const tessen = useTessen();

// used to update search and category values
Expand All @@ -23,7 +25,7 @@ const useSearchAndCategory = (setSearch, setCategory, location) => {
quickstartCategory: categoryParam,
});
}
}, [location.search, tessen, setSearch, setCategory]);
}, [location.search, tessen]);

/**
* Updates search parameter from location
Expand Down Expand Up @@ -57,7 +59,15 @@ const useSearchAndCategory = (setSearch, setCategory, location) => {
setSearch((s) => s + value);
};

return { handleParam, updateSearch, clearParam };
return {
search,
category,
setSearch,
setCategory,
handleParam,
updateSearch,
clearParam,
};
};

export default useSearchAndCategory;
22 changes: 8 additions & 14 deletions src/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import 'slick-carousel/slick/slick.css';
import 'slick-carousel/slick/slick-theme.css';
import '@components/styles.scss';

import {
Button,
Icon,
Spinner,
useTessen,
} from '@newrelic/gatsby-theme-newrelic';
import { Button, Icon, Spinner } from '@newrelic/gatsby-theme-newrelic';
import React, { useEffect, useState } from 'react';

import CATEGORIES from '@data/instant-observability-categories';
Expand All @@ -22,7 +17,6 @@ import Slider from 'react-slick';
import SuperTiles from '@components/SuperTiles';
import { css } from '@emotion/react';
import { graphql } from 'gatsby';
import { navigate } from '@reach/router';
import LeftArrowSVG from '@components/Icons/LeftArrowSVG';
import RightArrowSVG from '@components/Icons/RightArrowSVG';
import featherIcons from '../@newrelic/gatsby-theme-newrelic/icons/feather';
Expand Down Expand Up @@ -80,13 +74,13 @@ const filterByCategory = (category) => {
};

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

const [isCategoriesOverlayOpen, setIsCategoriesOverlayOpen] = useState(false);
// variable to check if the page load completed
Expand Down Expand Up @@ -481,7 +475,7 @@ const QuickstartsPage = ({ data, location }) => {
type="button"
key={value}
variant={Button.VARIANT.PRIMARY}
onClick={() => handleCategory(value)}
onClick={() => handleParam('category')(value)}
css={css`
width: 100%;
display: flex;
Expand Down

0 comments on commit 5060c4a

Please sign in to comment.