-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…esolve #391
- Loading branch information
Showing
10 changed files
with
1,207 additions
and
999 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/react-components/src/components/customers/MyIdentityLink.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { useContext } from 'react' | ||
import Parent from '../utils/Parent' | ||
import { type ChildrenFunction } from '#typings/index' | ||
import CommerceLayerContext from '#context/CommerceLayerContext' | ||
import { getApplicationLink } from '#utils/getApplicationLink' | ||
import { getDomain } from '#utils/getDomain' | ||
|
||
interface ChildrenProps extends Omit<Props, 'children'> { | ||
/** | ||
* The link href | ||
*/ | ||
href: string | ||
} | ||
|
||
interface Props extends Omit<JSX.IntrinsicElements['a'], 'children'> { | ||
/** | ||
* A render function to render your own custom component | ||
*/ | ||
children?: ChildrenFunction<ChildrenProps> | ||
/** | ||
* The label of the link | ||
*/ | ||
label: string | ||
/** | ||
* The type of the link | ||
*/ | ||
type: 'login' | 'signup' | ||
/** | ||
* The client id of the Commerce Layer application | ||
*/ | ||
clientId: string | ||
/** | ||
* The scope of the Commerce Layer application | ||
*/ | ||
scope: string | ||
/** | ||
* The return url to redirect the user after the login/signup | ||
*/ | ||
returnUrl?: string | ||
} | ||
|
||
export function MyIdentityLink(props: Props): JSX.Element { | ||
const { label, children, type, clientId, scope, returnUrl, ...p } = props | ||
const { accessToken, endpoint } = useContext(CommerceLayerContext) | ||
if (accessToken == null || endpoint == null) | ||
throw new Error('Cannot use `MyIdentityLink` outside of `CommerceLayer`') | ||
const { domain, slug } = getDomain(endpoint) | ||
const href = getApplicationLink({ | ||
slug, | ||
accessToken, | ||
applicationType: 'identity', | ||
domain, | ||
modeType: type, | ||
clientId, | ||
scope, | ||
returnUrl: returnUrl ?? window.location.href | ||
}) | ||
console.log('href', href) | ||
const parentProps = { | ||
label, | ||
href, | ||
clientId, | ||
scope, | ||
...p | ||
} | ||
return children ? ( | ||
<Parent {...parentProps}>{children}</Parent> | ||
) : ( | ||
<a href={href} {...p}> | ||
{label} | ||
</a> | ||
) | ||
} | ||
|
||
export default MyIdentityLink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 41 additions & 14 deletions
55
packages/react-components/src/utils/getApplicationLink.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,59 @@ | ||
type ApplicationType = 'checkout' | 'cart' | 'my-account' | ||
type ApplicationType = 'checkout' | 'cart' | 'my-account' | 'identity' | ||
|
||
type ApplicationTypeProps = | ||
| { | ||
applicationType: 'my-account' | ||
orderId?: string | ||
} | ||
| { | ||
applicationType: Omit<ApplicationType, 'my-account'> | ||
orderId: string | ||
} | ||
type ApplicationTypeProps<T extends ApplicationType = ApplicationType> = | ||
T extends 'my-account' | ||
? { | ||
applicationType: T | ||
orderId?: string | ||
modeType?: 'login' | 'signup' | ||
clientId?: string | ||
scope?: string | ||
returnUrl?: string | ||
} | ||
: T extends 'identity' | ||
? { | ||
applicationType: T | ||
orderId?: string | ||
modeType: 'login' | 'signup' | ||
clientId: string | ||
scope: string | ||
returnUrl: string | ||
} | ||
: { | ||
applicationType: Omit<T, 'my-account' | 'identity'> | ||
orderId: string | ||
modeType?: 'login' | 'signup' | ||
clientId?: string | ||
scope?: string | ||
returnUrl?: string | ||
} | ||
|
||
interface TArgs { | ||
accessToken: string | ||
slug: string | ||
domain: string | ||
} | ||
|
||
type Props = TArgs & ApplicationTypeProps | ||
type Props = ApplicationTypeProps & TArgs | ||
|
||
export function getApplicationLink({ | ||
orderId, | ||
accessToken, | ||
slug, | ||
domain, | ||
applicationType | ||
applicationType, | ||
modeType, | ||
clientId, | ||
scope, | ||
returnUrl | ||
}: Props): string { | ||
const env = domain === 'commercelayer.io' ? '' : 'stg.' | ||
const t = modeType === 'login' ? '' : 'signup' | ||
const c = clientId ? `&clientId=${clientId}` : '' | ||
const s = scope ? `&scope=${scope}` : '' | ||
const r = returnUrl ? `&returnUrl=${returnUrl}` : '' | ||
const params = applicationType === 'identity' ? `${c}${s}${r}` : '' | ||
return `https://${slug}.${env}commercelayer.app/${applicationType.toString()}/${ | ||
orderId ?? '' | ||
}?accessToken=${accessToken}` | ||
orderId ?? t ?? '' | ||
}?accessToken=${accessToken}${params}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.