@commercelayer/react-components
Version:
The Official Commerce Layer React Components
2 lines • 1.85 kB
JavaScript
"use client";
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.MyIdentityLink=MyIdentityLink;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"),organization_1=require("../../utils/organization");function MyIdentityLink(props){const{label,children,type,clientId,scope,returnUrl,customDomain,resetPasswordUrl}=props,p=tslib_1.__rest(props,["label","children","type","clientId","scope","returnUrl","customDomain","resetPasswordUrl"]),{accessToken,endpoint}=(0,react_1.useContext)(CommerceLayerContext_1.default),[href,setHref]=(0,react_1.useState)(void 0);if(accessToken==null||endpoint==null)throw new Error("Cannot use `MyIdentityLink` outside of `CommerceLayer`");const{domain,slug}=(0,getDomain_1.getDomain)(endpoint);(0,react_1.useEffect)(()=>(accessToken&&endpoint&&(0,organization_1.getOrganizationConfig)({accessToken,endpoint,params:{accessToken,slug}}).then(config=>{var _a;if(!((_a=config?.links)===null||_a===void 0)&&_a.identity)setHref(config.links.identity);else{const link=(0,getApplicationLink_1.getApplicationLink)({slug,accessToken,applicationType:"identity",domain,modeType:type,clientId,scope,returnUrl:returnUrl??window.location.href,resetPasswordUrl,customDomain});setHref(link)}}),()=>{setHref(void 0)}),[accessToken,endpoint]);const parentProps=Object.assign({label,href,clientId,scope},p);return children?(0,jsx_runtime_1.jsx)(Parent_1.default,Object.assign({},parentProps,{children})):(0,jsx_runtime_1.jsx)("a",Object.assign({href},p,{children:label}))}exports.default=MyIdentityLink;