@kompasid/lit-web-components 0.6.8 → 0.7.0
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.
- package/demo/index.html +34 -0
- package/dist/src/components/kompasid-widget-recirculations-default/KompasWidgetRecirculationsDefault.d.ts +39 -0
- package/dist/src/components/kompasid-widget-recirculations-default/KompasWidgetRecirculationsDefault.js +340 -0
- package/dist/src/components/kompasid-widget-recirculations-default/KompasWidgetRecirculationsDefault.js.map +1 -0
- package/dist/src/components/kompasid-widget-recirculations-default/types.d.ts +27 -0
- package/dist/src/components/kompasid-widget-recirculations-default/types.js +2 -0
- package/dist/src/components/kompasid-widget-recirculations-default/types.js.map +1 -0
- package/dist/src/components/kompasid-widget-recirculations-list/KompasWidgetRecirculationsList.d.ts +28 -0
- package/dist/src/components/kompasid-widget-recirculations-list/KompasWidgetRecirculationsList.js +188 -0
- package/dist/src/components/kompasid-widget-recirculations-list/KompasWidgetRecirculationsList.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -1
- package/dist/tailwind/tailwind.js +45 -0
- package/dist/tailwind/tailwind.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -1
- package/src/components/kompasid-widget-recirculations-default/KompasWidgetRecirculationsDefault.ts +342 -0
- package/src/components/kompasid-widget-recirculations-default/readme.md +57 -0
- package/src/components/kompasid-widget-recirculations-default/types.ts +28 -0
- package/src/components/kompasid-widget-recirculations-list/KompasWidgetRecirculationsList.ts +230 -0
- package/src/components/kompasid-widget-recirculations-list/readme.md +27 -0
- package/src/index.ts +2 -0
- package/tailwind/tailwind.css +42 -0
- package/tailwind/tailwind.ts +45 -0
package/src/components/kompasid-widget-recirculations-default/KompasWidgetRecirculationsDefault.ts
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { html, css, LitElement } from 'lit'
|
|
2
|
+
import { customElement, property } from 'lit/decorators.js'
|
|
3
|
+
import { format } from 'date-fns'
|
|
4
|
+
import { id } from 'date-fns/locale/id'
|
|
5
|
+
import { TWStyles } from '../../../tailwind/tailwind.js'
|
|
6
|
+
import { Post, Navigation } from './types.js'
|
|
7
|
+
|
|
8
|
+
@customElement('kompasid-widget-recirculations-default')
|
|
9
|
+
export class KompasWidgetRecirculationsDefault extends LitElement {
|
|
10
|
+
static styles = [
|
|
11
|
+
css`
|
|
12
|
+
:host {
|
|
13
|
+
font-family: 'PT Sans', sans-serif;
|
|
14
|
+
}
|
|
15
|
+
.chip {
|
|
16
|
+
align-items: center;
|
|
17
|
+
border-radius: 0.25rem;
|
|
18
|
+
display: flex;
|
|
19
|
+
font-family: 'PT Sans', sans-serif;
|
|
20
|
+
font-size: 0.75rem;
|
|
21
|
+
font-weight: bold;
|
|
22
|
+
height: 1.5rem;
|
|
23
|
+
justify-content: center;
|
|
24
|
+
line-height: 1rem;
|
|
25
|
+
margin-bottom: 0.25rem;
|
|
26
|
+
padding: 0.375rem 0.5rem;
|
|
27
|
+
}
|
|
28
|
+
`,
|
|
29
|
+
TWStyles,
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Props
|
|
34
|
+
*/
|
|
35
|
+
@property({ type: Array }) posts: Post[][] = []
|
|
36
|
+
@property({ type: Object }) navigation: Navigation | undefined = undefined
|
|
37
|
+
@property({ type: String }) accessToken = ''
|
|
38
|
+
@property({ type: String }) permalinkArticle = ''
|
|
39
|
+
@property({ type: String }) userGuid = '0'
|
|
40
|
+
@property({ type: String }) slugs = ''
|
|
41
|
+
@property({ type: String }) type: 'relatedArticle' | 'otherArticle' =
|
|
42
|
+
'relatedArticle'
|
|
43
|
+
@property({ type: String }) mainCategory = ''
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Getters
|
|
47
|
+
*/
|
|
48
|
+
get navigationPermalink(): string | undefined {
|
|
49
|
+
return this.navigation?.permalink
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Fetch Data
|
|
54
|
+
*/
|
|
55
|
+
async connectedCallback() {
|
|
56
|
+
super.connectedCallback()
|
|
57
|
+
try {
|
|
58
|
+
await this.fetchAccessToken()
|
|
59
|
+
if (this.type === 'relatedArticle') {
|
|
60
|
+
await this.relatedArticles()
|
|
61
|
+
} else if (this.type === 'otherArticle') {
|
|
62
|
+
await this.otherArticles()
|
|
63
|
+
}
|
|
64
|
+
} catch (error) {
|
|
65
|
+
this.handleFetchError(error)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async fetchAccessToken() {
|
|
70
|
+
const response = await fetch(
|
|
71
|
+
'https://api.kompas.id/account/api/v1/login/guest',
|
|
72
|
+
{
|
|
73
|
+
method: 'POST',
|
|
74
|
+
headers: {
|
|
75
|
+
'Content-Type': 'application/json',
|
|
76
|
+
},
|
|
77
|
+
body: JSON.stringify({
|
|
78
|
+
email: 'anonynous.user@kompas.id',
|
|
79
|
+
}),
|
|
80
|
+
}
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
const result = await response.json()
|
|
84
|
+
|
|
85
|
+
if (result?.data?.accessToken) {
|
|
86
|
+
this.accessToken = result.data.accessToken
|
|
87
|
+
} else {
|
|
88
|
+
throw new Error('Token akses tidak tersedia dalam respons')
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async relatedArticles() {
|
|
93
|
+
if (!this.accessToken) {
|
|
94
|
+
throw new Error('Token akses tidak tersedia')
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const kompasApiAi = 'https://ai.kompas.id/api/v1'
|
|
98
|
+
|
|
99
|
+
// Constructing parameters
|
|
100
|
+
const params = new URLSearchParams()
|
|
101
|
+
params.append('page_url', this.permalinkArticle)
|
|
102
|
+
params.append('page_type', 'read')
|
|
103
|
+
params.append('item_type', 'articles')
|
|
104
|
+
params.append('guid', this.userGuid)
|
|
105
|
+
params.append('slugs', this.slugs)
|
|
106
|
+
|
|
107
|
+
// Constructing the URL with parameters
|
|
108
|
+
const endpoint = `${kompasApiAi}/recommendation?${params.toString()}`
|
|
109
|
+
|
|
110
|
+
const response = await fetch(endpoint, {
|
|
111
|
+
headers: {
|
|
112
|
+
Authorization: `Bearer ${this.accessToken}`,
|
|
113
|
+
'Content-Type': 'application/json',
|
|
114
|
+
},
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
const result = await response.json()
|
|
118
|
+
|
|
119
|
+
if (result?.result) {
|
|
120
|
+
const items = result.result
|
|
121
|
+
const firstChunk = items.slice(0, 5)
|
|
122
|
+
const secondChunk = items.slice(5, 7)
|
|
123
|
+
this.posts = [Object.freeze(firstChunk), Object.freeze(secondChunk)]
|
|
124
|
+
console.log('related article:', this.posts)
|
|
125
|
+
} else {
|
|
126
|
+
throw new Error('Data artikel terkait tidak ditemukan')
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async otherArticles() {
|
|
131
|
+
if (!this.accessToken) {
|
|
132
|
+
throw new Error('Token akses tidak tersedia')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const kompasApiCdsSPA = 'https://cds.kompas.id/api/v1'
|
|
136
|
+
|
|
137
|
+
// Constructing the URL with parameters
|
|
138
|
+
const endpoint = `${kompasApiCdsSPA}/article/list/category/${this.mainCategory}`
|
|
139
|
+
|
|
140
|
+
const response = await fetch(endpoint, {
|
|
141
|
+
headers: {
|
|
142
|
+
Authorization: `Bearer ${this.accessToken}`,
|
|
143
|
+
'Content-Type': 'application/json',
|
|
144
|
+
},
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
const result = await response.json()
|
|
148
|
+
|
|
149
|
+
if (result?.result) {
|
|
150
|
+
const items = result.result
|
|
151
|
+
const firstChunk = items.slice(0, 5)
|
|
152
|
+
const secondChunk = items.slice(5, 7)
|
|
153
|
+
this.posts = [Object.freeze(firstChunk), Object.freeze(secondChunk)]
|
|
154
|
+
console.log('other article:', this.posts)
|
|
155
|
+
} else {
|
|
156
|
+
throw new Error('Data artikel lainnya tidak ditemukan')
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
handleFetchError(error: unknown) {
|
|
161
|
+
const errorMessage =
|
|
162
|
+
error instanceof Error ? error.message : 'Kesalahan tidak diketahui'
|
|
163
|
+
alert(`Terjadi kesalahan: ${errorMessage}`)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Function to format date
|
|
168
|
+
*/
|
|
169
|
+
formatDate(date: string) {
|
|
170
|
+
return format(new Date(date), 'dd MMMM yyyy', { locale: id })
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Render widget components
|
|
175
|
+
*/
|
|
176
|
+
|
|
177
|
+
private WidgetTitle() {
|
|
178
|
+
if (this.navigationPermalink) {
|
|
179
|
+
return html`
|
|
180
|
+
<a
|
|
181
|
+
href="${this.navigationPermalink}"
|
|
182
|
+
class="flex font-sans uppercase items-start mb-6 mt-8"
|
|
183
|
+
>
|
|
184
|
+
<h5
|
|
185
|
+
class="${[
|
|
186
|
+
'capitalize font-bold font-sans',
|
|
187
|
+
this.navigationPermalink ? 'text-brand-1' : 'text-grey-600',
|
|
188
|
+
].join(' ')}"
|
|
189
|
+
>
|
|
190
|
+
${this.navigation?.name}
|
|
191
|
+
</h5>
|
|
192
|
+
</a>
|
|
193
|
+
`
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return html`
|
|
197
|
+
<div class="flex font-sans uppercase items-start mb-6 mt-8">
|
|
198
|
+
<div>
|
|
199
|
+
<h5
|
|
200
|
+
class="${[
|
|
201
|
+
'capitalize font-bold font-sans',
|
|
202
|
+
this.navigationPermalink ? 'text-brand-1' : 'text-grey-600',
|
|
203
|
+
].join(' ')}"
|
|
204
|
+
>
|
|
205
|
+
${this.navigation?.name}
|
|
206
|
+
</h5>
|
|
207
|
+
</div>
|
|
208
|
+
</div>
|
|
209
|
+
`
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
renderChips(post: Post) {
|
|
213
|
+
const chips = []
|
|
214
|
+
const isAnalisis = post.postTag?.some(tag => tag.slug === 'analisis')
|
|
215
|
+
const isEksklusif = post.postTag?.some(tag => tag.slug === 'eksklusif')
|
|
216
|
+
const isFreemium = post.isFreemium === true
|
|
217
|
+
|
|
218
|
+
if (isEksklusif) {
|
|
219
|
+
chips.push(
|
|
220
|
+
html`
|
|
221
|
+
<div class="flex">
|
|
222
|
+
<div class="chip bg-grey-600 text-white">
|
|
223
|
+
<img
|
|
224
|
+
src="https://cdn-www.kompas.id/assets/img/icons/kompas-icon-small.svg"
|
|
225
|
+
alt="Kompas Icon"
|
|
226
|
+
style="width: 16px; height: 16px; margin-right: 4px;"
|
|
227
|
+
/>
|
|
228
|
+
Eksklusif
|
|
229
|
+
</div>
|
|
230
|
+
</div>
|
|
231
|
+
`
|
|
232
|
+
)
|
|
233
|
+
} else if (isFreemium) {
|
|
234
|
+
chips.push(
|
|
235
|
+
html`
|
|
236
|
+
<div class="flex">
|
|
237
|
+
<div class="chip bg-blue-100 text-brand-1">Bebas Akses</div>
|
|
238
|
+
</div>
|
|
239
|
+
`
|
|
240
|
+
)
|
|
241
|
+
} else if (isAnalisis) {
|
|
242
|
+
chips.push(
|
|
243
|
+
html`
|
|
244
|
+
<div class="flex">
|
|
245
|
+
<div class="chip bg-orange-100 text-orange-500">Analisis</div>
|
|
246
|
+
</div>
|
|
247
|
+
`
|
|
248
|
+
)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return chips
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
renderImage(post: Post) {
|
|
255
|
+
const { thumbnailMedium } = post.thumbnails?.sizes || {}
|
|
256
|
+
if (!thumbnailMedium) return ''
|
|
257
|
+
|
|
258
|
+
const { permalink, width, height } = thumbnailMedium
|
|
259
|
+
return html`
|
|
260
|
+
<img
|
|
261
|
+
src="${permalink}"
|
|
262
|
+
width="${width}"
|
|
263
|
+
height="${height}"
|
|
264
|
+
alt="${post.title}"
|
|
265
|
+
class="aspect-video object-cover w-full"
|
|
266
|
+
/>
|
|
267
|
+
`
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
render() {
|
|
271
|
+
return html`
|
|
272
|
+
<div class="w-full">
|
|
273
|
+
<!-- start: widget title -->
|
|
274
|
+
${this.WidgetTitle()}
|
|
275
|
+
<!-- end: widget title -->
|
|
276
|
+
|
|
277
|
+
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 md:gap-8">
|
|
278
|
+
<!-- start: left-post-loop -->
|
|
279
|
+
<div class="grid grid-cols-1 gap-6">
|
|
280
|
+
${this.posts[0]
|
|
281
|
+
? this.posts[0].map(
|
|
282
|
+
post => html`
|
|
283
|
+
<div class="w-full">
|
|
284
|
+
<div class="flex">
|
|
285
|
+
<div class="w-1/2">${this.renderImage(post)}</div>
|
|
286
|
+
<div class="flex-grow pl-4 w-1/2">
|
|
287
|
+
<a href="${post.permalink}">
|
|
288
|
+
${this.renderChips(post)}
|
|
289
|
+
<h5
|
|
290
|
+
class="hover:underline font-bold font-sans leading-tight text-grey-600"
|
|
291
|
+
>
|
|
292
|
+
${post.title}
|
|
293
|
+
</h5>
|
|
294
|
+
<p class="text-grey-600 pt-2">
|
|
295
|
+
${this.formatDate(post.publishedDate)}
|
|
296
|
+
</p>
|
|
297
|
+
</a>
|
|
298
|
+
</div>
|
|
299
|
+
</div>
|
|
300
|
+
</div>
|
|
301
|
+
`
|
|
302
|
+
)
|
|
303
|
+
: 'Belum ada artikel'}
|
|
304
|
+
</div>
|
|
305
|
+
<!-- end: left-post-loop -->
|
|
306
|
+
|
|
307
|
+
<!-- start: right-post-loop -->
|
|
308
|
+
<div class="flex flex-col w-full">
|
|
309
|
+
${this.posts[1]
|
|
310
|
+
? this.posts[1].map(
|
|
311
|
+
post => html`
|
|
312
|
+
<div class="mb-6">
|
|
313
|
+
<div class="flex">
|
|
314
|
+
<div class="w-1/2">${this.renderImage(post)}</div>
|
|
315
|
+
<div class="flex-grow pl-4 w-1/2">
|
|
316
|
+
<a href="${post.permalink}">
|
|
317
|
+
${this.renderChips(post)}
|
|
318
|
+
<h5
|
|
319
|
+
class="hover:underline font-bold font-sans leading-tight text-grey-600"
|
|
320
|
+
>
|
|
321
|
+
${post.title}
|
|
322
|
+
</h5>
|
|
323
|
+
<p class="text-grey-600 pt-2">
|
|
324
|
+
${this.formatDate(post.publishedDate)}
|
|
325
|
+
</p>
|
|
326
|
+
</a>
|
|
327
|
+
</div>
|
|
328
|
+
</div>
|
|
329
|
+
</div>
|
|
330
|
+
`
|
|
331
|
+
)
|
|
332
|
+
: ''}
|
|
333
|
+
<!-- start: ads -->
|
|
334
|
+
<slot></slot>
|
|
335
|
+
<!-- end: ads -->
|
|
336
|
+
</div>
|
|
337
|
+
<!-- end: right-post-loop -->
|
|
338
|
+
</div>
|
|
339
|
+
</div>
|
|
340
|
+
`
|
|
341
|
+
}
|
|
342
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# kompasid-widget-recirculations-list
|
|
2
|
+
|
|
3
|
+
Komponen ini adalah komponen web berbasis LitElement yang digunakan untuk menampilkan daftar aliran artikel dari Kompas.id.
|
|
4
|
+
|
|
5
|
+
### Contoh Implementasi
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
const widgetRelatedPost = {
|
|
9
|
+
navigation: {
|
|
10
|
+
name: 'Artikel Terkait',
|
|
11
|
+
permalink: undefined
|
|
12
|
+
},
|
|
13
|
+
permalinkArticle: 'https://www.kompas.id/baca/opini/2024/05/02/pesan-bung-karno-jaga-persatuan-dan-keutuhan?open_from=Section_Artikel_Lainnya',
|
|
14
|
+
slugs: 'persatuan,bung-karno, surat pembaca, eduard lukman, bharoto, wira hardiprakoso, vision'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const widgetOtherPost = {
|
|
18
|
+
navigation: {
|
|
19
|
+
name: 'Lainnya Dalam Opini',
|
|
20
|
+
permalink: '/kategori/opini'
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
<div>
|
|
25
|
+
<kompasid-widget-recirculations-default
|
|
26
|
+
.navigation=${widgetRelatedPost.navigation}
|
|
27
|
+
.permalinkArticle=${widgetRelatedPost.permalinkArticle}
|
|
28
|
+
.slugs=${widgetRelatedPost.slugs}
|
|
29
|
+
></kompasid-widget-recirculations-default>
|
|
30
|
+
|
|
31
|
+
<kompasid-widget-recirculations-default
|
|
32
|
+
.navigation=${widgetOtherPost.navigation}
|
|
33
|
+
type='otherArticle'
|
|
34
|
+
mainCategory='opini'
|
|
35
|
+
></kompasid-widget-recirculations-default>
|
|
36
|
+
</div>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Properti
|
|
40
|
+
|
|
41
|
+
| Property | Attribute | Deskripsi | Tipe | Default | Konten |
|
|
42
|
+
| ------------------ | --------------- | -------------------------------------------------------------------------------------------------- | -------- | --------------- | ------------------------------------------- |
|
|
43
|
+
| `navigation` | `navigation` | Navigasi yang berisi nama dan tautan ke kategori atau halaman terkait. Mengecek value `name` dan `permalink` | `Object` | `undefined` | `Navigation` |
|
|
44
|
+
| `userGuid` | `userGuid` | GUID pengguna yang sedang menggunakan aplikasi untuk fetch data artikel terkait. | `String` | `'0'` | |
|
|
45
|
+
| `slugs` | `slugs` | Daftar slug kategori atau tag yang terkait dengan artikel untuk artikel terkait. | `String` | `''` | |
|
|
46
|
+
| `permalinkArticle` | `permalinkArticle` | Tautan kategori artikel yang sedang ditampilkan atau dibaca untuk rekomendasi artikel terkait. | `String` | `''` | |
|
|
47
|
+
| `type` | `type` | Tipe widget untuk membedakan antara pengambilan data artikel terkait (`relatedArticle`) atau lainnya dalam kategori (`otherArticle`). | `String` | `'relatedArticle'` | `"relatedArticle" \| "otherArticle"` |
|
|
48
|
+
| `mainCategory` | `mainCategory` | Kategori utama artikel yang akan ditampilkan jika menggunakan tipe `otherArticle` untuk lainnya dalam kategori. | `String` | `''` | |
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
### Digunakan oleh
|
|
52
|
+
|
|
53
|
+
- [kompasid-widget-recirculations-default](../kompasid-widget-recirculations-default)
|
|
54
|
+
|
|
55
|
+
## Kontributor
|
|
56
|
+
|
|
57
|
+
*Dokumentasi ini dibuat oleh tim front-end Kompas.id.*
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
interface PostTag {
|
|
2
|
+
slug: string
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export interface Post {
|
|
6
|
+
title: string
|
|
7
|
+
isAnalisis?: boolean
|
|
8
|
+
isEksklusif?: boolean
|
|
9
|
+
isFreemium?: boolean
|
|
10
|
+
featuredImageThumbnailMedium: string
|
|
11
|
+
permalink: string
|
|
12
|
+
publishedDate: string
|
|
13
|
+
postTag?: PostTag[]
|
|
14
|
+
thumbnails: {
|
|
15
|
+
sizes: {
|
|
16
|
+
thumbnailMedium: {
|
|
17
|
+
permalink: string
|
|
18
|
+
width: number
|
|
19
|
+
height: number
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface Navigation {
|
|
26
|
+
name: string
|
|
27
|
+
permalink: string
|
|
28
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { html, css, LitElement } from 'lit'
|
|
2
|
+
import { customElement, property } from 'lit/decorators.js'
|
|
3
|
+
import { format } from 'date-fns'
|
|
4
|
+
import { id } from 'date-fns/locale/id'
|
|
5
|
+
import { TWStyles } from '../../../tailwind/tailwind.js'
|
|
6
|
+
|
|
7
|
+
interface PostTag {
|
|
8
|
+
slug: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface ListItem {
|
|
12
|
+
title: string
|
|
13
|
+
isAnalisis?: boolean
|
|
14
|
+
isEksklusif?: boolean
|
|
15
|
+
isFreemium?: boolean
|
|
16
|
+
permalink: string
|
|
17
|
+
publishedDate?: string
|
|
18
|
+
postTag?: PostTag[]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@customElement('kompasid-widget-recirculations-list')
|
|
22
|
+
export class KompasWidgetRecirculationsList extends LitElement {
|
|
23
|
+
@property({ type: String }) widgetTitle = ''
|
|
24
|
+
@property({ type: Array }) articleList: ListItem[] = []
|
|
25
|
+
@property({ type: String }) accessToken = ''
|
|
26
|
+
@property({ type: String }) apiSlug = ''
|
|
27
|
+
|
|
28
|
+
static styles = [
|
|
29
|
+
css`
|
|
30
|
+
:host {
|
|
31
|
+
font-family: 'PT Sans', sans-serif;
|
|
32
|
+
}
|
|
33
|
+
.chip {
|
|
34
|
+
align-items: center;
|
|
35
|
+
border-radius: 0.25rem;
|
|
36
|
+
display: flex;
|
|
37
|
+
font-family: 'PT Sans', sans-serif;
|
|
38
|
+
font-size: 0.75rem;
|
|
39
|
+
font-weight: bold;
|
|
40
|
+
height: 1.5rem;
|
|
41
|
+
justify-content: center;
|
|
42
|
+
line-height: 1rem;
|
|
43
|
+
margin-bottom: 0.25rem;
|
|
44
|
+
padding: 0.375rem 0.5rem;
|
|
45
|
+
}
|
|
46
|
+
`,
|
|
47
|
+
TWStyles,
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
async connectedCallback() {
|
|
51
|
+
super.connectedCallback()
|
|
52
|
+
try {
|
|
53
|
+
await this.fetchAccessToken()
|
|
54
|
+
await this.fetchArticles()
|
|
55
|
+
} catch (error) {
|
|
56
|
+
this.handleFetchError(error)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async fetchAccessToken() {
|
|
61
|
+
const response = await fetch(
|
|
62
|
+
'https://api.kompas.id/account/api/v1/login/guest',
|
|
63
|
+
{
|
|
64
|
+
method: 'POST',
|
|
65
|
+
headers: {
|
|
66
|
+
'Content-Type': 'application/json',
|
|
67
|
+
},
|
|
68
|
+
body: JSON.stringify({
|
|
69
|
+
email: 'anonynous.user@kompas.id',
|
|
70
|
+
}),
|
|
71
|
+
}
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
const result = await response.json()
|
|
75
|
+
|
|
76
|
+
if (result?.data?.accessToken) {
|
|
77
|
+
this.accessToken = result.data.accessToken
|
|
78
|
+
} else {
|
|
79
|
+
throw new Error('Token akses tidak tersedia dalam respons')
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async fetchArticles() {
|
|
84
|
+
if (!this.accessToken) {
|
|
85
|
+
throw new Error('Token akses tidak tersedia')
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const endpoint = `https://cds.kompas.id/api/v1/article/${this.apiSlug}`
|
|
89
|
+
|
|
90
|
+
const response = await fetch(endpoint, {
|
|
91
|
+
headers: {
|
|
92
|
+
Authorization: `Bearer ${this.accessToken}`,
|
|
93
|
+
'Content-Type': 'application/json',
|
|
94
|
+
},
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
const result = await response.json()
|
|
98
|
+
|
|
99
|
+
if (result?.result) {
|
|
100
|
+
this.articleList = result.result.map(
|
|
101
|
+
(article: {
|
|
102
|
+
postTag?: never[] | undefined
|
|
103
|
+
isFreemium?: false | undefined
|
|
104
|
+
title: any
|
|
105
|
+
permalink: any
|
|
106
|
+
publishedDate: any
|
|
107
|
+
}) => {
|
|
108
|
+
const {
|
|
109
|
+
postTag = [],
|
|
110
|
+
isFreemium = false,
|
|
111
|
+
title,
|
|
112
|
+
permalink,
|
|
113
|
+
publishedDate,
|
|
114
|
+
} = article
|
|
115
|
+
|
|
116
|
+
const isAnalisis = postTag.some(
|
|
117
|
+
(tag: { slug: string }) => tag.slug === 'analisis'
|
|
118
|
+
)
|
|
119
|
+
const isEksklusif = postTag.some(
|
|
120
|
+
(tag: { slug: string }) => tag.slug === 'eksklusif'
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
isAnalisis,
|
|
125
|
+
isEksklusif,
|
|
126
|
+
isFreemium,
|
|
127
|
+
title,
|
|
128
|
+
permalink,
|
|
129
|
+
publishedDate,
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
)
|
|
133
|
+
} else {
|
|
134
|
+
throw new Error('Data artikel populer tidak ditemukan')
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
formatDate(date: string) {
|
|
139
|
+
return format(new Date(date), "dd MMMM yyyy' · 'HH:mm' WIB", { locale: id })
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
handleFetchError(error: unknown) {
|
|
143
|
+
const errorMessage =
|
|
144
|
+
error instanceof Error ? error.message : 'Kesalahan tidak diketahui'
|
|
145
|
+
alert(`Terjadi kesalahan: ${errorMessage}`)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
renderChips(item: ListItem) {
|
|
149
|
+
const chips = []
|
|
150
|
+
|
|
151
|
+
if (item.isAnalisis) {
|
|
152
|
+
chips.push(
|
|
153
|
+
html`
|
|
154
|
+
<div class="flex">
|
|
155
|
+
<div class="chip bg-orange-100 text-orange-500">Analisis</div>
|
|
156
|
+
</div>
|
|
157
|
+
`
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (item.isEksklusif) {
|
|
162
|
+
chips.push(
|
|
163
|
+
html`
|
|
164
|
+
<div class="flex">
|
|
165
|
+
<div class="chip bg-grey-600 text-white">
|
|
166
|
+
<img
|
|
167
|
+
src="https://cdn-www.kompas.id/assets/img/icons/kompas-icon-small.svg"
|
|
168
|
+
alt="Kompas Icon"
|
|
169
|
+
style="width: 16px; height: 16px; margin-right: 4px;"
|
|
170
|
+
/>
|
|
171
|
+
Eksklusif
|
|
172
|
+
</div>
|
|
173
|
+
</div>
|
|
174
|
+
`
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (item.isFreemium) {
|
|
179
|
+
chips.push(
|
|
180
|
+
html`
|
|
181
|
+
<div class="flex">
|
|
182
|
+
<div class="chip bg-blue-100 text-brand-1">Bebas Akses</div>
|
|
183
|
+
</div>
|
|
184
|
+
`
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return chips
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
render() {
|
|
192
|
+
return html`
|
|
193
|
+
<div>
|
|
194
|
+
<div class="mb-6">
|
|
195
|
+
<h5 class="capitalize font-bold text-grey-600 font-sans">
|
|
196
|
+
${this.widgetTitle}
|
|
197
|
+
</h5>
|
|
198
|
+
</div>
|
|
199
|
+
|
|
200
|
+
<ul class="flex flex-col">
|
|
201
|
+
${this.articleList.slice(0, 5).map(
|
|
202
|
+
item =>
|
|
203
|
+
html`
|
|
204
|
+
<li class="flex flex-col items-start mb-3">
|
|
205
|
+
<div class="flex w-full">
|
|
206
|
+
<div class="flex flex-col w-full">
|
|
207
|
+
${this.renderChips(item)}
|
|
208
|
+
<a
|
|
209
|
+
href="${item.permalink}"
|
|
210
|
+
class="font-bold font-sans hover:underline leading-tight text-base text-grey-600"
|
|
211
|
+
>
|
|
212
|
+
${item.title}
|
|
213
|
+
</a>
|
|
214
|
+
<div
|
|
215
|
+
class="flex w-full flex-row items-center leading-normal justify-between text-sm text-grey-500 font-sans pt-2"
|
|
216
|
+
>
|
|
217
|
+
${item.publishedDate
|
|
218
|
+
? this.formatDate(item.publishedDate)
|
|
219
|
+
: 'Tanggal tidak tersedia'}
|
|
220
|
+
</div>
|
|
221
|
+
</div>
|
|
222
|
+
</div>
|
|
223
|
+
</li>
|
|
224
|
+
`
|
|
225
|
+
)}
|
|
226
|
+
</ul>
|
|
227
|
+
</div>
|
|
228
|
+
`
|
|
229
|
+
}
|
|
230
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# kompasid-widget-recirculations-list
|
|
2
|
+
|
|
3
|
+
Komponen ini adalah komponen web berbasis LitElement yang digunakan untuk menampilkan daftar aliran artikel dari Kompas.id.
|
|
4
|
+
|
|
5
|
+
### Element
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
<kompasid-widget-recirculations-list
|
|
9
|
+
widgetTitle="Terpopuler"
|
|
10
|
+
apiSlug="popular-article"
|
|
11
|
+
></kompasid-widget-recirculations-list>
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Properties
|
|
15
|
+
|
|
16
|
+
| Property | Attribute | Description | Tipe | Default | Content |
|
|
17
|
+
| -------------| -------------| ----------------------------------------------- | -------- | ------- | ---------------- |
|
|
18
|
+
| `widgetTitle`| `widgetTitle`| Judul untuk widget. | `string` | `''` | `"Terbaru" \| "Terpopuler"` |
|
|
19
|
+
| `apiSlug` | `apiSlug` | Slug yang digunakan untuk mengidentifikasi endpoint API untuk mengambil daftar artikel. | `string` | `''` | `"list" \| "popular-article"` |
|
|
20
|
+
|
|
21
|
+
### Used by
|
|
22
|
+
|
|
23
|
+
- [kompasid-widget-recirculations-list](../kompasid-widget-recirculations-list)
|
|
24
|
+
|
|
25
|
+
## Contributors
|
|
26
|
+
|
|
27
|
+
*Dokumentasi ini dibuat oleh tim front-end Kompas.id.*
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export { KompasWidgetRecirculationsDefault } from './components/kompasid-widget-recirculations-default/KompasWidgetRecirculationsDefault.js'
|
|
2
|
+
export { KompasWidgetRecirculationsList } from './components/kompasid-widget-recirculations-list/KompasWidgetRecirculationsList.js'
|
|
1
3
|
export { KompasPaywall } from './components/kompasid-paywall/KompasPaywall.js'
|
|
2
4
|
export { KompasFooter } from './components/kompasid-footer/KompasFooter.js'
|
|
3
5
|
export { KompasPaywallVideo } from './components/kompasid-paywall-video/KompasPaywallVideo.js'
|