UNPKG

@forbespro/lead-agent

Version:
2 lines (1 loc) 3.7 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react/jsx-runtime"),P=require("react"),D=require("./index19.js"),E=require("./index6.js"),F=require("./index20.js"),T=require("./index21.js"),_=require("./index22.js");function U(n){const c=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(n){for(const o in n)if(o!=="default"){const f=Object.getOwnPropertyDescriptor(n,o);Object.defineProperty(c,o,f.get?f:{enumerable:!0,get:()=>n[o]})}}return c.default=n,Object.freeze(c)}const r=U(P),N=r.forwardRef(({id:n,name:c,value:o,onChange:f,onSelect:I,placeholder:S="Search for an address...",children:C,onKeyDown:k},A)=>{const[j,d]=r.useState(!1),[v,w]=r.useState(""),[i]=D.useDebounce(v,500),[m,p]=r.useState(!1),[h,u]=r.useState([]),[x,b]=r.useState(null),y=r.useRef(null),L=e=>{w(e.target.value),f(e.target.value),b(null),e.target.value&&d(!0)};r.useEffect(()=>{async function e(){if(!i){u([]);return}p(!0);try{const s=await fetch(`/api/address?query=${encodeURIComponent(i)}`);if(!s.ok)throw new Error("Failed to fetch addresses");const l=await s.json();u(l.Items||[])}catch(s){console.error("Failed to fetch addresses:",s),u([])}finally{p(!1)}}e()},[i]);const R=async e=>{p(!0);try{let s;if(e.Type==="Postcode"?s=await fetch(`/api/address?postcodeId=${encodeURIComponent(e.Id)}`):s=await fetch(`/api/address?id=${encodeURIComponent(e.Id)}`),!s.ok)throw new Error("Failed to retrieve address");const l=await s.json();if(l.Items&&l.Items.length>1)u(l.Items),d(!0);else if(l.Items&&l.Items.length===1){const a=l.Items[0];b(a);const g=q(a);w(g),f(g);const O={street:[a.Line1,a.Line2].filter(Boolean).join(", "),city:a.City||"",state:a.ProvinceName||"",postalCode:a.PostalCode||"",country:a.CountryName||""};I(g,O),d(!1)}}catch(s){console.error("Failed to retrieve address:",s)}finally{p(!1)}},q=e=>[e.Line1,e.Line2,e.City,e.PostalCode].filter(Boolean).join(", ");return r.useEffect(()=>{const e=s=>{y.current&&!y.current.contains(s.target)&&d(!1)};return document.addEventListener("mousedown",e),()=>{document.removeEventListener("mousedown",e)}},[]),t.jsxs("div",{className:"fpl-relative fpl-w-full",ref:y,children:[t.jsxs("div",{className:"fpl-flex fpl-items-center fpl-gap-2",children:[t.jsx(E.Input,{id:n,name:c,ref:A,value:v||o,onKeyDown:k,onChange:L,placeholder:S,onFocus:()=>i&&d(!0),className:"fpl-w-full fpl-mb-2"}),C]}),j&&(h.length>0||m)&&t.jsx("div",{className:"fpl-max-h-60 fpl-overflow-y-auto fpl-border fpl-rounded-md fpl-bg-gray-50 fpl-mt-1",children:m?t.jsxs("div",{className:"fpl-flex fpl-items-center fpl-justify-center fpl-p-4 fpl-text-gray-500",children:[t.jsx(F.default,{className:"fpl-h-4 fpl-w-4 fpl-mr-2 fpl-animate-spin"}),t.jsx("span",{children:"Loading addresses..."})]}):h.map(e=>t.jsxs("button",{onClick:()=>R(e),className:"fpl-w-full fpl-px-4 fpl-py-2 fpl-text-left hover:bg-gray-100 fpl-border-b focus:border-b-0 fpl-flex fpl-items-center",children:[(x==null?void 0:x.Id)===e.Id&&t.jsx(T.default,{className:"fpl-h-4 fpl-w-4 fpl-mr-2 fpl-text-primary fpl-flex-shrink-0"}),t.jsxs("div",{className:"fpl-flex-grow",children:[t.jsx("span",{className:"fpl-block",children:e.Text}),e.Description&&t.jsx("span",{className:"fpl-block fpl-text-xs fpl-text-gray-500",children:e.Description})]}),e.Type==="Postcode"&&t.jsx(_.default,{className:"fpl-h-4 fpl-w-4 fpl-ml-2 fpl-text-gray-400 fpl-flex-shrink-0"})]},e.Id))}),j&&h.length===0&&i&&!m&&t.jsx("div",{className:"fpl-max-h-60 fpl-overflow-y-auto fpl-border fpl-rounded-md fpl-bg-gray-50 fpl-mt-1",children:t.jsx("div",{className:"fpl-p-4 fpl-text-gray-500 fpl-text-center",children:"No addresses found"})})]})});N.displayName="AddressSearch";exports.AddressSearch=N;