import { ThirdPartyAPIEntryProps, useThirdPartyAPIEndpoints, } from "@tradetrust-tt/address-identity-resolver"; import React, { FunctionComponent } from "react"; import { useOverlayContext } from "../../common/context/OverlayContext"; import { DeleteResolverConfirmation } from "../UI/Overlay/OverlayContent"; import { EndpointEntry } from "./EndpointEntry"; export interface AddressesTableProps { isNewEndpoint: boolean; setNewEndpoint: (isNewEndpoint: boolean) => void; } export const AddressesTable: FunctionComponent = ({ isNewEndpoint, setNewEndpoint, }) => { const { thirdPartyAPIEndpoints, addThirdPartyAPIEndpoint, removeThirdPartyAPIEndpoint, setThirdPartyAPIEndpoints, } = useThirdPartyAPIEndpoints(); const { showOverlay, setOverlayVisible } = useOverlayContext(); const deleteAddress = (index: number): void => { removeThirdPartyAPIEndpoint(index); setOverlayVisible(false); }; const onRemoveEndpoint = (name: string, index: number): void => { showOverlay( { deleteAddress(index); }} /> ); }; const isCurrentEndpointUrlExists = (currentEndpoint: string) => (endpoint: string) => { const omitCurrent = thirdPartyAPIEndpoints.filter((item) => { return item.endpoint !== currentEndpoint; }); const isFound = !!omitCurrent.find((item) => { return item.endpoint === endpoint; }); return isFound; }; const isEndpointUrlExists = (endpoint: string): boolean => { const isFound = !!thirdPartyAPIEndpoints.find((item) => { return item.endpoint === endpoint; }); return isFound; }; const addNewEndpoint = (newValues: ThirdPartyAPIEntryProps): void => { addThirdPartyAPIEndpoint(newValues); setNewEndpoint(false); }; const onUpdateEndpoint = (index: number) => (newValues: ThirdPartyAPIEntryProps) => { const newEndpoint = [...thirdPartyAPIEndpoints]; newEndpoint.splice(index, 1, newValues); setThirdPartyAPIEndpoints(newEndpoint); }; const swapArray = (indexA: number, indexB: number): void => { const toOrdered = [...thirdPartyAPIEndpoints]; const to = toOrdered[indexB]; const from = toOrdered[indexA]; toOrdered[indexA] = to; toOrdered[indexB] = from; setThirdPartyAPIEndpoints(toOrdered); }; const moveEntryUp = (id: number): void => { if (id === 0) return; swapArray(id - 1, id); }; const moveEntryDown = (id: number): void => { if (id + 1 >= thirdPartyAPIEndpoints.length) return; swapArray(id + 1, id); }; return ( <>

Order

Name

Endpoint

API Header

API Key

{thirdPartyAPIEndpoints.map((item, index) => { const orderNumber = index + 1; return ( { onRemoveEndpoint(item.name, index); }} onMoveEntryUp={() => { moveEntryUp(index); }} onMoveEntryDown={() => { moveEntryDown(index); }} onUpdateEndpoint={onUpdateEndpoint(index)} api={thirdPartyAPIEndpoints[index].endpoint} name={thirdPartyAPIEndpoints[index].name} apiHeader={thirdPartyAPIEndpoints[index].apiHeader} apiKey={thirdPartyAPIEndpoints[index].apiKey} canEdit={false} /> ); })} {isNewEndpoint && ( { setNewEndpoint(false); }} onUpdateEndpoint={addNewEndpoint} api="" name="" apiHeader="" apiKey="" canEdit={true} /> )} {thirdPartyAPIEndpoints.length === 0 && !isNewEndpoint && (

No third party's endpoint found.{" "}

)} ); };