-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor-details.js
142 lines (125 loc) · 4.18 KB
/
actor-details.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict'
function viewActorDetails(API, target) {
let collapseEl = document.querySelector('#info-collapse')
if ($(collapseEl).hasClass('show')) {
$(collapseEl).collapse('hide')
}
let actorId = $(target).attr('data-actor-id')
const ActorDetailsRequest = retrieveActorDetails(API, actorId)
ActorDetailsRequest.done((data) => {
const detailsLoaded = populateActorDetails(data)
detailsLoaded.done(() => {
$(collapseEl).collapse('show')
})
})
}
function retrieveActorDetails(API, actorId) {
const request = $.Deferred()
$.get({
url: `https://api.themoviedb.org/3/person/${actorId}?api_key=${API.key}&language=en-US&append_to_response=movie_credits%2Cimages`,
success: (data) => {
console.log(data)
request.resolve(data)
},
}).fail(() => {
console.error('Failed to retrieve actor details.')
request.reject()
})
return request.promise()
}
function populateActorDetails(actor) {
const completed = $.Deferred()
$('#actor-details').attr('data-actor-id', actor.id).removeClass('hidden')
$('#actor-details #portrait')
.attr('src', `https://image.tmdb.org/t/p/original${actor.profile_path}`)
.addClass('w-100')
$('#actor-details #name').text(actor.name)
setBiographyString(actor.biography)
if (actor.birthday) {
$('#actor-details #age').text(
getActorAgeString(actor.birthday, actor.deathday)
)
} else {
hideInfo('#actor-details #age')
}
if (actor.place_of_birth) {
$('#actor-details #p-o-b').text(actor.place_of_birth)
} else {
hideInfo('#actor-details #p-o-b')
}
populateActorCredits(actor.movie_credits.cast)
completed.resolve()
return completed.promise()
}
function getActorAgeString(bDay, dDay) {
bDay = bDay.split('-')
const bDate = new Date(...bDay)
const options = {year: 'numeric', month: 'long', day: 'numeric'}
let string = `Born ${bDate.toLocaleDateString(undefined, options)}`
let age
if (dDay !== null) {
const dDate = new Date(endDay)
age = calcAge(bDate.getTime(), dDate.getTime())
return (string += `, died ${dDate.toLocaleDateString(
undefined,
options
)}} at ${age}`)
} else {
age = calcAge(bDate.getTime(), Date.now())
return (string += ` (${age})`)
}
}
function calcAge(bDate, endDate) {
const ageDifMs = endDate - bDate
const ageDate = new Date(ageDifMs) // miliseconds from epoch
return Math.abs(ageDate.getUTCFullYear() - 1970)
}
function setBiographyString(bio) {
if (bio.includes('Description above from')) {
bio = bio.split('Description above from ')
$('#actor-details #bio').text(bio[0])
$('#actor-details #bio-src').text(`Description above from ${bio[1]}`)
} else {
$('#actor-details #bio').text(bio)
hideInfo($('#actor-details #bio-src'))
}
}
function populateActorCredits(credits) {
const container = $('#actor-details #actor-credits')
credits = credits.sort(sortCreditDate)
for (let credit of credits) {
if (credit.character) {
container.append(
$(document.createElement('li'))
.addClass('d-flex align-items-center cast-credit')
.attr('data-movie-id', credit.id)
.html(getActorCreditString(credit))
)
} else {
continue
}
}
}
function sortCreditDate(a, b) {
if (!a.release_date) {
return -1
} else {
a = a.release_date.split('-')
const aDate = new Date(...a)
b = b.release_date.split('-')
const bDate = new Date(...b)
return bDate - aDate
}
}
function getActorCreditString(credit) {
const character = `<div><p class="my-0">${credit.character}</p>`
const title = `<p class="my-0 fst-italic lh-sm">${credit.title}</p6></div>`
let release
if (credit.release_date) {
release = credit.release_date.substr(0, 4)
} else {
release = 'TBA'
}
const date = `<p class="my-0 ms-auto justify-self-end">${release}</p>`
return character + title + date
}