@kompasid/lit-web-components 0.9.37 → 0.9.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.
@@ -1,7 +1,8 @@
1
1
  import { html, css, LitElement, nothing } from 'lit'
2
2
  import { property, state, customElement } from 'lit/decorators.js'
3
+ import { differenceInDays } from 'date-fns'
3
4
  import { TWStyles } from '../../../tailwind/tailwind.js'
4
- import { PaywallProduct, PaywallType } from './types.js'
5
+ import { PaywallProduct, PaywallType, Contextual } from './types.js'
5
6
  import '../kompasid-paywall-body/KompasPaywallBody.js'
6
7
  import '../kompasid-paywall-banner-registration/KompasPaywallBannerRegistration.js'
7
8
  import '../kompasid-paywall-information-header/KompasPaywallInformationHeader.js'
@@ -93,6 +94,9 @@ export class KompasPaywall extends LitElement {
93
94
  @property({ type: String }) tracker_content_title = ''
94
95
  @property({ type: String }) tracker_content_categories = ''
95
96
  @property({ type: String }) tracker_content_type = ''
97
+ @property({ type: String }) tracker_content_tags = ''
98
+ @property({ type: String }) tracker_content_published_date = ''
99
+ @property({ type: String }) tracker_content_variant = ''
96
100
  @property({ type: String }) tracker_user_type = ''
97
101
  @property({ type: String }) tracker_subscription_status = ''
98
102
  @property({ type: String }) tracker_page_domain = ''
@@ -273,10 +277,16 @@ export class KompasPaywall extends LitElement {
273
277
 
274
278
  async getPaywallData() {
275
279
  try {
276
- const response = await fetch(
277
- 'https://cdn-www.kompas.id/web-component/paywall.json'
280
+ const [paywallResponse, contextualResponse] = await Promise.all([
281
+ fetch('https://cdn-www.kompas.id/web-component/paywall.json'),
282
+ fetch(
283
+ 'https://cdn-www.kompas.id/web-component/contextual_paywall.json'
284
+ ),
285
+ ])
286
+ const json = await paywallResponse.json()
287
+ const isContextual = this.contextualScenario(
288
+ await contextualResponse.json()
278
289
  )
279
- const json = await response.json()
280
290
  const listType = [
281
291
  'reguler',
282
292
  'audio',
@@ -287,17 +297,24 @@ export class KompasPaywall extends LitElement {
287
297
  ]
288
298
  const findTypeOnTheList = listType.includes(this.type)
289
299
  const defaultType = findTypeOnTheList ? this.type : 'reguler'
300
+ const typeContextual = isContextual ? 'typeB' : 'typeA'
290
301
  this.paywallData = {
291
302
  ...this.mockResult,
292
303
  informations: {
293
304
  ...this.mockResult.informations,
294
- title: json[defaultType].head,
295
- description: json[defaultType].content,
305
+ title: json[defaultType][typeContextual]
306
+ ? json[defaultType][typeContextual].head
307
+ : json[defaultType].head,
308
+ description: json[defaultType][typeContextual]
309
+ ? json[defaultType][typeContextual].content
310
+ : json[defaultType].content,
296
311
  cta: json[defaultType].cta,
297
312
  },
298
313
  packages: {
299
314
  ...this.mockResult.packages,
300
- memberships: json[defaultType].memberships,
315
+ memberships: json[defaultType][typeContextual]
316
+ ? json[defaultType][typeContextual].memberships
317
+ : json[defaultType].memberships,
301
318
  swgEnable: json[defaultType].swgEnable,
302
319
  freeTrial: json[defaultType].freeTrial,
303
320
  swgContent: json[defaultType].swgContent,
@@ -310,6 +327,70 @@ export class KompasPaywall extends LitElement {
310
327
  }
311
328
  }
312
329
 
330
+ /**
331
+ * Checks if all strings in the given target tags and variant are found in the given string tag and variant.
332
+ * @param {Contextual} target - The contextual data to be checked.
333
+ * @param {string} stringTag - The string tag to search for.
334
+ * @param {string} stringVariant - The string variant to search for.
335
+ * @returns {boolean} - True if all strings are found, false otherwise.
336
+ */
337
+ private checkAllInString(
338
+ target: Contextual,
339
+ stringTag: string,
340
+ stringVariant: string
341
+ ) {
342
+ const tagsTarget = target.tags
343
+ const variantTarget = target.variant
344
+ const checkVariant =
345
+ variantTarget.toLowerCase() === stringVariant.toLowerCase()
346
+ const checkTags = () => {
347
+ for (const item of tagsTarget) {
348
+ if (!stringTag.toLowerCase().includes(item.toLowerCase())) {
349
+ return false
350
+ }
351
+ }
352
+ return true
353
+ }
354
+
355
+ return checkVariant && checkTags()
356
+ }
357
+
358
+ /**
359
+ * Check if the given contextual data is valid based on the following rules:
360
+ * - The published date must be within 90 days from now
361
+ * - The tracker tags and variant must be found in the contextual data
362
+ * @param {Contextual} jsonContextual - The contextual data to be checked
363
+ * @returns {boolean} - True if the contextual data is valid, false otherwise
364
+ */
365
+ private contextualScenario(jsonContextual: Contextual): boolean {
366
+ if (!jsonContextual) {
367
+ return false
368
+ }
369
+
370
+ const {
371
+ tracker_content_published_date: publishedDate,
372
+ tracker_content_tags: trackerTags,
373
+ tracker_content_variant: trackerVariant,
374
+ } = this
375
+
376
+ if (!publishedDate || !trackerVariant || !trackerTags) {
377
+ return false
378
+ }
379
+
380
+ const nowDate = new Date()
381
+ const isoDateString = publishedDate.replace(' ', 'T')
382
+ const publishedDateObj = new Date(isoDateString)
383
+
384
+ const isWithin90Days = differenceInDays(publishedDateObj, nowDate) <= -90 // differenceInDays jika tanggal telah melewati 90 hari
385
+ const hasMatchingTagsAndVariants = this.checkAllInString(
386
+ jsonContextual,
387
+ trackerTags,
388
+ trackerVariant
389
+ ) // checkAllInString jika ada tag yang cocok
390
+
391
+ return isWithin90Days || hasMatchingTagsAndVariants
392
+ }
393
+
313
394
  private transitionBox() {
314
395
  return html`
315
396
  <div
@@ -331,6 +412,9 @@ export class KompasPaywall extends LitElement {
331
412
  tracker_content_id=${this.tracker_content_id}
332
413
  tracker_content_title=${this.tracker_content_title}
333
414
  tracker_content_categories=${this.tracker_content_categories}
415
+ tracker_content_tags=${this.tracker_content_tags}
416
+ tracker_content_published_date=${this.tracker_content_published_date}
417
+ tracker_content_variant=${this.tracker_content_variant}
334
418
  tracker_user_type=${this.tracker_user_type}
335
419
  tracker_subscription_status=${this.tracker_subscription_status}
336
420
  tracker_page_domain=${this.tracker_page_domain}
@@ -378,6 +462,10 @@ export class KompasPaywall extends LitElement {
378
462
  tracker_content_id=${this.tracker_content_id}
379
463
  tracker_content_title=${this.tracker_content_title}
380
464
  tracker_content_categories=${this.tracker_content_categories}
465
+ tracker_content_tags=${this.tracker_content_tags}
466
+ tracker_content_published_date=${this
467
+ .tracker_content_published_date}
468
+ tracker_content_variant=${this.tracker_content_variant}
381
469
  tracker_user_type=${this.tracker_user_type}
382
470
  tracker_subscription_status=${this.tracker_subscription_status}
383
471
  tracker_page_domain=${this.tracker_page_domain}
@@ -421,6 +509,10 @@ export class KompasPaywall extends LitElement {
421
509
  tracker_content_id=${this.tracker_content_id}
422
510
  tracker_content_title=${this.tracker_content_title}
423
511
  tracker_content_categories=${this.tracker_content_categories}
512
+ tracker_content_tags=${this.tracker_content_tags}
513
+ tracker_content_published_date=${this
514
+ .tracker_content_published_date}
515
+ tracker_content_variant=${this.tracker_content_variant}
424
516
  tracker_user_type=${this.tracker_user_type}
425
517
  tracker_subscription_status=${this.tracker_subscription_status}
426
518
  tracker_epaper_edition=${this.tracker_epaper_edition}
@@ -471,6 +563,10 @@ export class KompasPaywall extends LitElement {
471
563
  tracker_content_id=${this.tracker_content_id}
472
564
  tracker_content_title=${this.tracker_content_title}
473
565
  tracker_content_categories=${this.tracker_content_categories}
566
+ tracker_content_tags=${this.tracker_content_tags}
567
+ tracker_content_published_date=${this
568
+ .tracker_content_published_date}
569
+ tracker_content_variant=${this.tracker_content_variant}
474
570
  tracker_user_type=${this.tracker_user_type}
475
571
  tracker_subscription_status=${this.tracker_subscription_status}
476
572
  tracker_page_domain=${this.tracker_page_domain}
@@ -515,6 +611,10 @@ export class KompasPaywall extends LitElement {
515
611
  tracker_content_id=${this.tracker_content_id}
516
612
  tracker_content_title=${this.tracker_content_title}
517
613
  tracker_content_categories=${this.tracker_content_categories}
614
+ tracker_content_tags=${this.tracker_content_tags}
615
+ tracker_content_published_date=${this
616
+ .tracker_content_published_date}
617
+ tracker_content_variant=${this.tracker_content_variant}
518
618
  tracker_user_type=${this.tracker_user_type}
519
619
  tracker_subscription_status=${this.tracker_subscription_status}
520
620
  tracker_epaper_edition=${this.tracker_epaper_edition}