|
| 1 | +import { SearchParameters } from '@constructor-io/constructorio-client-javascript/lib/types'; |
| 2 | +import { |
| 3 | + RequestConfigs, |
| 4 | + RequestQueryParams, |
| 5 | + PlpSearchDataResults, |
| 6 | + PlpSearchDataRedirect, |
| 7 | + PlpBrowseData, |
| 8 | +} from '../types'; |
| 9 | +import { removeNullValuesFromObject } from './common'; |
| 10 | +import { isPlpBrowseDataResults, isPlpSearchDataResults } from './typeHelpers'; |
| 11 | + |
| 12 | +export function getSearchParamsFromRequestConfigs(requestConfigs: RequestConfigs): { |
| 13 | + query: string; |
| 14 | + searchParams: SearchParameters; |
| 15 | +} { |
| 16 | + const { query = '', filterValue, filterName, ...rest } = requestConfigs; |
| 17 | + const searchParams = removeNullValuesFromObject(rest); |
| 18 | + |
| 19 | + return { query, searchParams }; |
| 20 | +} |
| 21 | + |
| 22 | +export function getBrowseParamsFromRequestConfigs(requestConfigs: RequestConfigs): { |
| 23 | + filterName: string; |
| 24 | + filterValue: string; |
| 25 | + queryParams: RequestQueryParams; |
| 26 | +} { |
| 27 | + const { query, filterValue = '', filterName = '', ...queryParams } = requestConfigs; |
| 28 | + |
| 29 | + return { filterName, filterValue, queryParams: removeNullValuesFromObject(queryParams) }; |
| 30 | +} |
| 31 | + |
| 32 | +export function checkIsSearchPage(requestConfigs: RequestConfigs) { |
| 33 | + const { query } = getSearchParamsFromRequestConfigs(requestConfigs); |
| 34 | + |
| 35 | + return !!query; |
| 36 | +} |
| 37 | + |
| 38 | +export function checkIsBrowsePage(requestConfigs: RequestConfigs) { |
| 39 | + const { filterName, filterValue } = getBrowseParamsFromRequestConfigs(requestConfigs); |
| 40 | + |
| 41 | + return !!filterName && !!filterValue; |
| 42 | +} |
| 43 | + |
| 44 | +export type PageType = 'search' | 'browse' | 'unknown'; |
| 45 | + |
| 46 | +export function getPageType(requestConfigs: RequestConfigs): PageType { |
| 47 | + if (checkIsSearchPage(requestConfigs)) { |
| 48 | + return 'search'; |
| 49 | + } |
| 50 | + if (checkIsBrowsePage(requestConfigs)) { |
| 51 | + return 'browse'; |
| 52 | + } |
| 53 | + return 'unknown'; |
| 54 | +} |
| 55 | + |
| 56 | +export function getPlpContainerCnstrcDataAttributes( |
| 57 | + data: PlpSearchDataResults | PlpSearchDataRedirect | PlpBrowseData | null, |
| 58 | + requestConfigs: RequestConfigs, |
| 59 | +) { |
| 60 | + if (!data || (!isPlpSearchDataResults(data) && !isPlpBrowseDataResults(data))) return {}; |
| 61 | + |
| 62 | + const { filterName, filterValue } = requestConfigs; |
| 63 | + const pageType = getPageType(requestConfigs); |
| 64 | + let dataCnstrc: Record<`data-cnstrc-${string}`, string | number | boolean> = {}; |
| 65 | + |
| 66 | + switch (pageType) { |
| 67 | + case 'browse': |
| 68 | + dataCnstrc = { |
| 69 | + 'data-cnstrc-browse': true, |
| 70 | + 'data-cnstrc-num-results': data.response.totalNumResults, |
| 71 | + 'data-cnstrc-filter-name': filterName!, |
| 72 | + 'data-cnstrc-filter-value': filterValue!, |
| 73 | + }; |
| 74 | + break; |
| 75 | + |
| 76 | + case 'search': |
| 77 | + dataCnstrc = { |
| 78 | + 'data-cnstrc-search': true, |
| 79 | + 'data-cnstrc-num-results': data.response.totalNumResults, |
| 80 | + }; |
| 81 | + break; |
| 82 | + |
| 83 | + case 'unknown': |
| 84 | + dataCnstrc = {}; |
| 85 | + break; |
| 86 | + |
| 87 | + default: |
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | + return dataCnstrc; |
| 92 | +} |
0 commit comments