5180 fichiers 38.5 Mo 0 téléchargements 549 vues mis à jour il y a 8 heures ui
nitro-v36-with-renderer-v36 / src / components / catalog / views / page / common / catalogSearch.helpers.ts
catalogSearch.helpers.ts 2 Ko Télécharger le fichier
import { IFurnitureData } from '@octane/renderer';
import { CatalogType } from '../../../../../api';

export const CATALOG_SEARCH_MIN_LENGTH = 3;
export const CATALOG_SEARCH_DEBOUNCE_MS = 50;
export const CATALOG_SEARCH_RESULT_LIMIT = 400;

export const normalizeCatalogSearchText = (value: string): string =>
    (value || '')
        .toLocaleLowerCase()
        .normalize('NFD')
        .replace(/[\u0300-\u036f]/g, '')
        .replace(/[_-]+/g, ' ')
        .replace(/\s+/g, ' ')
        .trim();

export const shouldRunCatalogSearch = (value: string): boolean => normalizeCatalogSearchText(value).length >= CATALOG_SEARCH_MIN_LENGTH;

export const isCatalogSearchEnterKey = (key: string, value: string): boolean => key === 'Enter' && shouldRunCatalogSearch(value);

const getFurnitureSearchText = (furniture: IFurnitureData): string =>
    normalizeCatalogSearchText([furniture.name, furniture.description, furniture.className, furniture.furniLine].filter((value) => !!value).join(' '));

export const findCatalogFurnitureMatches = (
    furnitureDatas: IFurnitureData[],
    query: string,
    catalogType: string,
    limit: number = CATALOG_SEARCH_RESULT_LIMIT
): { furniture: IFurnitureData[]; furniLines: string[] } => {
    const normalizedQuery = normalizeCatalogSearchText(query);
    const furniture: IFurnitureData[] = [];
    const furniLines = new Set<string>();

    if (!normalizedQuery || !furnitureDatas?.length) return { furniture, furniLines: [] };

    for (const item of furnitureDatas) {
        if (!item) continue;
        if (catalogType === CatalogType.BUILDER && !item.availableForBuildersClub) continue;
        if (catalogType === CatalogType.NORMAL && item.excludeDynamic) continue;
        if (!getFurnitureSearchText(item).includes(normalizedQuery)) continue;

        if (item.furniLine) furniLines.add(item.furniLine);

        const isBuyable = item.purchaseOfferId > -1 || item.rentOfferId > -1;

        if (isBuyable && furniture.length < limit) furniture.push(item);
    }

    return { furniture, furniLines: [...furniLines] };
};
15Sujets
1Dépôts
20Membres
4En ligne
Profile
Inscrit
Dernière visite
Messages
0
Votes positifs
0
Votes négatifs
0

Nous utilisons des cookies pour assurer le bon fonctionnement du site et améliorer votre expérience. en savoir plus

Autoriser