All files / src/components/ManualAddCompanyForm ManualAddCompanyForm.tsx

100% Statements 27/27
100% Branches 30/30
100% Functions 3/3
100% Lines 27/27

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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 2353x 3x           3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   8x 9x 8x   8x 8x 8x 8x 8x 8x 8x   8x 8x     8x       1x                                                                                                                                                                                                                                                                                                                                                                                                      
import React, { useRef, useEffect, useState } from 'react';
import { Form, P, useI18nContext } from '@procore/core-react';
import {
  generateSchema,
  generateDefaultValues,
  ManualAddCompanyFormData,
  transformMutateParams,
} from './schema';
import { CountryPicker } from './CountryPicker';
import { RegionPicker } from './RegionPicker';
import { ZipInput } from './ZipInput';
import { FormButtons } from './FormButtons';
import { ModalBody } from '../AddCompanyModal/ModalBody';
import { useSearchContext } from '../../Context/SearchContext';
import { useCreateCompanyVendorsQuery } from '../../hooks/useCreateCompanyVendorsQuery';
import { Loader } from '../Loader';
import { useAddCompanyContext } from '../../Context/AddCompanyContext';
import { PhoneInput } from './PhoneInput';
 
export function ManualAddCompanyForm() {
  const i18n = useI18nContext();
  const [searchTerm] = useSearchContext();
  const { vendorConfigurableFieldSets, companyCountryCode } =
    useAddCompanyContext();
  const { mutate: createCompany, isSuccess } = useCreateCompanyVendorsQuery();
  const [disabled] = useState(false);
  const infoRef = useRef<null | HTMLDivElement>(null);
  const { projectId } = useAddCompanyContext();
  const schema = generateSchema(vendorConfigurableFieldSets);
  const defaultValues = generateDefaultValues(searchTerm, companyCountryCode);
 
  useEffect(() => {
    infoRef.current?.scrollIntoView({ behavior: 'smooth', block: 'end' });
  }, []);
 
  return (
    <Form
      view="create"
      onSubmit={(values: ManualAddCompanyFormData) => {
        createCompany(transformMutateParams(values));
      }}
      initialValues={defaultValues}
      validationSchema={schema}
      disabled={disabled}
      isInitialValid={!!searchTerm}
      validateOnChange
    >
      <Loader isLoading={isSuccess} />
      <ModalBody>
        <Form.Form>
          <P ref={infoRef} style={{ fontStyle: 'italic', marginBottom: 16 }}>
            {projectId
              ? i18n.t(
                  'views.generic.directory.add_company_package.enter_company_information_project_level'
                )
              : i18n.t(
                  'views.generic.directory.add_company_package.enter_company_information'
                )}
          </P>
          <Form.Row>
            <Form.Text
              name="name"
              label={i18n.t(
                'views.generic.directory.add_company_package.company_name'
              )}
              colStart={1}
              colWidth={12}
              placeholder={i18n.t(
                'views.generic.directory.add_company_package.company_name_placeholder'
              )}
            />
          </Form.Row>
          {vendorConfigurableFieldSets.abbreviated_name.visible && (
            <Form.Row>
              <Form.Text
                name="abbreviated_name"
                label={i18n.t(
                  'views.generic.directory.add_company_package.abbreviated_name'
                )}
                colStart={1}
                colWidth={12}
                placeholder={i18n.t(
                  'views.generic.directory.add_company_package.abbreviated_name_placeholder'
                )}
              />
            </Form.Row>
          )}
          {vendorConfigurableFieldSets.trade_name.visible && (
            <Form.Row>
              <Form.Text
                name="trade_name"
                label={i18n.t(
                  'views.generic.directory.add_company_package.dba'
                )}
                colStart={1}
                colWidth={12}
                placeholder={i18n.t(
                  'views.generic.directory.add_company_package.dba_placeholder'
                )}
              />
            </Form.Row>
          )}
          {vendorConfigurableFieldSets.business_phone.visible && (
            <Form.Row>
              <PhoneInput inputType={'business'} />
            </Form.Row>
          )}
          {vendorConfigurableFieldSets.fax_number.visible && (
            <Form.Row>
              <PhoneInput inputType={'fax'} />
            </Form.Row>
          )}
          {vendorConfigurableFieldSets.address.visible && (
            <Form.Row>
              <Form.Text
                name="address"
                label={i18n.t(
                  'views.generic.directory.add_company_package.address'
                )}
                colStart={1}
                colWidth={12}
                placeholder={i18n.t(
                  'views.generic.directory.add_company_package.address_placeholder'
                )}
              />
            </Form.Row>
          )}
          {vendorConfigurableFieldSets.city.visible && (
            <Form.Row>
              <Form.Text
                name="city"
                label={i18n.t(
                  'views.generic.directory.add_company_package.city'
                )}
                colStart={1}
                colWidth={12}
                placeholder={i18n.t(
                  'views.generic.directory.add_company_package.city_placeholder'
                )}
              />
            </Form.Row>
          )}
          {vendorConfigurableFieldSets.country_code.visible && (
            <Form.Row>
              <CountryPicker />
            </Form.Row>
          )}
          <Form.Row>
            {vendorConfigurableFieldSets.country_code.visible && (
              <RegionPicker />
            )}
            {vendorConfigurableFieldSets.zip.visible && <ZipInput />}
          </Form.Row>
          {vendorConfigurableFieldSets.email_address.visible && (
            <Form.Row>
              <Form.Text
                name="email_address"
                label={i18n.t(
                  'views.generic.directory.add_company_package.email_address'
                )}
                colStart={1}
                colWidth={12}
                placeholder={i18n.t(
                  'views.generic.directory.add_company_package.email_address_placeholder'
                )}
              />
            </Form.Row>
          )}
          {vendorConfigurableFieldSets.website.visible && (
            <Form.Row>
              <Form.Text
                name="website"
                label={i18n.t(
                  'views.generic.directory.add_company_package.website'
                )}
                colStart={1}
                colWidth={12}
                placeholder={i18n.t(
                  'views.generic.directory.add_company_package.website_placeholder'
                )}
              />
            </Form.Row>
          )}
          {vendorConfigurableFieldSets.notes.visible && (
            <Form.Row>
              <Form.TextArea
                name="notes"
                label={i18n.t(
                  'views.generic.directory.add_company_package.tags_keywords'
                )}
                colStart={1}
                colWidth={12}
                placeholder={i18n.t(
                  'views.generic.directory.add_company_package.tags_keywords_placeholder'
                )}
              />
            </Form.Row>
          )}
          {vendorConfigurableFieldSets.license_number.visible && (
            <Form.Row>
              <Form.Text
                name="license_number"
                label={i18n.t(
                  'views.generic.directory.add_company_package.license_number'
                )}
                colStart={1}
                colWidth={12}
                placeholder={i18n.t(
                  'views.generic.directory.add_company_package.license_number_placeholder'
                )}
              />
            </Form.Row>
          )}
          {vendorConfigurableFieldSets.labor_union.visible && (
            <Form.Row>
              <Form.Text
                name="labor_union"
                label={i18n.t(
                  'views.generic.directory.add_company_package.labor_union'
                )}
                colStart={1}
                colWidth={12}
                placeholder={i18n.t(
                  'views.generic.directory.add_company_package.labor_union_placeholder'
                )}
              />
            </Form.Row>
          )}
        </Form.Form>
      </ModalBody>
      <FormButtons />
    </Form>
  );
}