diff --git a/src/components/academies/AcademyDetailPage.jsx b/src/components/academies/AcademyDetailPage.jsx index 4096e11f25..412ec52559 100644 --- a/src/components/academies/AcademyDetailPage.jsx +++ b/src/components/academies/AcademyDetailPage.jsx @@ -1,4 +1,4 @@ -import React, { useContext, useMemo } from 'react'; +import React, { useContext } from 'react'; import { Container, Chip, Breadcrumb, Skeleton, Spinner, @@ -7,12 +7,12 @@ import { useParams, Link, } from 'react-router-dom'; import { AppContext } from '@edx/frontend-platform/react'; -import algoliasearch from 'algoliasearch/lite'; import { getConfig } from '@edx/frontend-platform/config'; import { useAcademyMetadata } from './data/hooks'; import CourseCard from './CourseCard'; import NotFoundPage from '../NotFoundPage'; import { ACADEMY_NOT_FOUND_TITLE } from './data/constants'; +import { useAlgoliaSearch } from '../../utils/hooks'; const AcademyDetailPage = () => { const config = getConfig(); @@ -21,17 +21,7 @@ const AcademyDetailPage = () => { const [academy, isAcademyAPILoading, academyAPIError] = useAcademyMetadata(academyUUID); const academyURL = `/${enterpriseConfig.slug}/academy/${academyUUID}`; - // init algolia index - const courseIndex = useMemo( - () => { - const client = algoliasearch( - config.ALGOLIA_APP_ID, - config.ALGOLIA_SEARCH_API_KEY, - ); - return client.initIndex(config.ALGOLIA_INDEX_NAME); - }, - [config.ALGOLIA_APP_ID, config.ALGOLIA_INDEX_NAME, config.ALGOLIA_SEARCH_API_KEY], - ); + const [, courseIndex] = useAlgoliaSearch(config, config.ALGOLIA_INDEX_NAME); if (academyAPIError) { return ( diff --git a/src/components/academies/tests/AcademyDetailPage.test.jsx b/src/components/academies/tests/AcademyDetailPage.test.jsx index 67eae10587..ac1abd9194 100644 --- a/src/components/academies/tests/AcademyDetailPage.test.jsx +++ b/src/components/academies/tests/AcademyDetailPage.test.jsx @@ -74,7 +74,7 @@ getAuthenticatedHttpClient.mockReturnValue(axios); axiosMock.onGet(ACADEMY_API_ENDPOINT).reply(200, ACADEMY_MOCK_DATA); // Mock the 'algoliasearch' module -jest.mock('algoliasearch/lite', () => { +jest.mock('algoliasearch', () => { // Mock the 'initIndex' function const mockInitIndex = jest.fn(() => { // Mock the 'search' function of the index diff --git a/src/components/my-career/CategoryCard.jsx b/src/components/my-career/CategoryCard.jsx index 4e5bb5a66d..9615ea75c2 100644 --- a/src/components/my-career/CategoryCard.jsx +++ b/src/components/my-career/CategoryCard.jsx @@ -1,17 +1,17 @@ import React, { - useContext, useEffect, useMemo, useState, + useContext, useEffect, useState, } from 'react'; import PropTypes from 'prop-types'; import { Button, Card, useToggle } from '@edx/paragon'; import { getConfig } from '@edx/frontend-platform/config'; -import algoliasearch from 'algoliasearch/lite'; import { AppContext } from '@edx/frontend-platform/react'; import LevelBars from './LevelBars'; import SkillsRecommendationCourses from './SkillsRecommendationCourses'; import { UserSubsidyContext } from '../enterprise-user-subsidy'; import { isDisableCourseSearch } from '../enterprise-user-subsidy/enterprise-offers/data/utils'; import { features } from '../../config'; +import { useAlgoliaSearch } from '../../utils/hooks'; const CategoryCard = ({ topCategory }) => { const { skillsSubcategories } = topCategory; @@ -41,16 +41,7 @@ const CategoryCard = ({ topCategory }) => { const config = getConfig(); const { enterpriseConfig } = useContext(AppContext); - const courseIndex = useMemo( - () => { - const client = algoliasearch( - config.ALGOLIA_APP_ID, - config.ALGOLIA_SEARCH_API_KEY, - ); - return client.initIndex(config.ALGOLIA_INDEX_NAME); - }, - [config.ALGOLIA_APP_ID, config.ALGOLIA_INDEX_NAME, config.ALGOLIA_SEARCH_API_KEY], - ); + const [, courseIndex] = useAlgoliaSearch(config, config.ALGOLIA_INDEX_NAME); const filterRenderableSkills = (skills) => { const renderableSkills = []; diff --git a/src/components/search/Search.jsx b/src/components/search/Search.jsx index c372f157e3..d26352b90f 100644 --- a/src/components/search/Search.jsx +++ b/src/components/search/Search.jsx @@ -1,5 +1,5 @@ import React, { - useContext, useMemo, useEffect, + useContext, useEffect, } from 'react'; import { useParams, useHistory } from 'react-router-dom'; import { Helmet } from 'react-helmet'; @@ -9,7 +9,6 @@ import { getConfig } from '@edx/frontend-platform/config'; import { SearchHeader, SearchContext } from '@edx/frontend-enterprise-catalog-search'; import { useToggle, Stack } from '@edx/paragon'; -import algoliasearch from 'algoliasearch/lite'; import { useDefaultSearchFilters, useSearchCatalogs } from './data/hooks'; import { NUM_RESULTS_PER_PAGE, @@ -41,6 +40,7 @@ import AssignmentsOnlyEmptyState from './AssignmentsOnlyEmptyState'; import { LICENSE_STATUS } from '../enterprise-user-subsidy/data/constants'; import { POLICY_TYPES } from '../enterprise-user-subsidy/enterprise-offers/data/constants'; import AuthenticatedPageContext from '../app/AuthenticatedPageContext'; +import { useAlgoliaSearch } from '../../utils/hooks'; const Search = () => { const config = getConfig(); @@ -85,17 +85,7 @@ const Search = () => { const enterpriseUUID = enterpriseConfig.uuid; const { enterpriseCuration: { canOnlyViewHighlightSets } } = useEnterpriseCuration(enterpriseUUID); - const courseIndex = useMemo( - () => { - const client = algoliasearch( - config.ALGOLIA_APP_ID, - config.ALGOLIA_SEARCH_API_KEY, - ); - const cIndex = client.initIndex(config.ALGOLIA_INDEX_NAME); - return cIndex; - }, - [config.ALGOLIA_APP_ID, config.ALGOLIA_INDEX_NAME, config.ALGOLIA_SEARCH_API_KEY], - ); + const [, courseIndex] = useAlgoliaSearch(config, config.ALGOLIA_INDEX_NAME); // If a pathwayUUID exists, open the pathway modal. useEffect(() => { diff --git a/src/components/skills-quiz/SkillsQuizStepper.jsx b/src/components/skills-quiz/SkillsQuizStepper.jsx index f7131c344e..75b2e5853d 100644 --- a/src/components/skills-quiz/SkillsQuizStepper.jsx +++ b/src/components/skills-quiz/SkillsQuizStepper.jsx @@ -1,9 +1,8 @@ /* eslint-disable object-curly-newline */ -import React, { useEffect, useState, useContext, useMemo } from 'react'; +import React, { useEffect, useState, useContext } from 'react'; import { Button, Stepper, ModalDialog, Container, Form, Stack, } from '@edx/paragon'; -import algoliasearch from 'algoliasearch/lite'; import { Configure, InstantSearch } from 'react-instantsearch-dom'; import { getConfig } from '@edx/frontend-platform/config'; import { SearchContext } from '@edx/frontend-enterprise-catalog-search'; @@ -38,6 +37,7 @@ import { import { SkillsContext } from './SkillsContextProvider'; import { SET_KEY_VALUE } from './data/constants'; import { checkValidGoalAndJobSelected } from '../utils/skills-quiz'; +import { useAlgoliaSearch } from '../../utils/hooks'; import TopSkillsOverview from './TopSkillsOverview'; import SkillsQuizHeader from './SkillsQuizHeader'; @@ -48,18 +48,9 @@ import { fetchCourseEnrollments } from './data/service'; const SkillsQuizStepper = () => { const config = getConfig(); const { userId } = getAuthenticatedUser(); - const [searchClient, courseIndex, jobIndex] = useMemo( - () => { - const client = algoliasearch( - config.ALGOLIA_APP_ID, - config.ALGOLIA_SEARCH_API_KEY, - ); - const cIndex = client.initIndex(config.ALGOLIA_INDEX_NAME); - const jIndex = client.initIndex(config.ALGOLIA_INDEX_NAME_JOBS); - return [client, cIndex, jIndex]; - }, - [config.ALGOLIA_APP_ID, config.ALGOLIA_INDEX_NAME, config.ALGOLIA_INDEX_NAME_JOBS, config.ALGOLIA_SEARCH_API_KEY], - ); + const [, courseIndex] = useAlgoliaSearch(config, config.ALGOLIA_INDEX_NAME); + const [jobSearchClient, jobIndex] = useAlgoliaSearch(config, config.ALGOLIA_INDEX_NAME_JOBS); + const [currentStep, setCurrentStep] = useState(STEP1); const [isStudentChecked, setIsStudentChecked] = useState(false); const handleIsStudentCheckedChange = e => setIsStudentChecked(e.target.checked); @@ -198,7 +189,7 @@ const SkillsQuizStepper = () => {