import { getFeatures, ThirdPartyAPIEntryProps, } from "@tradetrust-tt/address-identity-resolver"; import React, { FunctionComponent, useState } from "react"; import { Edit, Trash2 } from "react-feather"; import isEmpty from "validator/lib/isEmpty"; import isURL from "validator/lib/isURL"; import { Input } from "../../UI/Input"; import { LoaderSpinner } from "../../UI/Loader"; import { useOverlayContext } from "../../../common/context/OverlayContext"; import { AddResolverConfirmation } from "../../UI/Overlay/OverlayContent"; import { Button } from "../../UI/Button"; interface EndpointEntryProps { orderNumber: number; api: string; name: string; apiHeader: string; apiKey: string; canEdit: boolean; removeEndpoint: () => void; onMoveEntryUp?: () => void; onMoveEntryDown?: () => void; onUpdateEndpoint: (newValues: ThirdPartyAPIEntryProps) => void; isEndpointUrlExists: (endpoint: string) => boolean; } export const EndpointEntry: FunctionComponent = ({ orderNumber, removeEndpoint, api, name, apiHeader, apiKey, canEdit, onMoveEntryUp, onMoveEntryDown, onUpdateEndpoint, isEndpointUrlExists, }: EndpointEntryProps) => { const [isLoading, setIsLoading] = useState(false); const [isEditable, setEditable] = useState(canEdit); const [inputErrorMessageName, setInputErrorMessageName] = useState(""); const [inputErrorMessageEndpoint, setInputErrorMessageEndpoint] = useState(""); const [inputErrorMessageApiHeader, setInputErrorMessageApiHeader] = useState(""); const [inputErrorMessageApiKey, setInputErrorMessageApiKey] = useState(""); const [endpointApi, setEndpointApi] = useState(api); const [endpointName, setEndpointName] = useState(name); const [endpointApiHeader, setEndpointApiHeader] = useState(apiHeader); const [endpointApiKey, setEndpointApiKey] = useState(apiKey); const { showOverlay } = useOverlayContext(); const onEndpointApiChanged = ( event: React.ChangeEvent ): void => { setEndpointApi(event.target.value); }; const onEndpointNameChanged = ( event: React.ChangeEvent ): void => { setEndpointName(event.target.value); }; const onEndpointApiHeaderChanged = ( event: React.ChangeEvent ): void => { setEndpointApiHeader(event.target.value); }; const onEndpointApiKeyChanged = ( event: React.ChangeEvent ): void => { setEndpointApiKey(event.target.value); }; const onSetAllError = (msg: string): void => { setInputErrorMessageName(msg); setInputErrorMessageEndpoint(msg); setInputErrorMessageApiHeader(msg); setInputErrorMessageApiKey(msg); }; const canUpdateValue = (): boolean => { const nameTrimmed = endpointName.trim(); const apiTrimmed = endpointApi.trim(); const apiHeaderTrimmed = endpointApiHeader.trim(); const apiKeyTrimmed = endpointApiKey.trim(); setInputErrorMessageName(""); setInputErrorMessageEndpoint(""); setInputErrorMessageApiHeader(""); setInputErrorMessageApiKey(""); if (isEmpty(nameTrimmed)) { setInputErrorMessageName("Name must not be blank."); return false; } if (isEmpty(apiTrimmed)) { setInputErrorMessageEndpoint("Endpoint must not be blank."); return false; } if (!isURL(apiTrimmed)) { setInputErrorMessageEndpoint("Endpoint must be an valid url."); return false; } if (isEndpointUrlExists(apiTrimmed)) { setInputErrorMessageEndpoint("Endpoint already exists."); return false; } if (isEmpty(apiHeaderTrimmed)) { setInputErrorMessageApiHeader("API Header must not be blank."); return false; } if (isEmpty(apiKeyTrimmed)) { setInputErrorMessageApiKey("API Key must not be blank."); return false; } return true; }; const onSave = async (): Promise => { setIsLoading(true); try { if (!canUpdateValue()) { setIsLoading(false); return; } const { features } = await getFeatures( endpointApi, endpointApiHeader, endpointApiKey ); setEditable(false); onUpdateEndpoint({ name: endpointName.trim(), endpoint: endpointApi.trim(), apiHeader: endpointApiHeader.trim(), apiKey: endpointApiKey.trim(), path: { addressResolution: features.addressResolution?.location, entityLookup: features.entityLookup?.location, }, }); showOverlay(); } catch (e) { if (e instanceof Error) { onSetAllError(e.message); } } setIsLoading(false); }; return (

Order

{orderNumber}

Name

{isEditable ? ( ) : ( <>{name} )}

Endpoint

{isEditable ? ( ) : ( <>{api} )}

API Header

{isEditable ? ( ) : ( <>{apiHeader} )}

API Key

{isEditable ? ( ) : ( <>{apiKey} )}
{!isEditable && (
{ setEditable(true); }} data-testid="edit-icon" />
)}
{isEditable && (
{isLoading ? ( ) : ( )}
)}
); };