All files / src/components/SearchResultCard SearchResultCard.tsx

100% Statements 37/37
85.71% Branches 30/35
100% Functions 10/10
100% Lines 36/36

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 1905x                     5x 5x   5x 5x     3x 27x 27x     2x       22x 10x   12x     3x       27x 9x   18x     3x       27x 4x   23x 5x   18x     2x 48x     2x 75x     5x         29x 10x   19x       3x 27x           18x                   24x 27x 24x                                                                                                                                                                                            
import React from 'react';
import {
  Card,
  Title,
  H3,
  spacing,
  Flex,
  Button,
  Link,
  Typography,
  useI18nContext,
} from '@procore/core-react';
import { ExternalLink } from '@procore/core-icons';
import { PCNResult, PCNAddress } from '../../models/PCNSearchResponse';
import { arrayTextTransform } from '../../shared/utilities';
import { StyledTypography } from '../StyledTypography';
import { getStylizedPhoneNumber } from '../../shared/utilities';
 
export function getPrimaryAddress(addresses: PCNAddress[]) {
  const primaryAddress = addresses.find((address) => address.primary);
  return primaryAddress ?? addresses[0];
}
 
export function getAddButtonText(
  projectId: string | undefined,
  i18n: { t: (string: string) => string }
) {
  if (projectId) {
    return i18n.t('views.generic.directory.add_company_package.add_to_project');
  }
  return i18n.t('views.generic.directory.add_company_package.add');
}
 
export function getTopBorderPresence(
  result: PCNResult,
  currentBusinessId: string
) {
  if (result.vendor_id || result.id === currentBusinessId) {
    return 'solid';
  }
  return 'none';
}
 
export function getTopBorderColor(
  result: PCNResult,
  currentBusinessId: string
) {
  if (isYourCompany(result, currentBusinessId)) {
    return '#FBF0D0';
  }
  if (inCompanyDirectory(result, currentBusinessId)) {
    return '#E4ECFB';
  }
  return 'none';
}
 
export function inCompanyDirectory(result: PCNResult, businessId: string) {
  return result.vendor_id && result.id !== businessId;
}
 
export function isYourCompany(result: PCNResult, businessId: string) {
  return result.id === businessId;
}
 
export function renderAddButton(
  result: PCNResult,
  businessId: string,
  projectId?: string
) {
  if (projectId && result.already_on_project !== true) {
    return true;
  } else {
    return !(isYourCompany(result, businessId) || result.vendor_id);
  }
}
 
export function renderStyledTypography(result: PCNResult, businessId: string) {
  if (
    isYourCompany(result, businessId) ||
    inCompanyDirectory(result, businessId)
  ) {
    return <StyledTypography result={result} businessId={businessId} />;
  }
  return null;
}
 
export interface SearchResultCardProps {
  result: PCNResult;
  businessId: string;
  projectId?: string;
  onSave: () => void;
}
 
export function SearchResultCard(props: SearchResultCardProps) {
  const i18n = useI18nContext();
  const primaryAddress = getPrimaryAddress(
    props.result.addresses
  ) as PCNAddress;
  return (
    <Card
      style={{
        position: 'relative',
        padding: spacing.md,
        marginBottom: spacing.lg,
        borderTopStyle: getTopBorderPresence(props.result, props.businessId),
        borderTopColor: getTopBorderColor(props.result, props.businessId),
        borderTopWidth: 24,
      }}
    >
      {renderStyledTypography(props.result, props.businessId)}
      <Title>
        <Title.Text>
          <H3>{props.result.name}</H3>
        </Title.Text>
        <Title.Subtext>
          {props.result.doing_business_as && (
            <span>
              {i18n.t(
                'views.generic.directory.add_company_package.doing_business_as'
              )}{' '}
              {props.result.doing_business_as} <br />
            </span>
          )}
          <span>{arrayTextTransform(props.result.business_types)}</span>
        </Title.Subtext>
      </Title>
      <Flex
        style={{
          borderBottom: '1px solid #E0E0E0',
        }}
        justifyContent="space-between"
      >
        <Flex direction="column" padding="lg 0 lg 0">
          <Typography intent="small">
            {primaryAddress.address1 ?? ''}
          </Typography>
          <Typography intent="small">
            {primaryAddress.city ?? ''}, {primaryAddress.province ?? ''}{' '}
            {primaryAddress.postal_code1 ?? ''}
          </Typography>
          <Typography intent="small">
            {getStylizedPhoneNumber(primaryAddress.phone_number) ?? ''}
          </Typography>
        </Flex>
        {renderAddButton(props.result, props.businessId, props.projectId) && (
          <Button
            variant="secondary"
            data-pendo="new-add-company-save-company"
            onClick={props.onSave}
          >
            {getAddButtonText(props.projectId, i18n)}
          </Button>
        )}
      </Flex>
      <Flex padding="md 0 0 0" gap="md">
        {props.result.website && (
          <>
            <Typography intent="small">
              <Link
                href={props.result.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>
          </>
        )}
        <Typography intent="small">
          <Link
            href={`https://www.procore.com/network/p/${props.result.primary_slug}`}
            target="_blank"
            rel="noreferrer"
            data-pendo="new-add-company-pcn-website-link"
          >
            {i18n.t(
              'views.generic.directory.add_company_package.business_page'
            )}
            <span style={{ paddingLeft: 3 }} />
            <ExternalLink style={{ height: 12, width: 12 }} />
          </Link>
        </Typography>
      </Flex>
    </Card>
  );
}