Skip to content

Commit

Permalink
fix deliverytype
Browse files Browse the repository at this point in the history
  • Loading branch information
ChelseaKR committed Jan 17, 2025
1 parent 987f169 commit 49838a5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 48 deletions.
30 changes: 4 additions & 26 deletions backend/src/domain/search/searchTrainings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || [],
Expand Down Expand Up @@ -81,6 +81,7 @@ const filterCerts = async (
services?: string[]
) => {
let filteredResults = results;

if (cip_code) {
const normalizedCip = normalizeCipCode(cip_code);
filteredResults = filteredResults.filter(
Expand Down Expand Up @@ -110,27 +111,24 @@ const filterCerts = async (
}

if (format && format.length > 0) {
// Define a mapping from `format` to `DeliveryType` terms
const deliveryTypeMapping: Record<string, DeliveryType> = {
"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 || [];
return mappedClassFormats.some(mappedFormat => deliveryTypes.includes(mappedFormat));
});
}


if (county) {
filteredResults = filteredResults.filter(result => {
const zipCodes = result.availableAt?.map(address => address.zipCode).filter(Boolean) || [];
Expand Down Expand Up @@ -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;
Expand Down
63 changes: 48 additions & 15 deletions frontend/src/components/SearchBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>(false);
const { t } = useTranslation();

const [maxCost, setMaxCost] = useState<string>("");
const [miles, setMiles] = useState<string>("");
const [online, setOnline] = useState<boolean>(false);
const [deliveryTypes, setDeliveryTypes] = useState<Set<DeliveryType>>(new Set());
const [zipCode, setZipCode] = useState<string>("");
const [searchTerm, setSearchTerm] = useState<string>("");
const [searchUrl, setSearchUrl] = useState<string>("");
Expand All @@ -20,6 +23,18 @@ export const SearchBlock = ({ drawerContent }: { drawerContent?: ContentfulRichT
const [socDrawerOpen, setSocDrawerOpen] = useState<boolean>(false);
const [cipDrawerOpen, setCipDrawerOpen] = useState<boolean>(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 = () => {
Expand All @@ -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("");
};
Expand Down Expand Up @@ -73,18 +87,27 @@ export const SearchBlock = ({ drawerContent }: { drawerContent?: ContentfulRichT

useEffect(() => {
const params = [];
const formatArray = [];

const deliveryTypeMapping: Record<string, string> = {
[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") {
Expand Down Expand Up @@ -243,16 +266,15 @@ export const SearchBlock = ({ drawerContent }: { drawerContent?: ContentfulRichT
/>
</div>
<div className="format">
<div className="label">Class format</div>
<div className="label">{t("SearchResultsPage.classFormatLabel")}</div>
<div className="checks">
<div className="usa-checkbox">
<input
className="usa-checkbox__input"
id="in-person"
type="checkbox"
onChange={() => {
setInPerson(!inPerson);
}}
checked={deliveryTypes.has(DeliveryType.InPerson)}
onChange={() => toggleDeliveryType(DeliveryType.InPerson)}
/>
<label className="usa-checkbox__label" htmlFor="in-person">
In-person
Expand All @@ -263,14 +285,25 @@ export const SearchBlock = ({ drawerContent }: { drawerContent?: ContentfulRichT
className="usa-checkbox__input"
id="online"
type="checkbox"
onChange={() => {
setOnline(!online);
}}
checked={deliveryTypes.has(DeliveryType.OnlineOnly)}
onChange={() => toggleDeliveryType(DeliveryType.OnlineOnly)}
/>
<label className="usa-checkbox__label" htmlFor="online">
Online
</label>
</div>
<div className="usa-checkbox">
<input
className="usa-checkbox__input"
id="blended"
type="checkbox"
checked={deliveryTypes.has(DeliveryType.BlendedDelivery)}
onChange={() => toggleDeliveryType(DeliveryType.BlendedDelivery)}
/>
<label className="usa-checkbox__label" htmlFor="blended">
Blended
</label>
</div>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/filtering/filterLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ export const classFormatList = [
},
]

export type ClassFormatProps = "online" | "inperson";
export type ClassFormatProps = "online" | "inperson" | "blended";
3 changes: 0 additions & 3 deletions frontend/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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.",
Expand Down
3 changes: 0 additions & 3 deletions frontend/src/locales/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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.",
Expand Down

0 comments on commit 49838a5

Please sign in to comment.