From 49838a5c5e93d507190e4e495167ff34bb3f5030 Mon Sep 17 00:00:00 2001 From: ChelseaKR Date: Thu, 16 Jan 2025 19:04:11 -0800 Subject: [PATCH] fix deliverytype --- backend/src/domain/search/searchTrainings.ts | 30 ++-------- frontend/src/components/SearchBlock.tsx | 63 +++++++++++++++----- frontend/src/filtering/filterLists.ts | 2 +- frontend/src/locales/en.ts | 3 - frontend/src/locales/es.ts | 3 - 5 files changed, 53 insertions(+), 48 deletions(-) diff --git a/backend/src/domain/search/searchTrainings.ts b/backend/src/domain/search/searchTrainings.ts index f0dd7c814..ca18be447 100644 --- a/backend/src/domain/search/searchTrainings.ts +++ b/backend/src/domain/search/searchTrainings.ts @@ -20,7 +20,7 @@ const cache = new NodeCache({ stdTTL: 300, checkperiod: 120 }); const fetchAllCerts = async (query: object, offset = 0, limit = 10): Promise<{ allCerts: CTDLResource[]; totalResults: number }> => { try { - console.log(`FETCHING RECORD with offset ${offset} and limit ${limit}`); + // console.log(`FETCHING RECORD with offset ${offset} and limit ${limit}`); const response = await credentialEngineAPI.getResults(query, offset, limit); return { allCerts: response.data.data || [], @@ -81,6 +81,7 @@ const filterCerts = async ( services?: string[] ) => { let filteredResults = results; + if (cip_code) { const normalizedCip = normalizeCipCode(cip_code); filteredResults = filteredResults.filter( @@ -110,19 +111,17 @@ const filterCerts = async ( } if (format && format.length > 0) { - // Define a mapping from `format` to `DeliveryType` terms const deliveryTypeMapping: Record = { - "in-person": DeliveryType.InPerson, + "inperson": DeliveryType.InPerson, "online": DeliveryType.OnlineOnly, "blended": DeliveryType.BlendedDelivery, }; // Convert format to the corresponding DeliveryType terms const mappedClassFormats = format - .map(f => deliveryTypeMapping[f.toLowerCase()]) + .map((f) => deliveryTypeMapping[f.toLowerCase() as keyof typeof deliveryTypeMapping]) .filter(Boolean); - // Filter results based on the mapped delivery types filteredResults = filteredResults.filter(result => { const deliveryTypes = result.deliveryTypes || []; @@ -130,7 +129,6 @@ const filterCerts = async ( }); } - if (county) { filteredResults = filteredResults.filter(result => { const zipCodes = result.availableAt?.map(address => address.zipCode).filter(Boolean) || []; @@ -327,26 +325,6 @@ function buildQuery(params: { const isZipCode = zipcodes.lookup(params.searchQuery); const isCounty = Object.keys(zipcodeJson.byCounty).includes(params.searchQuery); - /* const miles = params.miles; - const zipcode = params.zipcode;*/ - /* - let zipcodesList: string[] | zipcodes.ZipCode[] = [] - - if (isZipCode) { - zipcodesList = [params.searchQuery] - } else if (isCounty) { - zipcodesList = zipcodeJson.byCounty[params.searchQuery as keyof typeof zipcodeJson.byCounty] - } - - if (params.county) { - zipcodesList = zipcodeJson.byCounty[params.county as keyof typeof zipcodeJson.byCounty] - } - - if (miles && miles > 0 && zipcode) { - const zipcodesInRadius = zipcodes.radius(zipcode, miles); - zipcodesList = zipcodesInRadius; - }*/ - const queryParts = params.searchQuery.split('+').map(part => part.trim()); const hasMultipleParts = queryParts.length > 1; const [ownedByPart, trainingPart] = queryParts; diff --git a/frontend/src/components/SearchBlock.tsx b/frontend/src/components/SearchBlock.tsx index 20480d93c..555d0dcad 100644 --- a/frontend/src/components/SearchBlock.tsx +++ b/frontend/src/components/SearchBlock.tsx @@ -6,12 +6,15 @@ import { InlineIcon } from "./InlineIcon"; import { ContentfulRichText } from "../types/contentful"; import { ContentfulRichText as RichText } from "./ContentfulRichText"; import { CipDrawerContent } from "./CipDrawerContent"; +import {useTranslation} from "react-i18next"; +import {DeliveryType} from "../domain/Training"; export const SearchBlock = ({ drawerContent }: { drawerContent?: ContentfulRichText }) => { - const [inPerson, setInPerson] = useState(false); + const { t } = useTranslation(); + const [maxCost, setMaxCost] = useState(""); const [miles, setMiles] = useState(""); - const [online, setOnline] = useState(false); + const [deliveryTypes, setDeliveryTypes] = useState>(new Set()); const [zipCode, setZipCode] = useState(""); const [searchTerm, setSearchTerm] = useState(""); const [searchUrl, setSearchUrl] = useState(""); @@ -20,6 +23,18 @@ export const SearchBlock = ({ drawerContent }: { drawerContent?: ContentfulRichT const [socDrawerOpen, setSocDrawerOpen] = useState(false); const [cipDrawerOpen, setCipDrawerOpen] = useState(false); + const toggleDeliveryType = (deliveryType: DeliveryType) => { + setDeliveryTypes((prevTypes) => { + const updatedTypes = new Set(prevTypes); + if (updatedTypes.has(deliveryType)) { + updatedTypes.delete(deliveryType); + } else { + updatedTypes.add(deliveryType); + } + return updatedTypes; + }); + }; + const sanitizedValue = (value: string) => DOMPurify.sanitize(value); const clearAllInputs = () => { @@ -37,10 +52,9 @@ export const SearchBlock = ({ drawerContent }: { drawerContent?: ContentfulRichT select.value = "Miles"; }); // clear state - setInPerson(false); + setDeliveryTypes(new Set()); setMaxCost(""); setMiles(""); - setOnline(false); setZipCode(""); setSearchTerm(""); }; @@ -73,18 +87,27 @@ export const SearchBlock = ({ drawerContent }: { drawerContent?: ContentfulRichT useEffect(() => { const params = []; - const formatArray = []; + + const deliveryTypeMapping: Record = { + [DeliveryType.InPerson]: "inperson", + [DeliveryType.OnlineOnly]: "online", + [DeliveryType.BlendedDelivery]: "blended", + }; + + + const formatArray = Array.from(deliveryTypes) + .map((type) => deliveryTypeMapping[type as keyof typeof deliveryTypeMapping]) + .filter(Boolean); + if (maxCost) params.push(`maxCost=${encodeURIComponent(maxCost)}`); if (miles) params.push(`miles=${encodeURIComponent(miles)}`); if (zipCode) params.push(`zipcode=${encodeURIComponent(zipCode)}`); - if (inPerson) formatArray.push("inperson"); - if (online) formatArray.push("online"); if (formatArray.length > 0) params.push(`format=${formatArray.join(",")}`); const encodedSearchTerm = encodeURIComponent(searchTerm); const url = `/training/search?q=${encodedSearchTerm}${params.length > 0 ? "&" : ""}${params.join("&")}`; setSearchUrl(url); - }, [searchTerm, inPerson, maxCost, miles, online, zipCode]); + }, [searchTerm, deliveryTypes, maxCost, miles, zipCode]); useEffect(() => { if (typeof window !== "undefined") { @@ -243,16 +266,15 @@ export const SearchBlock = ({ drawerContent }: { drawerContent?: ContentfulRichT />
-
Class format
+
{t("SearchResultsPage.classFormatLabel")}
{ - setInPerson(!inPerson); - }} + checked={deliveryTypes.has(DeliveryType.InPerson)} + onChange={() => toggleDeliveryType(DeliveryType.InPerson)} />
+
+ toggleDeliveryType(DeliveryType.BlendedDelivery)} + /> + +
diff --git a/frontend/src/filtering/filterLists.ts b/frontend/src/filtering/filterLists.ts index aa301f504..e17e2cbc2 100644 --- a/frontend/src/filtering/filterLists.ts +++ b/frontend/src/filtering/filterLists.ts @@ -165,4 +165,4 @@ export const classFormatList = [ }, ] -export type ClassFormatProps = "online" | "inperson"; +export type ClassFormatProps = "online" | "inperson" | "blended"; diff --git a/frontend/src/locales/en.ts b/frontend/src/locales/en.ts index aa3b6b8e0..ed86efbed 100644 --- a/frontend/src/locales/en.ts +++ b/frontend/src/locales/en.ts @@ -206,7 +206,6 @@ export const en = { inDemandFilterLabel: "Show In-Demand Trainings Only", costFilterLabel: "Cost", maxCostLabel: "Max Cost", - classFormatFilterLabel: "Class Format", classFormatInPersonLabel: "In-Person", classFormatOnlineLabel: "Online", timeToCompleteFilterLabel: "Time to Complete", @@ -243,8 +242,6 @@ export const en = { searchHelperHeader: "What Can I Search for?", searchHelperText: "Here are some examples that may improve your search results:", boldText1: "Training Providers:", - helperText1: - 'If you\'re searching for a training provider, try using only the provider\'s name and exclude words like "university" or "college".', boldText2: "Occupations:", helperText2: "If you're looking for training for a job, you can type the job directly into the search box.", diff --git a/frontend/src/locales/es.ts b/frontend/src/locales/es.ts index c6d211b6a..1716784a8 100644 --- a/frontend/src/locales/es.ts +++ b/frontend/src/locales/es.ts @@ -216,7 +216,6 @@ export const es = { inDemandFilterLabel: "Mostrar solo capacitaciones bajo demanda", costFilterLabel: "Costo", maxCostLabel: "Costo máximo", - classFormatFilterLabel: "Formato de clase", classFormatInPersonLabel: "En persona", classFormatOnlineLabel: "En línea", timeToCompleteFilterLabel: "Tiempo para completar", @@ -253,8 +252,6 @@ export const es = { searchHelperHeader: "¿Qué puedo buscar?", searchHelperText: "Estos son algunos ejemplos que pueden mejorar sus resultados de búsqueda:", boldText1: "Proveedores de capacitación:", - helperText1: - 'si está buscando un proveedor de capacitación, intente usar solo el nombre del proveedor y excluya palabras como "universidad" o "facultad".', boldText2: "Ocupaciones:", helperText2: "si está buscando capacitación para un trabajo, puede escribir el trabajo directamente en el cuadro de búsqueda.",