All files / src/components/SearchDirectory/SearchDirectoryResults SearchDirectoryResults.tsx

97.43% Statements 38/39
70.37% Branches 19/27
87.5% Functions 7/8
97.43% Lines 38/39

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 2163x 3x                     3x 3x   3x 3x 3x               3x 6x 3x                       3x                     3x 6x 4x   2x     3x       6x 1x   5x 3x   2x     3x       6x 2x   4x 3x   1x     2x         5x 2x   3x 3x           2x         5x                                       1x 1x 1x       1x                                                                                                                                                                            
import React from 'react';
import { ExternalLink } from '@procore/core-icons';
import {
  Card,
  Title,
  Flex,
  Typography,
  Button,
  Link,
  H3,
  spacing,
  useI18nContext,
} from '@procore/core-react';
import { Loader } from '../../../components/Loader';
import { AjaxGetVendorsByNameResponse } from '../../../models/AjaxGetVendorsByNameResponse';
import { useAddCompanyContext } from '../../../Context/AddCompanyContext';
import { useAddToProject } from '../../../hooks/useAddToProject';
import { getStylizedPhoneNumber } from '../../../shared/utilities';
 
export interface AddButtonProps {
  vendorOnProject: boolean;
  i18n: { t: (string: string) => string };
  onSave: () => void;
}
 
export function AddButton({ vendorOnProject, i18n, onSave }: AddButtonProps) {
  if (vendorOnProject) {
    return (
      <Button
        variant="secondary"
        data-pendo="new-add-company-save-from-company-directory"
        disabled
      >
        {i18n.t(
          'views.generic.directory.add_company_package.already_on_project'
        )}
      </Button>
    );
  }
  return (
    <Button
      variant="secondary"
      data-pendo="new-add-company-save-from-company-directory"
      onClick={onSave}
    >
      {i18n.t('views.generic.directory.add_company_package.add_to_project')}
    </Button>
  );
}
 
export function getTopBorderPresence(vendor: AjaxGetVendorsByNameResponse) {
  if (vendor.is_connected) {
    return 'solid';
  }
  return 'none';
}
 
export function getTopBorderColor(
  vendor: AjaxGetVendorsByNameResponse,
  businessId: string
) {
  if (vendor.business_id === businessId) {
    return '#FBF0D0';
  }
  if (vendor.is_connected) {
    return '#E4ECFB';
  }
  return 'none';
}
 
export function getFontColor(
  vendor: AjaxGetVendorsByNameResponse,
  businessId: string
) {
  if (vendor.business_id === businessId) {
    return '#9F3C0D';
  }
  if (vendor.is_connected) {
    return '#1D5CC9';
  }
  return 'none';
}
 
export function getBannerText(
  vendor: AjaxGetVendorsByNameResponse,
  businessId: string,
  i18n: { t: (string: string) => string }
) {
  if (vendor.business_id === businessId) {
    return i18n.t('views.generic.directory.add_company_package.your_company');
  }
  if (vendor.is_connected) {
    return i18n.t(
      'views.generic.directory.add_company_package.connected_company'
    );
  }
}
 
export function getTopBanner(
  vendor: AjaxGetVendorsByNameResponse,
  businessId: string,
  i18n: { t: (string: string) => string }
) {
  return vendor.is_connected ? (
    <Typography
      style={{
        position: 'absolute',
        top: -22,
        fontSize: 12,
        fontWeight: 600,
        letterSpacing: 0.25,
        color: getFontColor(vendor, businessId),
      }}
    >
      {getBannerText(vendor, businessId, i18n)}
    </Typography>
  ) : null;
}
 
interface SearchDirectoryResultsProps {
  data?: AjaxGetVendorsByNameResponse[];
}
 
export function SearchDirectoryResults({ data }: SearchDirectoryResultsProps) {
  const { projectId, companyId, businessId } = useAddCompanyContext();
  const { mutate: addToProject, isSuccess } = useAddToProject(
    companyId,
    projectId as string
  );
  const i18n = useI18nContext();
  return (
    <>
      <Loader isLoading={isSuccess} />
      {data?.map((vendor) => (
        <Card
          key={vendor.id}
          style={{
            position: 'relative',
            padding: spacing.md,
            marginBottom: spacing.lg,
            borderTopStyle: getTopBorderPresence(vendor),
            borderTopColor: getTopBorderColor(vendor, businessId),
            borderTopWidth: 24,
          }}
        >
          {getTopBanner(vendor, businessId, i18n)}
          <Title>
            <Title.Text>
              <H3>{vendor.name?.replace(' (ERP linked)', '')}</H3>
            </Title.Text>
            <Title.Subtext>
              {vendor.trade_name && (
                <span>
                  {i18n.t(
                    'views.generic.directory.add_company_package.doing_business_as'
                  )}{' '}
                  {vendor.trade_name} <br />
                </span>
              )}
            </Title.Subtext>
          </Title>
          <Flex justifyContent="space-between">
            <Flex direction="column" padding="lg 0 lg 0">
              <Typography intent="small">{vendor.address ?? ''}</Typography>
              <Typography intent="small">
                {vendor.city ?? ''}, {vendor.state_code ?? ''}{' '}
                {vendor.zip ?? ''}
              </Typography>
              <Typography intent="small">
                {getStylizedPhoneNumber(vendor.business_phone ?? '')}
              </Typography>
              {vendor.origin_code && (
                <Typography intent="small" style={{ marginTop: 12 }}>
                  {i18n.t('views.generic.directory.add_company_package.erp_id')}
                  # {vendor.origin_code}
                </Typography>
              )}
            </Flex>
            <AddButton
              vendorOnProject={vendor.on_project}
              i18n={i18n}
              onSave={() => {
                vendor.id && addToProject(vendor.id);
              }}
            />
          </Flex>
          {vendor.website && (
            <Flex
              padding="md 0 0 0"
              gap="md"
              style={{
                borderTop: '1px solid #E0E0E0',
              }}
            >
              <Typography intent="small">
                <Link
                  href={vendor.website}
                  target="_blank"
                  rel="noreferrer"
                  data-pendo="new-add-company-external-website-link"
                >
                  {i18n.t(
                    'views.generic.directory.add_company_package.website'
                  )}
                  <span style={{ paddingLeft: 3 }} />
                  <ExternalLink style={{ height: 12, width: 12 }} />
                </Link>
              </Typography>
            </Flex>
          )}
        </Card>
      ))}
    </>
  );
}