@postnord/pn-marketweb-components 1.0.34 → 1.0.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/cjs/FetchHelper-f80943bf.js +87 -0
  2. package/cjs/{MarketWebLoginManager-8b675eb4.js → MarketWebLoginManager-859590e2.js} +8 -0
  3. package/cjs/loader.cjs.js +1 -1
  4. package/cjs/pn-language-selector_9.cjs.entry.js +6 -1
  5. package/cjs/pn-mainnav-link.cjs.entry.js +1 -1
  6. package/cjs/pn-market-web-components.cjs.js +1 -1
  7. package/cjs/pn-marketweb-sitefooter.cjs.entry.js +15 -2
  8. package/cjs/pn-marketweb-siteheader.cjs.entry.js +54 -23
  9. package/cjs/pn-profile-selector.cjs.entry.js +21 -13
  10. package/collection/components/layout-components/pn-marketweb-sitefooter/pn-marketweb-sitefooter.js +33 -2
  11. package/collection/components/layout-components/pn-marketweb-sitefooter/pn-marketweb-sitefooter.stories.js +3 -3
  12. package/collection/components/layout-components/pn-marketweb-siteheader/pn-marketweb-siteheader-login.js +23 -0
  13. package/collection/components/layout-components/pn-marketweb-siteheader/pn-marketweb-siteheader.js +102 -26
  14. package/collection/components/layout-components/pn-marketweb-siteheader/pn-marketweb-siteheader.stories.js +60 -2
  15. package/collection/components/navigation/pn-mainnav/pn-mainnav-link.js +1 -1
  16. package/collection/components/navigation/pn-profile-selector/pn-profile-selector.js +26 -18
  17. package/collection/globals/FetchHelper.js +84 -0
  18. package/collection/globals/MarketWebLoginManager.js +8 -0
  19. package/custom-elements/index.js +379 -233
  20. package/esm/FetchHelper-a0c8aa54.js +85 -0
  21. package/esm/{MarketWebLoginManager-dd7ac023.js → MarketWebLoginManager-83f2e86a.js} +8 -0
  22. package/esm/loader.js +1 -1
  23. package/esm/pn-language-selector_9.entry.js +6 -1
  24. package/esm/pn-mainnav-link.entry.js +1 -1
  25. package/esm/pn-market-web-components.js +1 -1
  26. package/esm/pn-marketweb-sitefooter.entry.js +15 -2
  27. package/esm/pn-marketweb-siteheader.entry.js +54 -23
  28. package/esm/pn-profile-selector.entry.js +21 -13
  29. package/esm-es5/FetchHelper-a0c8aa54.js +1 -0
  30. package/esm-es5/MarketWebLoginManager-83f2e86a.js +1 -0
  31. package/esm-es5/loader.js +1 -1
  32. package/esm-es5/pn-language-selector_9.entry.js +1 -1
  33. package/esm-es5/pn-mainnav-link.entry.js +1 -1
  34. package/esm-es5/pn-market-web-components.js +1 -1
  35. package/esm-es5/pn-marketweb-sitefooter.entry.js +1 -1
  36. package/esm-es5/pn-marketweb-siteheader.entry.js +1 -1
  37. package/esm-es5/pn-profile-selector.entry.js +1 -1
  38. package/package.json +1 -1
  39. package/pn-market-web-components/p-02ab0208.entry.js +1 -0
  40. package/pn-market-web-components/{p-ffdf87ee.entry.js → p-03fb0285.entry.js} +1 -1
  41. package/pn-market-web-components/p-1649b94a.entry.js +1 -0
  42. package/pn-market-web-components/p-25bdf3f8.system.js +1 -1
  43. package/pn-market-web-components/p-4921fcc3.entry.js +1 -0
  44. package/pn-market-web-components/p-58cdf3a6.system.js +1 -0
  45. package/pn-market-web-components/{p-6a32362d.system.entry.js → p-7b4c721f.system.entry.js} +1 -1
  46. package/pn-market-web-components/p-97dc5687.js +1 -0
  47. package/pn-market-web-components/p-a983c263.system.entry.js +1 -0
  48. package/pn-market-web-components/p-c254cfdf.js +1 -0
  49. package/pn-market-web-components/p-d6a17042.system.js +1 -0
  50. package/pn-market-web-components/p-daa6ddb3.system.entry.js +1 -0
  51. package/pn-market-web-components/p-dade9bdb.system.entry.js +1 -0
  52. package/pn-market-web-components/p-dc471243.entry.js +1 -0
  53. package/pn-market-web-components/{p-251d44f2.system.entry.js → p-e3fb52a6.system.entry.js} +1 -1
  54. package/pn-market-web-components/pn-market-web-components.esm.js +1 -1
  55. package/types/components/layout-components/pn-marketweb-sitefooter/pn-marketweb-sitefooter.d.ts +5 -0
  56. package/types/components/layout-components/pn-marketweb-siteheader/pn-marketweb-siteheader-login.d.ts +2 -0
  57. package/types/components/layout-components/pn-marketweb-siteheader/pn-marketweb-siteheader.d.ts +12 -0
  58. package/types/components/navigation/pn-profile-selector/pn-profile-selector.d.ts +2 -1
  59. package/types/components.d.ts +34 -2
  60. package/types/globals/FetchHelper.d.ts +17 -0
  61. package/types/globals/MarketWebLoginManager.d.ts +1 -0
  62. package/esm-es5/MarketWebLoginManager-dd7ac023.js +0 -1
  63. package/pn-market-web-components/p-031d91aa.entry.js +0 -1
  64. package/pn-market-web-components/p-134f14eb.system.entry.js +0 -1
  65. package/pn-market-web-components/p-37efd029.entry.js +0 -1
  66. package/pn-market-web-components/p-4dcea578.js +0 -1
  67. package/pn-market-web-components/p-51768dc0.system.js +0 -1
  68. package/pn-market-web-components/p-b4075f56.entry.js +0 -1
  69. package/pn-market-web-components/p-d0f1c36b.system.entry.js +0 -1
  70. package/pn-market-web-components/p-db7419be.system.entry.js +0 -1
  71. package/pn-market-web-components/p-e220ea83.entry.js +0 -1
@@ -4,6 +4,8 @@ export class PnMarketwebSiteheaderLogin {
4
4
  constructor() {
5
5
  /** Specifies which endpoint domain we should load from */
6
6
  this.endpoint = "";
7
+ /** Access token passed from backend */
8
+ this.token = "";
7
9
  this.emitEvents = true;
8
10
  // Login dialog
9
11
  this.loginDialog = null;
@@ -15,6 +17,9 @@ export class PnMarketwebSiteheaderLogin {
15
17
  componentWillLoad() {
16
18
  this.loginManager = new MarketWebLoginManager({ endpoint: this.endpoint, eventTarget: this.hostElement });
17
19
  this.hostElement["loginmanager"] = this.loginManager;
20
+ if (this.token) {
21
+ this.loginManager.registerToken(this.token);
22
+ }
18
23
  this.hostElement.addEventListener(this.loginManager.events.loginstatechange, this.onLoginStateChange.bind(this));
19
24
  this.init();
20
25
  }
@@ -160,6 +165,24 @@ export class PnMarketwebSiteheaderLogin {
160
165
  "reflect": false,
161
166
  "defaultValue": "\"\""
162
167
  },
168
+ "token": {
169
+ "type": "string",
170
+ "mutable": false,
171
+ "complexType": {
172
+ "original": "string",
173
+ "resolved": "string",
174
+ "references": {}
175
+ },
176
+ "required": false,
177
+ "optional": false,
178
+ "docs": {
179
+ "tags": [],
180
+ "text": "Access token passed from backend"
181
+ },
182
+ "attribute": "token",
183
+ "reflect": false,
184
+ "defaultValue": "\"\""
185
+ },
163
186
  "i18n": {
164
187
  "type": "any",
165
188
  "mutable": false,
@@ -1,6 +1,8 @@
1
1
  import { Component, Prop, h, State, Element, Listen, Watch, Host, Event } from "@stencil/core";
2
2
  import { translations } from "./translations";
3
3
  import { MarketWebContextService } from "../../../globals/MarketWebContextService";
4
+ import { MarketWebLoginManager } from "../../../globals/MarketWebLoginManager";
5
+ import { FetchHelper } from "../../../globals/FetchHelper";
4
6
  export class PnMarketwebSiteheader {
5
7
  constructor() {
6
8
  this.endpointPath = "/api/navigation/header";
@@ -12,6 +14,8 @@ export class PnMarketwebSiteheader {
12
14
  this.language = null; //sv
13
15
  /** Specifies which environment we're fetching data from. (production, preproduction, integration, localhost) */
14
16
  this.environment = null; //sv
17
+ /** Access token passed from backend */
18
+ this.token = "";
15
19
  /** Specifies which endpoint domain we should load from */
16
20
  this.endpoint = null;
17
21
  /** Hides the site selector if set to true*/
@@ -24,10 +28,13 @@ export class PnMarketwebSiteheader {
24
28
  this.hideLogin = false;
25
29
  /** Forward session to backend */
26
30
  this.sessionForward = false;
31
+ /** If the component should use cached requests */
32
+ this.cache = false;
27
33
  /** Event based only language switch */
28
34
  this.spaMode = false;
29
35
  this.gotData = false;
30
36
  this.fetchingData = false;
37
+ this.homePageLink = "";
31
38
  // Menu state
32
39
  this.menuItems = [];
33
40
  // Search
@@ -40,8 +47,14 @@ export class PnMarketwebSiteheader {
40
47
  // Login dialog
41
48
  this.loginDialog = null;
42
49
  this.minimizeSearch = false;
50
+ this.loggedIn = false;
51
+ // FetchHelper
52
+ this.fetchHelper = new FetchHelper("siteheader");
53
+ // Login manager
54
+ this.loginManager = null;
43
55
  }
44
56
  componentWillLoad() {
57
+ this.loginManager = new MarketWebLoginManager({ eventTarget: this.hostElement });
45
58
  this.setInitialValues().then(() => {
46
59
  this.init();
47
60
  });
@@ -78,7 +91,9 @@ export class PnMarketwebSiteheader {
78
91
  }
79
92
  }
80
93
  onLanguageSelectorChange(e) {
94
+ console.log('onLanguageSelectorChange(e', e);
81
95
  this.language = e.detail;
96
+ window.dispatchEvent(new CustomEvent('marketweb-languagechange', { detail: this.language }));
82
97
  }
83
98
  onLanguageChange() {
84
99
  this.setTranslations();
@@ -97,12 +112,14 @@ export class PnMarketwebSiteheader {
97
112
  this.fetchingData = true;
98
113
  const endpointBase = (this.endpoint.lastIndexOf("/") === this.endpoint.length - 1) ? this.endpoint.substring(0, this.endpoint.length - 1) : this.endpoint;
99
114
  const fetchUrl = `${endpointBase}${this.endpointPath}?market=${this.market}&language=${this.language}`;
100
- const response = await fetch(fetchUrl, {
115
+ const data = await this.fetchHelper.fetchJson(fetchUrl, {
101
116
  'mode': 'cors'
102
- });
103
- const data = await response.json();
117
+ }, this.cache, this.onFetchRefreshed.bind(this));
104
118
  await this.setStateFromData(data);
105
119
  }
120
+ onFetchRefreshed(data) {
121
+ this.setStateFromData(data);
122
+ }
106
123
  getLanguageVersionUrl(item) {
107
124
  if (this.spaMode) {
108
125
  return null;
@@ -116,6 +133,7 @@ export class PnMarketwebSiteheader {
116
133
  return siteUrl + ((siteUrl.lastIndexOf("/") !== siteUrl.length - 1) ? "/" : "") + item.twoLetterISOLanguageName;
117
134
  }
118
135
  async setStateFromData(data) {
136
+ var _a, _b, _c;
119
137
  if (typeof data !== "object") {
120
138
  console.warn('Data was not valid', data);
121
139
  }
@@ -124,14 +142,16 @@ export class PnMarketwebSiteheader {
124
142
  // Set navigation information
125
143
  this.menuItems = data.mainMenu.menuItems;
126
144
  // Create the "Home" link
127
- this.menuItems.unshift({
128
- href: this.siteDefinition.url,
129
- name: this.i18n.menuHomeButton,
130
- id: 'homelink',
131
- open: false,
132
- selected: false,
133
- children: []
134
- });
145
+ if (!this.menuItems[0] || ((_a = this.menuItems[0]) === null || _a === void 0 ? void 0 : _a.name) !== this.i18n.menuHomeButton) {
146
+ this.menuItems.unshift({
147
+ href: this.siteDefinition.url,
148
+ name: this.i18n.menuHomeButton,
149
+ id: 'homelink',
150
+ open: false,
151
+ selected: false,
152
+ children: []
153
+ });
154
+ }
135
155
  // Set search
136
156
  this.search = data.search;
137
157
  // Set site selector
@@ -143,6 +163,7 @@ export class PnMarketwebSiteheader {
143
163
  await this.spaModeAdjustments();
144
164
  this.gotData = true;
145
165
  this.fetchingData = true;
166
+ this.homePageLink = (_c = (_b = this.siteDefinition) === null || _b === void 0 ? void 0 : _b.url) !== null && _c !== void 0 ? _c : document.location.hostname;
146
167
  window.setTimeout(() => { this.checkMenuOverflow(); }, 100);
147
168
  }
148
169
  setLanguageOptions() {
@@ -152,17 +173,19 @@ export class PnMarketwebSiteheader {
152
173
  }
153
174
  const hrefLangsTags = (_a = Array.prototype.slice.call(document.querySelectorAll('link[rel="alternate"][hreflang]'))) !== null && _a !== void 0 ? _a : [];
154
175
  const hrefLangs = hrefLangsTags.map((tag) => { return tag.getAttribute("hreflang"); });
176
+ let languageOptions = []; // Reset language options
155
177
  this.languageSelector.languages.map((languageOption) => {
156
178
  // In case there are language links present on the page we only show languages that have those tags
157
179
  if (hrefLangs && hrefLangs.length > 0) {
158
180
  if (hrefLangs.includes(languageOption.twoLetterISOLanguageName)) {
159
- this.languageOptions.push(languageOption);
181
+ languageOptions.push(languageOption);
160
182
  }
161
183
  }
162
184
  else {
163
- this.languageOptions.push(languageOption);
185
+ languageOptions.push(languageOption);
164
186
  }
165
187
  });
188
+ this.languageOptions = languageOptions; // Replace existing language options so we don't trigger a event change on every push.
166
189
  }
167
190
  async spaModeAdjustments() {
168
191
  if (!this.spaMode) {
@@ -171,12 +194,15 @@ export class PnMarketwebSiteheader {
171
194
  return;
172
195
  }
173
196
  onLoginStateChange(e) {
174
- var _a, _b;
175
- if (((_a = e.detail) === null || _a === void 0 ? void 0 : _a.loggedIn) && ((_b = e.detail) === null || _b === void 0 ? void 0 : _b.token) && this.sessionForward) {
176
- try {
177
- fetch('/api/userprofile/sync?token=' + e.detail.token);
197
+ var _a, _b, _c;
198
+ if (((_a = e.detail) === null || _a === void 0 ? void 0 : _a.loggedIn) && ((_b = e.detail) === null || _b === void 0 ? void 0 : _b.token)) {
199
+ this.loggedIn = ((_c = e.detail) === null || _c === void 0 ? void 0 : _c.loggedIn) === true;
200
+ if (this.sessionForward) {
201
+ try {
202
+ //fetch('/api/userprofile/sync?token=' + e.detail.token);
203
+ }
204
+ catch (e) { }
178
205
  }
179
- catch (e) { }
180
206
  }
181
207
  }
182
208
  promotedItemId(item) {
@@ -207,8 +233,19 @@ export class PnMarketwebSiteheader {
207
233
  this.minimizeSearch = true;
208
234
  }
209
235
  }
236
+ adjustSiteSelectorUrl(href) {
237
+ if (!this.loggedIn) {
238
+ return href;
239
+ }
240
+ const loginDomains = ['portal.postnord.com'];
241
+ const matchedHref = loginDomains.filter((domain) => { return href.indexOf(domain) !== -1; });
242
+ if (matchedHref.length === 0) {
243
+ return href;
244
+ }
245
+ return this.loginManager.getLoginUrl(href);
246
+ }
210
247
  render() {
211
- var _a, _b, _c, _d, _e, _f, _g, _h;
248
+ var _a, _b, _c, _d, _e, _f;
212
249
  return (h(Host, { language: this.language, market: this.market, environment: this.environment },
213
250
  h("header", null,
214
251
  h("div", { class: "siteheader-row" },
@@ -217,15 +254,15 @@ export class PnMarketwebSiteheader {
217
254
  !this.hideSiteSelector &&
218
255
  h("pn-site-selector", { language: this.language, buttontext: (_b = (_a = this.siteSelector) === null || _a === void 0 ? void 0 : _a.currentSiteTitle) !== null && _b !== void 0 ? _b : 'postnord' },
219
256
  (this.gotData && ((_c = this.siteSelector) === null || _c === void 0 ? void 0 : _c.currentSiteTitle)) &&
220
- h("pn-site-selector-item", { heading: this.siteSelector.currentSiteTitle, description: this.siteSelector.currentSiteDescription }),
221
- (this.gotData && ((_d = this.siteSelector) === null || _d === void 0 ? void 0 : _d.siteSelections)) && this.siteSelector.siteSelections.map((site) => h("pn-site-selector-item", { heading: site.linkText, description: site.linkDescription, url: site.href, newwindow: site.openInNewWindow })))),
257
+ h("pn-site-selector-item", { heading: this.siteSelector.currentSiteTitle + ' ', description: this.siteSelector.currentSiteDescription }),
258
+ (this.gotData && ((_d = this.siteSelector) === null || _d === void 0 ? void 0 : _d.siteSelections)) && this.siteSelector.siteSelections.map((site) => h("pn-site-selector-item", { heading: site.linkText + ' ', description: site.linkDescription, url: this.adjustSiteSelectorUrl(site.href), newwindow: site.openInNewWindow })))),
222
259
  h("div", { class: "siteheader-logocontainer" },
223
- h("a", { href: (_f = (_e = this.siteDefinition) === null || _e === void 0 ? void 0 : _e.url) !== null && _f !== void 0 ? _f : document.location.hostname, title: "Home", class: "siteheader-logolink" },
260
+ h("a", { href: this.homePageLink, title: "Home", class: "siteheader-logolink" },
224
261
  h("svg", { class: "siteheader-logo", xmlns: "http://www.w3.org/2000/svg", width: "12.7rem", height: "2.4rem", viewBox: "0 0 141.73 26.65" },
225
262
  h("path", { d: "M108.84,13.35c0,6.34-5.17,9-10,9S89,19.76,89,13.63c0-6.28,5.08-9,10-9S108.84,7.25,108.84,13.35Zm-6.56.18a3.4,3.4,0,1,0-6.8,0,3.29,3.29,0,0,0,3.42,3.41A3.24,3.24,0,0,0,102.28,13.54Zm-83,0c0,4.83-3.32,8.82-8.49,8.82a6.54,6.54,0,0,1-4.65-1.57v5.86H0V5.16H6.17V6.8a6.43,6.43,0,0,1,5.11-2.18C16.41,4.62,19.31,8.58,19.31,13.54Zm-6.53,0A3.27,3.27,0,0,0,9.4,10,3.28,3.28,0,0,0,6,13.54,3.29,3.29,0,0,0,9.4,16.95,3.24,3.24,0,0,0,12.78,13.54Zm103.69,0.27c0-2.54,1.54-3.32,3.6-3.32a6.56,6.56,0,0,1,2.08.3L122.3,5.1a3.92,3.92,0,0,0-1.45-.18c-3.45,0-4.38,2.18-4.38,2.18V5.16H110.3V21.84h6.17v-8ZM141.73,0V21.84h-6.16V20.21a6.43,6.43,0,0,1-5.11,2.18c-5.14,0-8-4-8-8.91s3.32-8.82,8.49-8.82a6.54,6.54,0,0,1,4.65,1.57v-5Zm-6,13.48a3.28,3.28,0,0,0-3.41-3.41A3.24,3.24,0,0,0,129,13.47,3.4,3.4,0,1,0,135.75,13.47ZM81.35,4.62c-3.6,0-4.9,2.18-4.9,2.18V5.16H70.29V21.84h6.16V13.29c0-2.11.79-3.08,2.69-3.08s2.18,1.57,2.18,3.57v8.07h6.22V11.6C87.55,7,85.34,4.62,81.35,4.62ZM64.07,1.42H57.9V21.84h6.16V10.36H67l2.81-5.2H64.07V1.42ZM50.26,11.21c-2-.3-2.87-0.3-2.87-1.09s0.81-.94,2.42-0.94a16.15,16.15,0,0,1,5.11.91L56,5.41a24.56,24.56,0,0,0-6.13-.75c-5.89,0-9,2.27-9,5.89,0,2.81,1.66,4.47,6.53,5.11,2.06,0.27,2.81.39,2.81,1.15s-0.82,1-2.24,1a16.67,16.67,0,0,1-6-1.3L40.77,21.3a22.14,22.14,0,0,0,6.68,1c6.35,0,9.28-2.24,9.28-5.89C56.72,13.54,55.15,12,50.26,11.21ZM40.19,13.35c0,6.34-5.17,9-10,9s-9.82-2.6-9.82-8.73c0-6.28,5.08-9,10-9S40.19,7.25,40.19,13.35Zm-6.56.18a3.4,3.4,0,1,0-6.8,0,3.28,3.28,0,0,0,3.41,3.41A3.24,3.24,0,0,0,33.63,13.54Z", fill: "#00A0D6", transform: "translate(0 0)" })))),
226
263
  h("div", { class: "siteheader-topright" },
227
264
  h("slot", { name: "toprightstart" }),
228
- (this.gotData && this.loginDialog && !this.hideLogin) && (h("pn-marketweb-siteheader-login", { loginDialog: this.loginDialog, endpoint: this.endpoint, i18n: this.i18n })),
265
+ (this.gotData && this.loginDialog && !this.hideLogin) && (h("pn-marketweb-siteheader-login", { token: this.token, loginDialog: this.loginDialog, endpoint: this.endpoint, i18n: this.i18n })),
229
266
  (this.gotData && this.languageOptions && this.languageOptions.length) && !this.hideLanguageSelector &&
230
267
  h("pn-language-selector", { value: this.language }, this.languageOptions.map(language => h("pn-language-selector-option", { name: language.nativeName, code: language.twoLetterISOLanguageName, selected: language.isCurrent, url: this.getLanguageVersionUrl(language) }))),
231
268
  h("slot", { name: "toprightend" }))),
@@ -253,9 +290,9 @@ export class PnMarketwebSiteheader {
253
290
  h("pn-language-selector", { value: this.language }, this.languageOptions.map(language => h("pn-language-selector-option", { name: language.nativeName, code: language.twoLetterISOLanguageName, selected: language.isCurrent, url: this.getLanguageVersionUrl(language) }))),
254
291
  !this.hideSiteSelector &&
255
292
  h("pn-site-selector", { language: this.language },
256
- (this.gotData && ((_g = this.siteSelector) === null || _g === void 0 ? void 0 : _g.currentSiteTitle)) &&
293
+ (this.gotData && ((_e = this.siteSelector) === null || _e === void 0 ? void 0 : _e.currentSiteTitle)) &&
257
294
  h("pn-site-selector-item", { heading: this.siteSelector.currentSiteTitle, description: this.siteSelector.currentSiteDescription }),
258
- (this.gotData && ((_h = this.siteSelector) === null || _h === void 0 ? void 0 : _h.siteSelections)) && this.siteSelector.siteSelections.map((site) => h("pn-site-selector-item", { heading: site.linkText, description: site.linkDescription, url: site.pageLink, newwindow: site.openInNewWindow })))),
295
+ (this.gotData && ((_f = this.siteSelector) === null || _f === void 0 ? void 0 : _f.siteSelections)) && this.siteSelector.siteSelections.map((site) => h("pn-site-selector-item", { heading: site.linkText, description: site.linkDescription, url: site.pageLink, newwindow: site.openInNewWindow })))),
259
296
  h("div", { slot: "top", class: "siteheader-menu-top" },
260
297
  h("pn-marketweb-siteheader-search", { search: this.search, "hide-search": this.hideSearch, i18n: this.i18n }))))),
261
298
  h("pn-marketweb-siteheader-search", { search: this.search, "hide-search": this.hideSearch, "show-only-link": this.minimizeSearch, i18n: this.i18n })))));
@@ -322,6 +359,24 @@ export class PnMarketwebSiteheader {
322
359
  "reflect": true,
323
360
  "defaultValue": "null"
324
361
  },
362
+ "token": {
363
+ "type": "string",
364
+ "mutable": false,
365
+ "complexType": {
366
+ "original": "string",
367
+ "resolved": "string",
368
+ "references": {}
369
+ },
370
+ "required": false,
371
+ "optional": false,
372
+ "docs": {
373
+ "tags": [],
374
+ "text": "Access token passed from backend"
375
+ },
376
+ "attribute": "token",
377
+ "reflect": false,
378
+ "defaultValue": "\"\""
379
+ },
325
380
  "endpoint": {
326
381
  "type": "string",
327
382
  "mutable": false,
@@ -430,6 +485,24 @@ export class PnMarketwebSiteheader {
430
485
  "reflect": false,
431
486
  "defaultValue": "false"
432
487
  },
488
+ "cache": {
489
+ "type": "boolean",
490
+ "mutable": false,
491
+ "complexType": {
492
+ "original": "boolean",
493
+ "resolved": "boolean",
494
+ "references": {}
495
+ },
496
+ "required": false,
497
+ "optional": false,
498
+ "docs": {
499
+ "tags": [],
500
+ "text": "If the component should use cached requests"
501
+ },
502
+ "attribute": "cache",
503
+ "reflect": false,
504
+ "defaultValue": "false"
505
+ },
433
506
  "spaMode": {
434
507
  "type": "boolean",
435
508
  "mutable": false,
@@ -453,6 +526,7 @@ export class PnMarketwebSiteheader {
453
526
  "i18n": {},
454
527
  "gotData": {},
455
528
  "fetchingData": {},
529
+ "homePageLink": {},
456
530
  "menuItems": {},
457
531
  "siteDefinition": {},
458
532
  "search": {},
@@ -460,7 +534,9 @@ export class PnMarketwebSiteheader {
460
534
  "languageSelector": {},
461
535
  "languageOptions": {},
462
536
  "loginDialog": {},
463
- "minimizeSearch": {}
537
+ "minimizeSearch": {},
538
+ "loggedIn": {},
539
+ "loginManager": {}
464
540
  }; }
465
541
  static get events() { return [{
466
542
  "method": "changeLanguage",
@@ -49,6 +49,7 @@ const PrimaryTemplate = ({ ...args }) => {
49
49
  hide-language-selector="false"
50
50
  hide-search="false"
51
51
  hide-login="false"
52
+ cache="true"
52
53
  session-forward="true">
53
54
  <a href="" class="header-cart" slot="toprightend" style="align-self:center;position:relative;text-decoration:none;">
54
55
  <span>Varukorg</span>
@@ -71,8 +72,8 @@ export const Primary = PrimaryTemplate.bind({});
71
72
  Primary.args = {
72
73
  market : "se",
73
74
  language: 'sv',
74
- endpoint: 'http://localhost:51444'
75
- };
75
+ endpoint: 'https://com-production.postnord.com'
76
+ }
76
77
 
77
78
 
78
79
  const MinimalOptionsTemplate = ({ ...args }) => {
@@ -96,13 +97,70 @@ const SPAModeOptionsTemplate = ({ ...args }) => {
96
97
  <div style="background:#ddd;min-height:100vh;">
97
98
  <pn-marketweb-siteheader language="sv" spa-mode="true">
98
99
  </pn-marketweb-siteheader>
100
+ <script>
101
+ document.addEventListener('DOMContentLoaded', () => {
102
+ if (!window["haslangchange"]) {
103
+ console.log('SPA Mode - attached global market web header language change eventlistener')
104
+
105
+ window.addEventListener('marketweb-languagechange', (e) => {
106
+ console.log('marketweb-languagechange e', e)
107
+ });
108
+ window["haslangchange"] = true;
109
+ }
110
+ });
111
+ </script>
99
112
  </div>
100
113
  `;
101
114
  };
102
115
 
116
+
117
+
103
118
  export const SPAModeOptions = SPAModeOptionsTemplate.bind({});
104
119
  SPAModeOptions.args = {
105
120
  market : "",
106
121
  language: '',
107
122
  endpoint: ''
123
+ };
124
+
125
+ const LocalhostTemplate = ({ ...args }) => {
126
+ return `
127
+ <div style="background:#ddd;min-height:100vh;">
128
+ <pn-marketweb-siteheader
129
+ market="${ args.market }"
130
+ language="${ args.language }"
131
+ endpoint="${ args.endpoint }"
132
+ environment="${ args.environment }"
133
+ >
134
+ </pn-marketweb-siteheader>
135
+ </div>
136
+ `;
137
+ };
138
+
139
+ export const LocalhostOptions = LocalhostTemplate.bind({});
140
+ LocalhostOptions.args = {
141
+ market : "se",
142
+ language: 'sv',
143
+ endpoint: 'http://localhost:51444/',
144
+ };
145
+
146
+ const LoggedInTemplate = ({ ...args }) => {
147
+ return `
148
+ <div style="background:#ddd;min-height:100vh;">
149
+ <pn-marketweb-siteheader
150
+ market="${ args.market }"
151
+ language="${ args.language }"
152
+ endpoint="${ args.endpoint }"
153
+ token="${ args.token }"
154
+ >
155
+ </pn-marketweb-siteheader>
156
+ </div>
157
+ `;
158
+ };
159
+
160
+ export const LoggedIn = LoggedInTemplate.bind({});
161
+ LoggedIn.args = {
162
+ market : "se",
163
+ language: 'sv',
164
+ endpoint: 'https://com-integration.postnord.com',
165
+ token: '12c8287a039c57a87cef023dde90605a6d27b4141f74ff7356347e3',
108
166
  };
@@ -31,7 +31,7 @@ export class PnMainnavLink {
31
31
  this.hasChildren ? (h("button", { onClick: this.setOpenMenuLevel.bind(this), "aria-controls": this.levelId, "aria-pressed": "", "aria-expanded": ((state.openLevel + "" === this.levelId + "")) + '' },
32
32
  this.name,
33
33
  h("pn-icon", { class: "first-level_icon", symbol: "angle-small-down", color: "blue700" }),
34
- h("pn-icon", { class: "first-level_arrow", symbol: "arrow-right", color: "blue700" }))) : (h("a", Object.assign({ href: this.href }, (this.target ? { target: this.target } : {}), (this.linkid ? { id: this.linkid } : {})),
34
+ h("pn-icon", { class: "first-level_arrow", symbol: "arrow-right", color: "blue700" }))) : (h("a", Object.assign({ href: this.href }, (this.target ? { target: this.target } : {}), (this.target === "_blank" ? { rel: "nofollow noopener" } : {}), (this.linkid ? { id: this.linkid } : {})),
35
35
  this.name,
36
36
  this.target === "_blank" ? (h("pn-icon", { symbol: "open-in-new", color: "blue700" })) : null)),
37
37
  h("slot", null)));
@@ -6,7 +6,8 @@ export class PnProfileSelector {
6
6
  constructor() {
7
7
  this.language = null; //sv
8
8
  this.returnUrl = "/"; //sv
9
- this.endpoint = "/api/userprofile/sync"; // Endpoint where we send in the current user session
9
+ // @Prop() endpoint = "/api/userprofile/sync"; // Endpoint where we send in the current user session
10
+ this.spamode = false; // Endpoint where we send in the current user session
10
11
  this.loginManager = null;
11
12
  this.loggedIn = false;
12
13
  this.i18n = translations["en"];
@@ -15,9 +16,10 @@ export class PnProfileSelector {
15
16
  }
16
17
  componentWillLoad() {
17
18
  this.loginManager = new MarketWebLoginManager({ eventTarget: this.hostElement });
18
- this.loginManager.init(true);
19
+ this.loginManager.init(this.spamode);
19
20
  this.hostElement["loginmanager"] = this.loginManager;
20
21
  this.hostElement.addEventListener(this.loginManager.events.loginstatechange, this.onLoginStateChange.bind(this));
22
+ this.checkLoggedInState();
21
23
  this.setInitialValues().then(() => {
22
24
  this.init();
23
25
  });
@@ -37,19 +39,25 @@ export class PnProfileSelector {
37
39
  init() {
38
40
  this.setTranslations();
39
41
  }
42
+ checkLoggedInState() {
43
+ if (this.loginManager.getToken() && this.loginManager.getUserInfo()) {
44
+ this.onLoginStateChange({ detail: true });
45
+ }
46
+ }
40
47
  async onLoginStateChange(e) {
41
48
  if (this.loggedIn !== e.detail) {
42
49
  this.loggedIn = e.detail;
43
- const token = this.loginManager.getToken();
44
- if (this.loggedIn && token) {
45
- try {
46
- await fetch(this.endpoint + '?token=' + token);
47
- if (this.numberOfProfiles === 1) {
48
- this.redirectUser();
49
- }
50
- }
51
- catch (e) { }
52
- }
50
+ this.isLoading = false;
51
+ this.redirectUser();
52
+ // const token = this.loginManager.getToken();
53
+ // if (this.loggedIn && token) {
54
+ // try {
55
+ // await fetch(this.endpoint + '?token=' + token);
56
+ // if (this.numberOfProfiles === 1) {
57
+ // this.redirectUser();
58
+ // }
59
+ // } catch(e) { }
60
+ // }
53
61
  }
54
62
  }
55
63
  async onLoggedIn() {
@@ -114,12 +122,12 @@ export class PnProfileSelector {
114
122
  "reflect": false,
115
123
  "defaultValue": "\"/\""
116
124
  },
117
- "endpoint": {
118
- "type": "string",
125
+ "spamode": {
126
+ "type": "boolean",
119
127
  "mutable": false,
120
128
  "complexType": {
121
- "original": "string",
122
- "resolved": "string",
129
+ "original": "boolean",
130
+ "resolved": "boolean",
123
131
  "references": {}
124
132
  },
125
133
  "required": false,
@@ -128,9 +136,9 @@ export class PnProfileSelector {
128
136
  "tags": [],
129
137
  "text": ""
130
138
  },
131
- "attribute": "endpoint",
139
+ "attribute": "spamode",
132
140
  "reflect": false,
133
- "defaultValue": "\"/api/userprofile/sync\""
141
+ "defaultValue": "false"
134
142
  }
135
143
  }; }
136
144
  static get states() { return {
@@ -0,0 +1,84 @@
1
+ class FetchHelper {
2
+ constructor(namespace = "") {
3
+ this.storagePrefix = "";
4
+ this.store = {
5
+ get: (key, permanentStorageFirst = false) => {
6
+ const firstProfider = permanentStorageFirst ? window.localStorage : window.sessionStorage;
7
+ const secondProvider = permanentStorageFirst ? window.sessionStorage : window.localStorage;
8
+ let value = firstProfider.getItem(`${this.storagePrefix}-${key}`);
9
+ if (!value) {
10
+ value = secondProvider.getItem(`${this.storagePrefix}-${key}`);
11
+ }
12
+ if (!value) {
13
+ return value;
14
+ }
15
+ if (value.indexOf('{') === 0) {
16
+ try {
17
+ return JSON.parse(value);
18
+ }
19
+ catch (e) {
20
+ }
21
+ }
22
+ if (value.indexOf(',') !== -1) {
23
+ return value.split(',');
24
+ }
25
+ return value;
26
+ },
27
+ set: (key, value, permanent = false) => {
28
+ const provider = permanent ? window.localStorage : window.sessionStorage;
29
+ if (typeof value === "object" && typeof value.length === "undefined") {
30
+ provider.setItem(`${this.storagePrefix}-${key}`, JSON.stringify(value));
31
+ return;
32
+ }
33
+ provider.setItem(`${this.storagePrefix}-${key}`, value);
34
+ },
35
+ remove: (key) => {
36
+ window.sessionStorage.removeItem(`${this.storagePrefix}-${key}`);
37
+ },
38
+ };
39
+ this.storagePrefix = namespace;
40
+ }
41
+ async fetchJson(input, init = {}, useCache = false, onCacheUpdated = null) {
42
+ const requestPromise = new Promise(async (resolve) => {
43
+ let doFetchRequest = true;
44
+ const url = (typeof input === "string") ? input : input.url;
45
+ const cacheKey = url;
46
+ let jsonData = null;
47
+ let cachedData = null;
48
+ if (useCache) {
49
+ cachedData = this.store.get(cacheKey);
50
+ // If the data was stored in session storage, then we don't need to do a full request
51
+ if (cachedData && !cachedData.permanent) {
52
+ doFetchRequest = false;
53
+ }
54
+ if (cachedData && cachedData.data) {
55
+ jsonData = cachedData.data;
56
+ resolve(jsonData);
57
+ }
58
+ }
59
+ if (doFetchRequest) {
60
+ const response = await window.fetch(input, init);
61
+ jsonData = await response.json();
62
+ resolve(jsonData);
63
+ if (useCache) {
64
+ if (typeof onCacheUpdated === "function" && cachedData != null) {
65
+ onCacheUpdated(jsonData);
66
+ }
67
+ this.store.set(cacheKey, this.wrapJson(jsonData, true), true);
68
+ this.store.set(cacheKey, this.wrapJson(jsonData, false), false);
69
+ }
70
+ }
71
+ });
72
+ return requestPromise;
73
+ }
74
+ wrapJson(data, permanent = false) {
75
+ const now = new Date();
76
+ return {
77
+ timestamp: now.getTime(),
78
+ time: now.toISOString(),
79
+ data: data,
80
+ permanent: permanent
81
+ };
82
+ }
83
+ }
84
+ export { FetchHelper };
@@ -73,6 +73,14 @@ class MarketWebLoginManager {
73
73
  const isLoggedIn = (this.store.get(this.keys.token) && this.store.get(this.keys.user)) ? true : false;
74
74
  return isLoggedIn;
75
75
  }
76
+ registerToken(token) {
77
+ if (!token || token === this.getToken()) {
78
+ return;
79
+ }
80
+ this.clearUserData();
81
+ this.store.set(this.keys.token, token);
82
+ this.fetchUserInfo();
83
+ }
76
84
  getLoginUrl(redirectPage = "") {
77
85
  const currentPage = window.location.href;
78
86
  redirectPage = redirectPage ? redirectPage : currentPage;