Skip to content

Commit

Permalink
feat: Add MyIdentityLink and use client to each file. Resolve #382, r…
Browse files Browse the repository at this point in the history
…esolve #391
  • Loading branch information
acasazza committed Aug 8, 2023
1 parent 3dc2126 commit c8539c1
Show file tree
Hide file tree
Showing 10 changed files with 1,207 additions and 999 deletions.
46 changes: 23 additions & 23 deletions packages/react-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
"coverage:report": "nyc report --reporter=html",
"build": "tsc -b tsconfig.prod.json tsconfig.prod.esm.json --verbose && pnpm postbuild",
"build:dev": "tsc -b tsconfig.prod.json tsconfig.prod.esm.json --verbose && tsc-alias -p tsconfig.prod.json && tsc-alias -p tsconfig.prod.esm.json",
"postbuild": "tsc-alias -p tsconfig.prod.json && tsc-alias -p tsconfig.prod.esm.json && minimize-js lib -w -s",
"postbuild": "tsc-alias -p tsconfig.prod.json && tsc-alias -p tsconfig.prod.esm.json && minimize-js lib -w -s -b '\"use client\";'",
"dev": "NODE_OPTIONS='--inspect' next dev"
},
"repository": {
Expand Down Expand Up @@ -180,14 +180,14 @@
"dependencies": {
"@ac-dev/countries-service": "^1.2.0",
"@ac-dev/states-service": "^1.1.1",
"@adyen/adyen-web": "^5.44.0",
"@commercelayer/sdk": "^5.4.1",
"@stripe/react-stripe-js": "^2.1.0",
"@stripe/stripe-js": "^1.54.0",
"@tanstack/react-table": "^8.9.2",
"@adyen/adyen-web": "^5.49.3",
"@commercelayer/sdk": "^5.10.0",
"@stripe/react-stripe-js": "^2.1.1",
"@stripe/stripe-js": "^2.0.0",
"@tanstack/react-table": "^8.9.3",
"@types/iframe-resizer": "^3.5.9",
"axios": "^1.4.0",
"braintree-web": "^3.94.0",
"braintree-web": "^3.96.1",
"frames-react": "^1.1.0",
"iframe-resizer": "^4.3.6",
"jwt-decode": "^3.1.2",
Expand All @@ -197,32 +197,32 @@
"devDependencies": {
"@commercelayer/js-auth": "^4.1.1",
"@faker-js/faker": "^8.0.2",
"@playwright/test": "^1.35.0",
"@testing-library/dom": "^9.3.0",
"@playwright/test": "^1.36.2",
"@testing-library/dom": "^9.3.1",
"@testing-library/react": "^14.0.0",
"@types/braintree-web": "^3.75.23",
"@types/lodash": "^4.14.195",
"@types/node": "^20.3.0",
"@types/braintree-web": "^3.94.0",
"@types/lodash": "^4.14.196",
"@types/node": "^20.4.8",
"@types/prop-types": "^15.7.5",
"@types/react": "^18.2.11",
"@types/react": "^18.2.19",
"@types/react-test-renderer": "^18.0.0",
"@types/react-window": "^1.8.5",
"@vitejs/plugin-react": "^4.0.0",
"@vitest/coverage-c8": "^0.32.0",
"@vitejs/plugin-react": "^4.0.4",
"@vitest/coverage-c8": "^0.33.0",
"jsdom": "^22.1.0",
"minimize-js": "^1.3.1",
"msw": "^1.2.2",
"minimize-js": "^1.4.0",
"msw": "^1.2.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-test-renderer": "^18.2.0",
"tsc-alias": "^1.8.6",
"tslib": "^2.5.3",
"typescript": "^5.1.3",
"vite": "^4.3.9",
"tsc-alias": "^1.8.7",
"tslib": "^2.6.1",
"typescript": "^5.1.6",
"vite": "^4.4.9",
"vite-tsconfig-paths": "^4.2.0",
"vitest": "^0.32.0"
"vitest": "^0.34.1"
},
"peerDependencies": {
"react": "^18.0.0"
}
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function GiftCardOrCouponCode({
)
codeType = 'coupon_code'
else if (!type) codeType = 'gift_card_code'
// @ts-expect-error deprecate `gift_card_or_coupon_code`
const code = order && codeType ? order[codeType] : ''
const hide = !(order && code)
const parentProps: ChildrenProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function GiftCardOrCouponRemoveButton(props: Props): JSX.Element | null {
if (!type && order && 'coupon_code' in order && order.coupon_code != null)
codeType = 'coupon_code'
else if (!type) codeType = 'gift_card_code'
// @ts-expect-error deprecate `gift_card_or_coupon_code`
const code = order && codeType ? order[codeType] : ''
const hide = !(order && code)
const handleClick = async (): Promise<void> => {
Expand Down
10 changes: 10 additions & 0 deletions packages/react-components/src/components/orders/CartLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ interface ChildrenProps extends Omit<Props, 'children'> {
* @returns Promise<void>
*/
handleClick?: (e: MouseEvent<HTMLAnchorElement>) => Promise<void>
/**
* The order id
*/
orderId?: string
/**
* The access token
*/
accessToken?: string
}

interface Props extends Omit<JSX.IntrinsicElements['a'], 'children'> {
Expand Down Expand Up @@ -77,6 +85,8 @@ export function CartLink(props: Props): JSX.Element | null {
handleClick,
label,
href,
orderId: order?.id,
accessToken,
...p
}
if (!accessToken) return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function Shipment({
return () => {
setLoading(true)
}
// @ts-expect-error deprecate `gift_card_or_coupon_code`
}, [shipments != null, shipments?.length, order?.gift_card_or_coupon_code])
const components = shipments?.map((shipment, k) => {
const shipmentLineItems = shipment.stock_line_items
Expand Down
1 change: 1 addition & 0 deletions packages/react-components/src/reducers/CustomerReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ export async function createCustomerAddress({
} else {
const newAddress = await sdk.addresses.create(address)
if (state?.customers?.id && newAddress?.id) {
// @ts-expect-error Expected customer_email
const newCustomerAddress = await sdk.customer_addresses.create({
customer: sdk.customers.relationship(state?.customers?.id),
address: sdk.addresses.relationship(newAddress.id)
Expand Down
55 changes: 41 additions & 14 deletions packages/react-components/src/utils/getApplicationLink.ts
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}`
}
2 changes: 1 addition & 1 deletion packages/react-components/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineConfig({
globals: true,
environment: 'jsdom',
coverage: {
provider: 'c8',
provider: 'v8',
reporter: ['text', 'json', 'html'],
exclude: ['mocks', 'specs']
},
Expand Down
Loading

0 comments on commit c8539c1

Please sign in to comment.