Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { setupServer } from 'msw/node';
import { rest } from 'msw';
import type { SetupServer } from 'msw/node';
import { SWRConfig } from 'swr';
import AddressSearch from '.';
import AddressInput from './address-input';

const DEFAULT_RESPONSE = [
{
Expand All @@ -24,7 +24,7 @@ const DEFAULT_RESPONSE = [
const LOCATIONS_URL = 'https://login.gov/api/locations';
const ADDRESSES_URL = 'https://login.gov/api/addresses';

describe('AddressSearch', () => {
describe('AddressInput', () => {
const sandbox = useSandbox();
context('when an address is found', () => {
let server: SetupServer;
Expand All @@ -45,7 +45,7 @@ describe('AddressSearch', () => {
const handleLocationsFound = sandbox.stub();
const { findByText, findByLabelText } = render(
<SWRConfig value={{ provider: () => new Map() }}>
<AddressSearch
<AddressInput
onFoundAddress={handleAddressFound}
onFoundLocations={handleLocationsFound}
locationsURL={LOCATIONS_URL}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { TextInput } from '@18f/identity-components';
import { useState, useRef, useEffect, useCallback } from 'react';
import { t } from '@18f/identity-i18n';
import ValidatedField from '@18f/identity-validated-field/validated-field';
import SpinnerButton, { SpinnerButtonRefHandle } from '@18f/identity-spinner-button/spinner-button';
import { useDidUpdateEffect } from '@18f/identity-react-hooks';
import useUspsLocations from '../hooks/use-usps-locations';
import type { AddressInputProps } from '../types';

function AddressInput({
registerField = () => undefined,
onFoundAddress = () => undefined,
onFoundLocations = () => undefined,
onLoadingLocations = () => undefined,
onError = () => undefined,
disabled = false,
addressSearchURL,
locationsURL,
}: AddressInputProps) {
const spinnerButtonRef = useRef<SpinnerButtonRefHandle>(null);
const [textInput, setTextInput] = useState('');
const {
locationResults,
uspsError,
addressError,
isLoading,
handleAddressSearch: onSearch,
foundAddress,
validatedFieldRef,
} = useUspsLocations({ locationsURL, addressSearchURL });

const onTextInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { target } = event;
setTextInput(target.value);
};

useEffect(() => {
spinnerButtonRef.current?.toggleSpinner(isLoading);
onLoadingLocations(isLoading);
}, [isLoading]);

useEffect(() => {
addressError && onError(addressError);
uspsError && onError(uspsError);
}, [uspsError, addressError]);

useDidUpdateEffect(() => {
onFoundLocations(locationResults);

foundAddress && onFoundAddress(foundAddress);
}, [locationResults]);

const handleSearch = useCallback(
(event) => {
onError(null);
onSearch(event, textInput);
},
[textInput],
);

return (
<>
<ValidatedField
ref={validatedFieldRef}
messages={{
valueMissing: t('in_person_proofing.body.location.inline_error'),
}}
>
<TextInput
required
ref={registerField('address')}
value={textInput}
onChange={onTextInputChange}
label={t('in_person_proofing.body.location.po_search.address_search_label')}
hint={t('in_person_proofing.body.location.po_search.address_search_hint')}
disabled={disabled}
/>
</ValidatedField>
<div className="margin-y-5">
<SpinnerButton
isWide
isBig
ref={spinnerButtonRef}
type="submit"
onClick={handleSearch}
spinOnClick={false}
actionMessage={t('in_person_proofing.body.location.po_search.is_searching_message')}
longWaitDurationMs={1}
>
{t('in_person_proofing.body.location.po_search.search_button')}
</SpinnerButton>
</div>
</>
);
}

export default AddressInput;
98 changes: 2 additions & 96 deletions app/javascript/packages/address-search/index.tsx
Original file line number Diff line number Diff line change
@@ -1,101 +1,7 @@
import { TextInput } from '@18f/identity-components';
import { useState, useRef, useEffect, useCallback } from 'react';
import { t } from '@18f/identity-i18n';
import ValidatedField from '@18f/identity-validated-field/validated-field';
import SpinnerButton, { SpinnerButtonRefHandle } from '@18f/identity-spinner-button/spinner-button';
import { useDidUpdateEffect } from '@18f/identity-react-hooks';
import useUspsLocations from './hooks/use-usps-locations';
import type { AddressSearchProps } from './types';
import { snakeCase, formatLocations, transformKeys } from './utils';
import InPersonLocations from './components/in-person-locations';
import AddressInput from './components/address-input';

export { snakeCase, formatLocations, transformKeys, InPersonLocations };

function AddressSearch({
registerField = () => undefined,
onFoundAddress = () => undefined,
onFoundLocations = () => undefined,
onLoadingLocations = () => undefined,
onError = () => undefined,
disabled = false,
addressSearchURL,
locationsURL,
}: AddressSearchProps) {
const spinnerButtonRef = useRef<SpinnerButtonRefHandle>(null);
const [textInput, setTextInput] = useState('');
const {
locationResults,
uspsError,
addressError,
isLoading,
handleAddressSearch: onSearch,
foundAddress,
validatedFieldRef,
} = useUspsLocations({ locationsURL, addressSearchURL });

const onTextInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { target } = event;
setTextInput(target.value);
};

useEffect(() => {
spinnerButtonRef.current?.toggleSpinner(isLoading);
onLoadingLocations(isLoading);
}, [isLoading]);

useEffect(() => {
addressError && onError(addressError);
uspsError && onError(uspsError);
}, [uspsError, addressError]);

useDidUpdateEffect(() => {
onFoundLocations(locationResults);

foundAddress && onFoundAddress(foundAddress);
}, [locationResults]);

const handleSearch = useCallback(
(event) => {
onError(null);
onSearch(event, textInput);
},
[textInput],
);

return (
<>
<ValidatedField
ref={validatedFieldRef}
messages={{
valueMissing: t('in_person_proofing.body.location.inline_error'),
}}
>
<TextInput
required
ref={registerField('address')}
value={textInput}
onChange={onTextInputChange}
label={t('in_person_proofing.body.location.po_search.address_search_label')}
hint={t('in_person_proofing.body.location.po_search.address_search_hint')}
disabled={disabled}
/>
</ValidatedField>
<div className="margin-y-5">
<SpinnerButton
isWide
isBig
ref={spinnerButtonRef}
type="submit"
onClick={handleSearch}
spinOnClick={false}
actionMessage={t('in_person_proofing.body.location.po_search.is_searching_message')}
longWaitDurationMs={1}
>
{t('in_person_proofing.body.location.po_search.search_button')}
</SpinnerButton>
</div>
</>
);
}

export default AddressSearch;
export default AddressInput;
2 changes: 1 addition & 1 deletion app/javascript/packages/address-search/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface Location {
address: string;
}

interface AddressSearchProps {
interface AddressInputProps {
registerField?: RegisterFieldCallback;
onFoundAddress?: (address: LocationQuery | null) => void;
onFoundLocations?: (locations: FormattedLocation[] | null | undefined) => void;
Expand Down