Skip to content

Commit

Permalink
[DXP-1146] update to new search format, publish components script fix…
Browse files Browse the repository at this point in the history
…, git ignore for generated FE files
  • Loading branch information
pawel-boguski-ds committed Sep 30, 2024
1 parent 778951a commit 74cb004
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def publishComponents() {
RenderingContext.OutputType.FRAGMENT
))
publishRenderingContext(client, "products-tiles", new RenderingContext(
"/apps/puresight/components/templates/carousel.html",
"/apps/puresight/components/templates/tiles.html",
"collected:products:.*",
"/published/puresight/pages/_fragments/{{key}}.tiles.html",
RenderingContext.OutputType.FRAGMENT
Expand Down
7 changes: 7 additions & 0 deletions applications/puresight/frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# These are some examples of commonly ignored file patterns.
# You should customize this list as applicable to your project.
# Learn more about .gitignore:
# https://www.atlassian.com/git/tutorials/saving-changes/gitignore

# Front-end files generated by ui.frontend
/src/main/resources/**/webroot/*
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
getQueenSizeBedResults,
} from "./service";

const PATH_TO_REMOVE = "published/puresight/pages";
const PATH_TO_REMOVE = "/published/puresight/pages";

const getAutocompleteItemUrl = (item: Page) => {
return item.path.replace(PATH_TO_REMOVE, "");
return (item && item.path) ? item.path.replace(PATH_TO_REMOVE, "") : "";
};

const getItemTemplate = (html: HTMLTemplate, item: Page) => {
Expand Down
52 changes: 46 additions & 6 deletions applications/puresight/frontend/src/components/Search/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,28 @@ export interface GetPagesResponse {
executionTimeMs: number;
}

interface ApiResponse {
took: number;
hits: {
hits?: Array<{
_id: string;
_score: number;
_source: {
title: string;
};
highlight?: {
title?: string[];
content?: string[];
};
}>;
};
}

const SEARCH_RESULTS_COUNT = 40;
const PREDEFINED_RESULTS_COUNT = 2;

const buildUrl = (query: string, limit: number) => {
return `/search?limit=${limit}&query=${query}*`;
return `/search/byQuery?size=${limit}&query=${query}`;
};

const getMockResponse = (items: Page[]) => {
Expand All @@ -37,15 +54,38 @@ const getMockResponse = (items: Page[]) => {
};
};

export const getPages = async (query: string) => {
export const getPages = async (query: string): Promise<GetPagesResponse> => {
if (MOCK_BACKEND_ENABLED) {
return new Promise<GetPagesResponse>((resolve) => {
resolve(getMockResponse(MOCK_BACKEND_NO_RESULTS ? [] : ITEMS));
});
}

const response = await fetch(buildUrl(query, SEARCH_RESULTS_COUNT));
return response.json() as Promise<GetPagesResponse>;
return mapToPagesResponse(response);
};

const mapToPagesResponse = async (response: Response): Promise<GetPagesResponse> => {
const jsonResponse: ApiResponse = await response.json();
const items: Page[] = jsonResponse.hits.hits ? jsonResponse.hits.hits.map(mapToPage) : [];
return {
items: items,
executionTimeMs: jsonResponse.took
};
}

const mapToPage = (hit: ApiResponse["hits"]["hits"][0]): Page => {
const path = hit._id;
const score = hit._score;
const title = hit.highlight?.title?.[0] || hit._source.title;
const bestFragment = hit.highlight?.content?.[0] || "";

return {
path,
title,
bestFragment,
score
};
};

export const getCoffeeTableResults = async () => {
Expand All @@ -58,7 +98,7 @@ export const getCoffeeTableResults = async () => {
const response = await fetch(
buildUrl("coffee table", PREDEFINED_RESULTS_COUNT)
);
return response.json() as Promise<GetPagesResponse>;
return mapToPagesResponse(response);
};

export const getBeigeChairResults = async () => {
Expand All @@ -71,7 +111,7 @@ export const getBeigeChairResults = async () => {
const response = await fetch(
buildUrl("beige chair", PREDEFINED_RESULTS_COUNT)
);
return response.json() as Promise<GetPagesResponse>;
return mapToPagesResponse(response);
};

export const getQueenSizeBedResults = async () => {
Expand All @@ -84,5 +124,5 @@ export const getQueenSizeBedResults = async () => {
const response = await fetch(
buildUrl("queen size bed", PREDEFINED_RESULTS_COUNT)
);
return response.json() as Promise<GetPagesResponse>;
return mapToPagesResponse(response);
};

0 comments on commit 74cb004

Please sign in to comment.