All files / src/components/SearchDirectory SearchDirectory.tsx

75.86% Statements 22/29
12.5% Branches 1/8
33.33% Functions 1/3
75.86% Lines 22/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 792x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   4x 6x 4x 4x         4x 4x   4x       4x               4x 4x                                                                                  
import React from 'react';
import { useDebounce } from 'use-debounce';
import { useAjaxGetVendorsByName } from '../../hooks/useAjaxGetVendorsByName';
import { useSearchContext } from '../../Context/SearchContext';
import { ModalBody } from '../AddCompanyModal/ModalBody';
import { SearchDirectoryEmptyState } from './SearchDirectoryEmptyState';
import { SearchDirectoryResults } from './SearchDirectoryResults';
import toast from 'react-hot-toast/headless';
import { Spinner, useI18nContext } from '@procore/core-react';
import { useScreenContext } from '../../Context/ScreenContext';
import { NoCompanySearchResultsFound } from '../SearchDirectory/NoCompanySearchResultsFound';
import { SearchDirectoryFooter } from './SearchDirectoryFooter';
 
export function SearchDirectory() {
  const [searchTerm] = useSearchContext();
  const [debouncedSearchTerm] = useDebounce<string>(searchTerm, 500);
  const i18n = useI18nContext();
  const {
    data,
    isFetching,
    isError: searchError,
  } = useAjaxGetVendorsByName(debouncedSearchTerm);
  const [, setCurrentScreen] = useScreenContext();
 
  Iif (searchError) {
    toast(i18n.t('views.generic.directory.add_company_package.search_error'));
  }
 
  Iif (isFetching) {
    return (
      <Spinner loading={true}>
        <ModalBody> </ModalBody>
      </Spinner>
    );
  }
 
  if (!data) {
    return (
      <>
        <ModalBody>
          <SearchDirectoryEmptyState />
        </ModalBody>
        <SearchDirectoryFooter />
      </>
    );
  } else Eif (data && data.length >= 1) {
    return (
      <>
        <ModalBody>
          <SearchDirectoryResults data={data} />
        </ModalBody>
        <SearchDirectoryFooter />
      </>
    );
  } else {
    return (
      <>
        <ModalBody>
          <NoCompanySearchResultsFound
            setManuallyAddCompany={() =>
              setCurrentScreen({
                currentScreen: 'ManuallyAdd',
                previousScreen: 'SearchDirectory',
              })
            }
            setSearchPCN={() =>
              setCurrentScreen({
                currentScreen: 'SearchPCN',
                previousScreen: 'SearchDirectory',
              })
            }
          />
        </ModalBody>
        <SearchDirectoryFooter />
      </>
    );
  }
}