@commercelayer/react-components
Version:
The Official Commerce Layer React Components
2 lines • 1.7 kB
JavaScript
"use client";
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.MyAccountLink=MyAccountLink;const tslib_1=require("tslib"),jsx_runtime_1=require("react/jsx-runtime"),react_1=require("react"),Parent_1=tslib_1.__importDefault(require("../utils/Parent")),CommerceLayerContext_1=tslib_1.__importDefault(require("../../context/CommerceLayerContext")),getApplicationLink_1=require("../../utils/getApplicationLink"),getDomain_1=require("../../utils/getDomain"),jwt_1=require("../../utils/jwt"),organization_1=require("../../utils/organization");function MyAccountLink(props){const{label="Go to my account",children,customDomain}=props,p=tslib_1.__rest(props,["label","children","customDomain"]),{accessToken,endpoint}=(0,react_1.useContext)(CommerceLayerContext_1.default);if(accessToken==null||endpoint==null)throw new Error("Cannot use `MyAccountLink` outside of `CommerceLayer`");const{domain,slug}=(0,getDomain_1.getDomain)(endpoint),disabled=!("owner"in(0,jwt_1.jwt)(accessToken)),href=(0,getApplicationLink_1.getApplicationLink)({slug,accessToken,applicationType:"my-account",domain,customDomain}),parentProps=Object.assign({disabled,label,href},p);function handleClick(e){!disabled&&accessToken&&endpoint&&(0,organization_1.getOrganizationConfig)({accessToken,endpoint,params:{accessToken,slug}}).then(config=>{var _a;!((_a=config?.links)===null||_a===void 0)&&_a.my_account&&(e.preventDefault(),location.href=config.links.my_account)})}return children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children})):(0,jsx_runtime_1.jsx)("a",Object.assign({"aria-disabled":disabled,onClick:handleClick,href},p,{children:label}))}exports.default=MyAccountLink;