UNPKG

@commercelayer/react-components

Version:
2 lines 1.27 kB
"use client"; import{jsx as _jsx}from"react/jsx-runtime";import{useContext}from"react";import Parent from"../utils/Parent";import CommerceLayerContext from"../../context/CommerceLayerContext";import{getApplicationLink}from"../../utils/getApplicationLink";import{getDomain}from"../../utils/getDomain";import{jwt}from"../../utils/jwt";import{getOrganizationConfig}from"../../utils/organization";export function MyAccountLink(props){const{label="Go to my account",children,customDomain,...p}=props,{accessToken,endpoint}=useContext(CommerceLayerContext);if(accessToken==null||endpoint==null)throw new Error("Cannot use `MyAccountLink` outside of `CommerceLayer`");const{domain,slug}=getDomain(endpoint),disabled=!("owner"in jwt(accessToken)),href=getApplicationLink({slug,accessToken,applicationType:"my-account",domain,customDomain}),parentProps={disabled,label,href,...p};function handleClick(e){!disabled&&accessToken&&endpoint&&getOrganizationConfig({accessToken,endpoint,params:{accessToken,slug}}).then(config=>{config?.links?.my_account&&(e.preventDefault(),location.href=config.links.my_account)})}return children?_jsx(Parent,{...parentProps,children}):_jsx("a",{"aria-disabled":disabled,onClick:handleClick,href,...p,children:label})}export default MyAccountLink;