diff --git a/src/apps/companies/apps/add-company/client/CompanySearchStep.jsx b/src/apps/companies/apps/add-company/client/CompanySearchStep.jsx index 8f5cde125fb..cd6b9182438 100644 --- a/src/apps/companies/apps/add-company/client/CompanySearchStep.jsx +++ b/src/apps/companies/apps/add-company/client/CompanySearchStep.jsx @@ -76,6 +76,7 @@ function CompanySearchStep({ name="dnbCompany" country={countryName} entityRenderer={DnbCompanyRenderer} + csrfToken={csrfToken} onCannotFind={() => { // The CompanyNotFoundStep where the user manually adds a company to Data Hub via // a form was being skipped altogether throwing out the sequencing of form steps. diff --git a/src/client/components/Form/elements/FieldCompanyDnBTypeahead/index.jsx b/src/client/components/Form/elements/FieldCompanyDnBTypeahead/index.jsx new file mode 100644 index 00000000000..67f10c155f2 --- /dev/null +++ b/src/client/components/Form/elements/FieldCompanyDnBTypeahead/index.jsx @@ -0,0 +1,80 @@ +import React from 'react' +import PropTypes from 'prop-types' +import { throttle, compact, isEmpty } from 'lodash' +import axios from 'axios' + +import { getCompanyAddress } from '../../../../utils/addresses' +import FieldTypeahead from '../FieldTypeahead' + +function getTradingNames(dnb_company) { + return isEmpty(dnb_company.trading_names) + ? null + : { + label: 'Trading name(s)', + value: dnb_company.trading_names.join(', '), + } +} + +function getAddress(dnb_company) { + return { + label: 'Location at', + value: getCompanyAddress(dnb_company), + } +} + +const FieldCompanyDnBTypeahead = ({ + name, + label, + required, + isMulti, + placeholder = 'Type to search for companies', + loadOptions, + csrfToken, + ...props +}) => { + return ( + { + if (searchString.length > 2) { + return axios + .post(`/companies/create/dnb/company-search?_csrf=${csrfToken}`, { + search_term: searchString, + address_country: 'GB', + }) + .then(({ data }) => + data.results.map((result) => ({ + chipLabel: result.dnb_company.primary_name, + label: result.dnb_company.primary_name, + value: result.dnb_company.duns_number, + id: result.dnb_company.duns_number, + meta: compact([ + getTradingNames(result.dnb_company), + getAddress(result.dnb_company), + ]), + data: result, + })) + ) + } else { + return Promise.resolve([]) + } + }, 500)} + isMulti={false} + {...props} + /> + ) +} + +FieldCompanyDnBTypeahead.propTypes = { + name: PropTypes.string.isRequired, + label: PropTypes.string.isRequired, + required: PropTypes.string, + isMulti: PropTypes.bool, + placeholder: PropTypes.string, +} + +export default FieldCompanyDnBTypeahead diff --git a/src/client/components/Form/elements/FieldDnbCompany/index.jsx b/src/client/components/Form/elements/FieldDnbCompany/index.jsx index ac4b790f44b..196df01ffb1 100644 --- a/src/client/components/Form/elements/FieldDnbCompany/index.jsx +++ b/src/client/components/Form/elements/FieldDnbCompany/index.jsx @@ -17,6 +17,7 @@ import StatusMessage from '../../../StatusMessage' import FieldWrapper from '../FieldWrapper' import FieldUneditable from '../FieldUneditable' import FieldInput from '../FieldInput' +import FieldCompanyDnBTypeahead from '../FieldCompanyDnBTypeahead' import ButtonLink from '../../../ButtonLink' import useEntitySearch from '../../../EntityList/useEntitySearch' import useDnbSearch from '../../../EntityList/useDnbSearch' @@ -56,6 +57,7 @@ const FieldDnbCompany = ({ queryParams = {}, entityRenderer, onCannotFind, + csrfToken, searchResultsMessage = SEARCH_RESULTS_MESSAGE, features, }) => { @@ -97,6 +99,15 @@ const FieldDnbCompany = ({ )} + +