diff options
Diffstat (limited to 'client/src/standalone/videos/embed.ts')
-rw-r--r-- | client/src/standalone/videos/embed.ts | 44 |
1 files changed, 39 insertions, 5 deletions
diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts index a99bc586f..aa418d2d4 100644 --- a/client/src/standalone/videos/embed.ts +++ b/client/src/standalone/videos/embed.ts | |||
@@ -9,19 +9,53 @@ function getVideoUrl (id: string) { | |||
9 | return window.location.origin + '/api/v1/videos/' + id | 9 | return window.location.origin + '/api/v1/videos/' + id |
10 | } | 10 | } |
11 | 11 | ||
12 | async function loadVideoInfo (videoId: string): Promise<VideoDetails> { | 12 | function loadVideoInfo (videoId: string): Promise<Response> { |
13 | const response = await fetch(getVideoUrl(videoId)) | 13 | return fetch(getVideoUrl(videoId)) |
14 | return response.json() | 14 | } |
15 | |||
16 | function removeElement (element: HTMLElement) { | ||
17 | element.parentElement.removeChild(element) | ||
18 | } | ||
19 | |||
20 | function displayError (videoElement: HTMLVideoElement, text: string) { | ||
21 | // Remove video element | ||
22 | removeElement(videoElement) | ||
23 | |||
24 | document.title = 'Sorry - ' + text | ||
25 | |||
26 | const errorBlock = document.getElementById('error-block') | ||
27 | errorBlock.style.display = 'flex' | ||
28 | |||
29 | const errorText = document.getElementById('error-content') | ||
30 | errorText.innerHTML = text | ||
31 | } | ||
32 | |||
33 | function videoNotFound (videoElement: HTMLVideoElement) { | ||
34 | const text = 'This video does not exist.' | ||
35 | displayError(videoElement, text) | ||
36 | } | ||
37 | |||
38 | function videoFetchError (videoElement: HTMLVideoElement) { | ||
39 | const text = 'We cannot fetch the video. Please try again later.' | ||
40 | displayError(videoElement, text) | ||
15 | } | 41 | } |
16 | 42 | ||
17 | const urlParts = window.location.href.split('/') | 43 | const urlParts = window.location.href.split('/') |
18 | const videoId = urlParts[urlParts.length - 1] | 44 | const videoId = urlParts[urlParts.length - 1] |
19 | 45 | ||
20 | loadVideoInfo(videoId) | 46 | loadVideoInfo(videoId) |
21 | .then(videoInfo => { | 47 | .then(async response => { |
22 | const videoContainerId = 'video-container' | 48 | const videoContainerId = 'video-container' |
23 | |||
24 | const videoElement = document.getElementById(videoContainerId) as HTMLVideoElement | 49 | const videoElement = document.getElementById(videoContainerId) as HTMLVideoElement |
50 | |||
51 | if (!response.ok) { | ||
52 | if (response.status === 404) return videoNotFound(videoElement) | ||
53 | |||
54 | return videoFetchError(videoElement) | ||
55 | } | ||
56 | |||
57 | const videoInfo: VideoDetails = await response.json() | ||
58 | |||
25 | let autoplay = false | 59 | let autoplay = false |
26 | let startTime = 0 | 60 | let startTime = 0 |
27 | 61 | ||