Skip to content

Commit a418239

Browse files
Merge pull request #124 from commercelayer/my-account-back-to-shop
Add "back to shop" feature to my account
2 parents 08515ef + 6ab500e commit a418239

File tree

8 files changed

+57
-10
lines changed

8 files changed

+57
-10
lines changed

packages/docs/stories/my-account/cl-my-account-link.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default meta
1414

1515
const Template: StoryFn<Args> = (args) => {
1616
return create(html`
17-
<cl-my-account-link target=${args.target ?? nothing}>
17+
<cl-my-account-link target=${args.target ?? nothing} back-to-shop=${args["back-to-shop"] ?? nothing}>
1818
Go to my account
1919
</cl-my-account-link>
2020
`)

packages/drop-in/src/apis/commercelayer/account.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { getAccessToken } from "@/apis/commercelayer/client"
22
import { getConfig, getOrganizationConfig } from "@/apis/commercelayer/config"
33
import { getClosestLocationHref } from "@/utils/url"
44

5-
export async function getMyAccountUrl(): Promise<string | undefined> {
5+
export async function getMyAccountUrl({
6+
returnUrl,
7+
}: {
8+
returnUrl?: string
9+
}): Promise<string | undefined> {
610
const config = getConfig()
711
const { ownerType } = await getAccessToken(config)
812

@@ -13,7 +17,7 @@ export async function getMyAccountUrl(): Promise<string | undefined> {
1317
const organizationConfig = await getOrganizationConfig()
1418
const lang = config.defaultAttributes?.orders?.language_code
1519

16-
return `${organizationConfig.links.my_account}${lang != null ? `&lang=${lang}` : ""}`
20+
return `${organizationConfig.links.my_account}${lang != null ? `&lang=${lang}` : ""}${returnUrl != null ? `&returnUrl=${returnUrl}` : ""}`
1721
}
1822

1923
export async function getIdentityUrl(

packages/drop-in/src/apis/commercelayer/client.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ export async function createClient(config: Config) {
105105
CommerceLayer({
106106
accessToken: token.accessToken,
107107
domain: config.domain,
108+
fetch: async (input, init) => {
109+
const response = await fetch(input, init)
110+
111+
if (response.status === 401) {
112+
const config = getConfig()
113+
const clientCredentials = configToClientCredentials(config)
114+
const salesChannel = getSalesChannel(clientCredentials)
115+
await salesChannel.removeAuthorization()
116+
117+
window.location.reload()
118+
}
119+
120+
return response
121+
},
108122
})
109123

110124
return {

packages/drop-in/src/components.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ export namespace Components {
145145
"type": "guest" | "customer" | undefined;
146146
}
147147
interface ClMyAccountLink {
148+
/**
149+
* Providing this attribute will enable the "Back to shop" and "Logout" navigation links on the My Account page. When set to `true`, the link will redirect to the current page URL. You can also provide a custom URL string.
150+
*/
151+
"backToShop"?: "true" | string;
148152
/**
149153
* The browsing context in which to open the linked URL (a tab, a window, or an &lt;iframe&gt;).
150154
* @default "_self"
@@ -407,6 +411,10 @@ declare namespace LocalJSX {
407411
"type": "guest" | "customer" | undefined;
408412
}
409413
interface ClMyAccountLink {
414+
/**
415+
* Providing this attribute will enable the "Back to shop" and "Logout" navigation links on the My Account page. When set to `true`, the link will redirect to the current page URL. You can also provide a custom URL string.
416+
*/
417+
"backToShop"?: "true" | string;
410418
/**
411419
* The browsing context in which to open the linked URL (a tab, a window, or an &lt;iframe&gt;).
412420
* @default "_self"

packages/drop-in/src/components/cl-identity-link/cl-identity-link.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class ClIdentityLink {
9090
if (this.type === "logout") {
9191
event.preventDefault()
9292
void logout().then(() => {
93-
location.reload()
93+
window.location.reload()
9494
})
9595
}
9696
}}

packages/drop-in/src/components/cl-my-account-link/cl-my-account-link.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "@stencil/core"
1111
import { getMyAccountUrl } from "@/apis/commercelayer/account"
1212
import { listenTo } from "@/apis/event"
13+
import { getClosestLocationHref } from "@/utils/url"
1314

1415
@Component({
1516
tag: "cl-my-account-link",
@@ -24,14 +25,29 @@ export class ClMyAccountLink {
2425
@Prop({ reflect: true }) target: "_self" | "_blank" | "_parent" | "_top" =
2526
"_self"
2627

28+
/**
29+
* Providing this attribute will enable the "Back to shop" and "Logout" navigation links on the My Account page.
30+
* When set to `true`, the link will redirect to the current page URL.
31+
* You can also provide a custom URL string.
32+
*/
33+
@Prop({ reflect: true, mutable: true }) backToShop?: "true" | string
34+
2735
@State() href: string | undefined
2836

2937
async componentWillLoad(): Promise<void> {
3038
listenTo("cl-identity-gettoken", async () => {
31-
this.href = await getMyAccountUrl()
39+
if (isTrue(this.backToShop)) {
40+
this.backToShop = getClosestLocationHref()
41+
}
42+
43+
this.href = await getMyAccountUrl({ returnUrl: this.backToShop })
3244
})
3345

34-
this.href = await getMyAccountUrl()
46+
if (isTrue(this.backToShop)) {
47+
this.backToShop = getClosestLocationHref()
48+
}
49+
50+
this.href = await getMyAccountUrl({ returnUrl: this.backToShop })
3551
}
3652

3753
render(): JSX.Element {
@@ -44,3 +60,7 @@ export class ClMyAccountLink {
4460
)
4561
}
4662
}
63+
64+
function isTrue(value: unknown): value is true {
65+
return value === true || value === "true"
66+
}

packages/drop-in/src/components/cl-my-account-link/readme.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
## Properties
99

10-
| Property | Attribute | Description | Type | Default |
11-
| -------- | --------- | --------------------------------------------------------------------------------------------- | -------------------------------------------- | --------- |
12-
| `target` | `target` | The browsing context in which to open the linked URL (a tab, a window, or an &lt;iframe&gt;). | `"_blank" \| "_parent" \| "_self" \| "_top"` | `"_self"` |
10+
| Property | Attribute | Description | Type | Default |
11+
| ------------ | -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- | ----------- |
12+
| `backToShop` | `back-to-shop` | Providing this attribute will enable the "Back to shop" and "Logout" navigation links on the My Account page. When set to `true`, the link will redirect to the current page URL. You can also provide a custom URL string. | `string \| undefined` | `undefined` |
13+
| `target` | `target` | The browsing context in which to open the linked URL (a tab, a window, or an &lt;iframe&gt;). | `"_blank" \| "_parent" \| "_self" \| "_top"` | `"_self"` |
1314

1415

1516
----------------------------------------------

packages/drop-in/src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
<cl-identity-link type="signup">Signup</cl-identity-link>
8787
</cl-identity-status>
8888
<cl-identity-status type="customer">
89-
<cl-my-account-link target="_blank" class="mr-4">My Account</cl-my-account-link>
89+
<cl-my-account-link class="mr-4" back-to-shop="true">My Account</cl-my-account-link>
9090
<cl-identity-link type="logout">Logout</cl-identity-link>
9191
</cl-identity-status>
9292
</div>

0 commit comments

Comments
 (0)