| 1 |
import BaseModel from './base-model'; |
| 2 |
import Document from './document'; |
| 3 |
|
| 4 |
/** |
| 5 |
* @typedef {import('./content-type')} ContentType |
| 6 |
*/ |
| 7 |
export default class Kit extends BaseModel { |
| 8 |
id = ''; |
| 9 |
title = ''; |
| 10 |
description = ''; |
| 11 |
isFavorite = false; |
| 12 |
thumbnailUrl = null; |
| 13 |
previewUrl = ''; |
| 14 |
accessLevel = 0; |
| 15 |
trendIndex = null; |
| 16 |
popularityIndex = null; |
| 17 |
featuredIndex = null; |
| 18 |
createdAt = null; |
| 19 |
updatedAt = null; |
| 20 |
keywords = []; |
| 21 |
taxonomies = []; |
| 22 |
documents = []; |
| 23 |
|
| 24 |
/** |
| 25 |
* Create a kit from server response |
| 26 |
* |
| 27 |
* @param {Kit} kit |
| 28 |
*/ |
| 29 |
static createFromResponse( kit ) { |
| 30 |
return new Kit().init( { |
| 31 |
id: kit.id, |
| 32 |
title: kit.title, |
| 33 |
description: kit.description, |
| 34 |
isFavorite: kit.is_favorite, |
| 35 |
thumbnailUrl: kit.thumbnail_url, |
| 36 |
previewUrl: kit.preview_url, |
| 37 |
accessLevel: kit.access_level, |
| 38 |
trendIndex: kit.trend_index, |
| 39 |
popularityIndex: kit.popularity_index, |
| 40 |
featuredIndex: kit.featured_index, |
| 41 |
// TODO: Remove when the API is stable (when date params always exists) |
| 42 |
createdAt: kit.created_at ? new Date( kit.created_at ) : null, |
| 43 |
updatedAt: kit.updated_at ? new Date( kit.updated_at ) : null, |
| 44 |
// |
| 45 |
keywords: kit.keywords, |
| 46 |
taxonomies: kit.taxonomies, |
| 47 |
documents: kit.documents |
| 48 |
? kit.documents.map( ( document ) => Document.createFromResponse( document ) ) |
| 49 |
: [], |
| 50 |
} ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get content types as param and group all the documents based on it. |
| 55 |
* |
| 56 |
* @param {ContentType[]} contentTypes |
| 57 |
* @return {ContentType[]} content types |
| 58 |
*/ |
| 59 |
getDocumentsByTypes( contentTypes ) { |
| 60 |
return contentTypes.map( ( contentType ) => { |
| 61 |
contentType = contentType.clone(); |
| 62 |
|
| 63 |
contentType.documents = this.documents |
| 64 |
.filter( ( document ) => contentType.documentTypes.includes( document.documentType ) ); |
| 65 |
|
| 66 |
return contentType; |
| 67 |
} ); |
| 68 |
} |
| 69 |
} |
| 70 |
|