All files / src/hooks useCompanyVendorsSearchQuery.tsx

100% Statements 14/14
100% Branches 1/1
100% Functions 2/2
100% Lines 14/14

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 391x 1x 1x   1x   4x 4x 4x 4x     2x 2x                 2x 1x             1x           4x    
import { request } from '@procore/core-http';
import { useQuery } from '@tanstack/react-query';
import { useI18nContext } from '@procore/core-react';
import { CompanyVendorsSearchResponse } from '../models/CompanyVendorsSearchResponse';
import { useAddCompanyContext } from '../Context/AddCompanyContext';
 
export function useCompanyVendorsSearchQuery(companyName: string) {
  const i18n = useI18nContext();
  const companyId = useAddCompanyContext().companyId;
  const query = useQuery<CompanyVendorsSearchResponse[]>({
    queryKey: ['companyVendorsSearch', companyName, companyId],
    queryFn: async () => {
      const encodedCompanyName = encodeURIComponent(companyName);
      const response = await request(
        `/rest/v1.0/vendors?company_id=${companyId}&per_page=10&filters[search]=${encodedCompanyName}&view=extended`,
        {
          headers: {
            'Procore-Company-ID': companyId.toString(),
          },
        }
      );
 
      if (response.status >= 400) {
        throw new Error(
          i18n.t(
            'views.generic.directory.add_company_package.hook_errors.get_company_vendors_failed'
          )
        );
      }
 
      return response.json();
    },
    enabled: !!companyName,
    retry: false,
    refetchOnMount: false,
  });
  return query;
}