1import{r as d,k as L,j as e}from"./react-vendor-CakdnCLW.js";import{a as S,A as j}from"./account-layout-zSHymsIN.js";import{Field as i,AccountFormStyles as D}from"./login-C1qQdySd.js";import{v as n}from"./index-CHiJ-3ZR.js";import{z as g,ac as k,g as E,O as T}from"./icons-B-um9kmx.js";import"./storefront-layout-BiPdYnN1.js";import"./66721_1789330752794-DzVQkLf2.js";import"./basket-context-BBmBykDy.js";import"./use-auth-Cn-sr86I.js";import"./textarea-B7l4IhcF.js";import"./index-BJeG27Bx.js";import"./TintReturnsNotice-C5XmpPlY.js";import"./tint-order-presentation-CCiLp4YQ.js";import"./not-found-C1jsgL3-.js";const b={label:"",recipientName:"",phone:"",addressLine1:"",addressLine2:"",city:"",postcode:"",country:"GB",isDefault:!1};function _(){const c=S(!0).data?.customer??null,[p,l]=d.useState([]),[y,N]=d.useState(!0),[o,x]=d.useState(!1),[t,a]=d.useState({...b}),[h,f]=d.useState(null),u=async()=>{try{const s=await fetch("/api/account/addresses",{credentials:"include",headers:n()}),r=s.ok?await s.json():[];l(r)}catch{l([])}finally{N(!1)}};d.useEffect(()=>{c&&u()},[c]);const v=async s=>{s.preventDefault(),f(null);const r=await fetch("/api/account/addresses",{method:"POST",headers:{"Content-Type":"application/json",...n()},credentials:"include",body:JSON.stringify(t)});if(r.ok)await u(),x(!1),a({...b});else{const m=await r.json().catch(()=>({}));f(m?.error||"Could not save address")}},w=async s=>{if(!confirm("Delete this address?"))return;(await fetch(`/api/account/addresses/${s}`,{method:"DELETE",credentials:"include",headers:n()})).ok&&l(m=>m.filter(C=>C.id!==s))},A=L({mutationFn:async s=>{await fetch(`/api/account/addresses/${s}`,{method:"PUT",headers:{"Content-Type":"application/json",...n()},credentials:"include",body:JSON.stringify({isDefault:!0})})},onSuccess:()=>u()});return c?e.jsxs(j,{title:"Addresses",testId:"page-account-addresses",children:[e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Saved delivery addresses are pre-filled at checkout to save you time."}),e.jsxs("button",{type:"button",onClick:()=>x(s=>!s),className:"text-sm inline-flex items-center gap-1 text-primary underline","data-testid":"button-toggle-address-form",children:[e.jsx(k,{className:"w-4 h-4"})," ",o?"Cancel":"Add address"]})]}),y?e.jsxs("div",{className:"flex items-center gap-2 text-sm text-muted-foreground",children:[e.jsx(g,{className:"w-4 h-4 animate-spin"})," Loadingâ¦"]}):p.length===0&&!o?e.jsx("p",{className:"text-sm text-muted-foreground","data-testid":"text-no-addresses",children:"No saved addresses yet."}):e.jsx("ul",{className:"grid sm:grid-cols-2 gap-3","data-testid":"list-addresses",children:p.map(s=>e.jsxs("li",{className:"border rounded-md p-3 text-sm space-y-1 bg-muted/20","data-testid":`address-${s.id}`,children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("strong",{className:"inline-flex items-center gap-1",children:[e.jsx(E,{className:"w-3 h-3"})," ",s.label||"Address"]}),s.isDefault&&e.jsx("span",{className:"text-xs px-2 py-0.5 rounded-full bg-emerald-100 text-emerald-700",children:"Default"})]}),s.recipientName&&e.jsx("div",{children:s.recipientName}),e.jsx("div",{children:s.addressLine1}),s.addressLine2&&e.jsx("div",{children:s.addressLine2}),e.jsxs("div",{children:[s.city,", ",s.postcode]}),e.jsxs("div",{className:"flex gap-2 pt-1 text-xs",children:[!s.isDefault&&e.jsx("button",{type:"button",onClick:()=>A.mutate(s.id),className:"text-primary underline","data-testid":`button-address-default-${s.id}`,children:"Make default"}),e.jsxs("button",{type:"button",onClick:()=>w(s.id),className:"text-destructive inline-flex items-center gap-1","data-testid":`button-address-delete-${s.id}`,children:[e.jsx(T,{className:"w-3 h-3"})," Delete"]})]})]},s.id))}),o&&e.jsxs("form",{onSubmit:v,className:"grid sm:grid-cols-2 gap-3 max-w-2xl border rounded-md p-4 bg-muted/20","data-testid":"form-address",children:[e.jsx(i,{label:"Label (e.g. Home, Yard)",children:e.jsx("input",{className:"account-input",value:t.label??"",onChange:s=>a({...t,label:s.target.value}),"data-testid":"input-address-label"})}),e.jsx(i,{label:"Recipient name",children:e.jsx("input",{className:"account-input",value:t.recipientName??"",onChange:s=>a({...t,recipientName:s.target.value}),"data-testid":"input-address-recipient"})}),e.jsx(i,{label:"Address line 1",children:e.jsx("input",{className:"account-input",required:!0,value:t.addressLine1,onChange:s=>a({...t,addressLine1:s.target.value}),"data-testid":"input-address-line1"})}),e.jsx(i,{label:"Address line 2",children:e.jsx("input",{className:"account-input",value:t.addressLine2??"",onChange:s=>a({...t,addressLine2:s.target.value}),"data-testid":"input-address-line2"})}),e.jsx(i,{label:"Town / city",children:e.jsx("input",{className:"account-input",required:!0,value:t.city,onChange:s=>a({...t,city:s.target.value}),"data-testid":"input-address-city"})}),e.jsx(i,{label:"Postcode",children:e.jsx("input",{className:"account-input",required:!0,value:t.postcode,onChange:s=>a({...t,postcode:s.target.value}),"data-testid":"input-address-postcode"})}),e.jsxs("label",{className:"flex items-center gap-2 text-sm sm:col-span-2",children:[e.jsx("input",{type:"checkbox",checked:t.isDefault,onCh
1ange:s=>a({...t,isDefault:s.target.checked}),"data-testid":"checkbox-address-default"}),e.jsx("span",{children:"Use as default delivery address"})]}),h&&e.jsx("p",{className:"sm:col-span-2 text-sm text-destructive","data-testid":"text-address-error",children:h}),e.jsx("div",{className:"sm:col-span-2",children:e.jsx("button",{type:"submit",className:"rounded-md bg-primary text-white px-4 py-2 font-semibold","data-testid":"button-address-save",children:"Save address"})})]})]}),e.jsx(D,{})]}):e.jsx(j,{title:"Addresses",children:e.jsxs("div",{className:"flex items-center gap-2 text-sm text-muted-foreground","data-testid":"addresses-loading",children:[e.jsx(g,{className:"w-4 h-4 animate-spin"})," Loadingâ¦"]})})}export{_ as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.