From 8cc89830e3b4d6f8441a067ede1a294c25e6efac Mon Sep 17 00:00:00 2001 From: Maham Akif <113524403+mahamakifdar19@users.noreply.github.com> Date: Mon, 29 Apr 2024 15:48:45 +0500 Subject: [PATCH] fix: fixed ai curation bugs (#414) Co-authored-by: Maham Akif --- src/components/aiCuration/AskXpert.jsx | 22 +++++++++---------- .../xpertResultCard/XpertResultCard.jsx | 9 +++++--- src/components/catalogs/CatalogSearch.jsx | 5 ++++- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/components/aiCuration/AskXpert.jsx b/src/components/aiCuration/AskXpert.jsx index 651e5b9b..068dba16 100644 --- a/src/components/aiCuration/AskXpert.jsx +++ b/src/components/aiCuration/AskXpert.jsx @@ -48,22 +48,22 @@ const AskXpert = ({ catalogName, onClose, onXpertData }) => { const defaultErrorMessage = 'An error occurred. Please try again.'; if (status < 400 || status === 429) { - if (!XPERT_RESULT_STATUSES.includes(finalResponse.status)) { + if (finalResponse.status && !XPERT_RESULT_STATUSES.includes(finalResponse.status)) { setResults(finalResponse.result); - if (finalResponse.result) { - setShowXpertResultCard(hasNonEmptyValues(finalResponse.result)); - - const aggregationKeys = { - [CONTENT_TYPE_COURSE]: finalResponse?.result?.ocm_courses.map(item => item.aggregation_key), - [EXEC_ED_TITLE]: finalResponse?.result?.exec_ed_courses.map(item => item.aggregation_key), - [CONTENT_TYPE_PROGRAM]: finalResponse?.result?.programs.map(item => item.aggregation_key), - }; - - onXpertData(aggregationKeys); // Pass aggregationKeys to CatalogSearch + if (hasNonEmptyValues(finalResponse.result)) { + setShowXpertResultCard(finalResponse.result); } else { // Handles the scenario where request is successful but no data is returned. setErrorMessage('Hm, I didnt find anything. Try telling me about the subjects, jobs, or skills you are trying to train in your organization.'); } + const aggregationKeys = { + [CONTENT_TYPE_COURSE]: finalResponse?.result?.ocm_courses.map(item => item.aggregation_key), + [EXEC_ED_TITLE]: finalResponse?.result?.exec_ed_courses.map(item => item.aggregation_key), + [CONTENT_TYPE_PROGRAM]: finalResponse?.result?.programs.map(item => item.aggregation_key), + }; + + onXpertData(aggregationKeys); // Pass aggregationKeys to CatalogSearch for Algolia filter + setIsLoading(false); setDelay(null); } diff --git a/src/components/aiCuration/xpertResultCard/XpertResultCard.jsx b/src/components/aiCuration/xpertResultCard/XpertResultCard.jsx index a1f5b922..bac57329 100644 --- a/src/components/aiCuration/xpertResultCard/XpertResultCard.jsx +++ b/src/components/aiCuration/xpertResultCard/XpertResultCard.jsx @@ -1,4 +1,4 @@ -import React, { useState, useMemo } from 'react'; +import React, { useState, useMemo, useEffect } from 'react'; import PropTypes from 'prop-types'; import { Icon, Card, Stack, Form, Button, Spinner, Image, @@ -27,8 +27,10 @@ const XpertResultCard = ({ const debouncedHandleChange = debounce(async (threshold) => { getXpertResultsWithThreshold(taskId, threshold); + }, 1000); - if (xpertResultsData) { + useEffect(() => { + if (hasNonEmptyValues(xpertResultsData)) { setXpertResults(xpertResultsData); const aggregationKeys = { [CONTENT_TYPE_COURSE]: xpertResultsData.ocm_courses?.map(item => item.aggregation_key), @@ -37,7 +39,8 @@ const XpertResultCard = ({ }; onXpertResults(aggregationKeys); } - }, 1000); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [thresholdValue, xpertResultsData]); const handleChange = async (e) => { const threshold = Number(e.target.value); diff --git a/src/components/catalogs/CatalogSearch.jsx b/src/components/catalogs/CatalogSearch.jsx index 674d0959..b33fb024 100644 --- a/src/components/catalogs/CatalogSearch.jsx +++ b/src/components/catalogs/CatalogSearch.jsx @@ -17,6 +17,7 @@ import { import { Image } from '@openedx/paragon'; import { useAlgoliaIndex } from './data/hooks'; import PageWrapper from '../PageWrapper'; +import { getSelectedCatalogFromURL } from '../../utils/common'; import { CONTENT_TYPE_COURSE, @@ -26,6 +27,7 @@ import { NUM_RESULTS_COURSE, NUM_RESULTS_PROGRAM, NUM_RESULTS_PER_PAGE, + QUERY_TITLE_REFINEMENT, } from '../../constants'; import CatalogSearchResults from '../catalogSearchResults/CatalogSearchResults'; import CatalogInfoModal from '../catalogInfoModal/CatalogInfoModal'; @@ -198,12 +200,13 @@ const CatalogSearch = (intl) => { // Take a list of learning types and render a search results component for each item const contentToRender = (items) => { + const selectedCatalog = getSelectedCatalogFromURL(); const itemsWithResultsList = items.map((item) => { let filters = contentData[item].filter; if (features.ENABLE_AI_CURATION) { if (xpertData[item]?.length > 0) { - filters += ` AND (${xpertData[item]?.map(key => `aggregation_key:'${key}'`).join(' OR ')})`; + filters += ` AND ${QUERY_TITLE_REFINEMENT}:"${selectedCatalog}" AND (${xpertData[item]?.map(key => `aggregation_key:'${key}'`).join(' OR ')})`; } else if (xpertData[item]?.length === 0) { filters = 'aggregation_key: null'; }