@kompasid/lit-web-components 0.9.37 → 0.9.39

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,74 @@ 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
+
347
+ const checkTags = () => {
348
+ if (!tagsTarget.length) return false
349
+ for (const item of tagsTarget) {
350
+ if (!stringTag.toLowerCase().includes(item.toLowerCase())) {
351
+ return false
352
+ }
353
+ }
354
+ return true
355
+ }
356
+ return checkVariant && checkTags()
357
+ }
358
+
359
+ /**
360
+ * Check if the given contextual data is valid based on the following rules:
361
+ * - The published date must be within 90 days from now
362
+ * - The tracker tags and variant must be found in the contextual data
363
+ * @param {Contextual} jsonContextual - The contextual data to be checked
364
+ * @returns {boolean} - True if the contextual data is valid, false otherwise
365
+ */
366
+ private contextualScenario(jsonContextual: Contextual): boolean {
367
+ if (!jsonContextual) {
368
+ return false
369
+ }
370
+
371
+ const {
372
+ tracker_content_published_date: publishedDate,
373
+ tracker_content_tags: trackerTags,
374
+ tracker_content_variant: trackerVariant,
375
+ } = this
376
+
377
+ if (!publishedDate || !trackerVariant || !trackerTags) {
378
+ return false
379
+ }
380
+
381
+ const nowDate = new Date()
382
+ const isoDateString = publishedDate.replace(' ', 'T')
383
+ const publishedDateObj = new Date(isoDateString)
384
+
385
+ const isWithin90Days = jsonContextual.articleAgeTreshold
386
+ ? differenceInDays(publishedDateObj, nowDate) <
387
+ -jsonContextual.articleAgeTreshold
388
+ : false // differenceInDays jika tanggal telah melewati 90 hari
389
+ const hasMatchingTagsAndVariants = this.checkAllInString(
390
+ jsonContextual,
391
+ trackerTags,
392
+ trackerVariant
393
+ ) // checkAllInString jika ada tag yang cocok
394
+
395
+ return isWithin90Days || hasMatchingTagsAndVariants
396
+ }
397
+
313
398
  private transitionBox() {
314
399
  return html`
315
400
  <div
@@ -331,6 +416,9 @@ export class KompasPaywall extends LitElement {
331
416
  tracker_content_id=${this.tracker_content_id}
332
417
  tracker_content_title=${this.tracker_content_title}
333
418
  tracker_content_categories=${this.tracker_content_categories}
419
+ tracker_content_tags=${this.tracker_content_tags}
420
+ tracker_content_published_date=${this.tracker_content_published_date}
421
+ tracker_content_variant=${this.tracker_content_variant}
334
422
  tracker_user_type=${this.tracker_user_type}
335
423
  tracker_subscription_status=${this.tracker_subscription_status}
336
424
  tracker_page_domain=${this.tracker_page_domain}
@@ -378,6 +466,10 @@ export class KompasPaywall extends LitElement {
378
466
  tracker_content_id=${this.tracker_content_id}
379
467
  tracker_content_title=${this.tracker_content_title}
380
468
  tracker_content_categories=${this.tracker_content_categories}
469
+ tracker_content_tags=${this.tracker_content_tags}
470
+ tracker_content_published_date=${this
471
+ .tracker_content_published_date}
472
+ tracker_content_variant=${this.tracker_content_variant}
381
473
  tracker_user_type=${this.tracker_user_type}
382
474
  tracker_subscription_status=${this.tracker_subscription_status}
383
475
  tracker_page_domain=${this.tracker_page_domain}
@@ -421,6 +513,10 @@ export class KompasPaywall extends LitElement {
421
513
  tracker_content_id=${this.tracker_content_id}
422
514
  tracker_content_title=${this.tracker_content_title}
423
515
  tracker_content_categories=${this.tracker_content_categories}
516
+ tracker_content_tags=${this.tracker_content_tags}
517
+ tracker_content_published_date=${this
518
+ .tracker_content_published_date}
519
+ tracker_content_variant=${this.tracker_content_variant}
424
520
  tracker_user_type=${this.tracker_user_type}
425
521
  tracker_subscription_status=${this.tracker_subscription_status}
426
522
  tracker_epaper_edition=${this.tracker_epaper_edition}
@@ -471,6 +567,10 @@ export class KompasPaywall extends LitElement {
471
567
  tracker_content_id=${this.tracker_content_id}
472
568
  tracker_content_title=${this.tracker_content_title}
473
569
  tracker_content_categories=${this.tracker_content_categories}
570
+ tracker_content_tags=${this.tracker_content_tags}
571
+ tracker_content_published_date=${this
572
+ .tracker_content_published_date}
573
+ tracker_content_variant=${this.tracker_content_variant}
474
574
  tracker_user_type=${this.tracker_user_type}
475
575
  tracker_subscription_status=${this.tracker_subscription_status}
476
576
  tracker_page_domain=${this.tracker_page_domain}
@@ -515,6 +615,10 @@ export class KompasPaywall extends LitElement {
515
615
  tracker_content_id=${this.tracker_content_id}
516
616
  tracker_content_title=${this.tracker_content_title}
517
617
  tracker_content_categories=${this.tracker_content_categories}
618
+ tracker_content_tags=${this.tracker_content_tags}
619
+ tracker_content_published_date=${this
620
+ .tracker_content_published_date}
621
+ tracker_content_variant=${this.tracker_content_variant}
518
622
  tracker_user_type=${this.tracker_user_type}
519
623
  tracker_subscription_status=${this.tracker_subscription_status}
520
624
  tracker_epaper_edition=${this.tracker_epaper_edition}