Skip to content

Commit f8437a6

Browse files
committed
refactor: extract video-quality-form
1 parent fcba172 commit f8437a6

File tree

4 files changed

+65
-38
lines changed

4 files changed

+65
-38
lines changed
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class VideoQualityForm extends HTMLElement {
2+
constructor () {
3+
super()
4+
}
5+
connectedCallback () {
6+
this.render()
7+
this.registerEvents()
8+
9+
window.fetch('/api/video-quality', {
10+
method: 'GET'
11+
}).then(response => response.json())
12+
.then((videoQuality) => {
13+
console.log('video quality', videoQuality)
14+
this.querySelector('#video-quality').value = +videoQuality
15+
})
16+
.catch(error => console.error('Error:', error))
17+
}
18+
disconnectedCallback () {
19+
this.unregisterEvents()
20+
}
21+
registerEvents () {
22+
this.querySelector('#video-quality')
23+
.addEventListener('change', this.setVideoQualityHandler.bind(this))
24+
}
25+
unregisterEvents () {
26+
this.querySelector('#video-quality')
27+
.removeEventListener('change', this.setVideoQualityHandler.bind(this))
28+
}
29+
render () {
30+
this.innerHTML = /*html*/`
31+
<form>
32+
<div class="flex space-between">
33+
<div>
34+
Video quality
35+
</div>
36+
<select name="video-quality" id="video-quality">
37+
<option value="360">360</option>
38+
<option value="480">480</option>
39+
<option value="720">720</option>
40+
<option value="1080">1080</option>
41+
<option value="1440">1440</option>
42+
<option value="2160">2160</option>
43+
</select>
44+
</div>
45+
</form>
46+
`
47+
}
48+
49+
setVideoQualityHandler (event) {
50+
event.preventDefault()
51+
console.log('change video quality', event.target.value)
52+
window.fetch('/api/video-quality', {
53+
method: 'POST',
54+
headers: {
55+
'Content-Type': 'application/json'
56+
},
57+
body: event.target.value
58+
})
59+
}
60+
}
61+
customElements.define('video-quality-form', VideoQualityForm)

client/index.html

+2-15
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,7 @@ <h2>Settings </h2>
6868
<div><small>Show the creators chosen thumbnail, or a neutral one based on the actual video frames</small></div>
6969
</form>
7070
<br>
71-
<form>
72-
<div class="flex space-between">
73-
<div>
74-
Video quality
75-
</div>
76-
<select name="video-quality" id="video-quality">
77-
<option value="360">360</option>
78-
<option value="480">480</option>
79-
<option value="720">720</option>
80-
<option value="1080">1080</option>
81-
<option value="1440">1440</option>
82-
<option value="2160">2160</option>
83-
</select>
84-
</div>
85-
</form>
71+
<video-quality-form></video-quality-form>
8672
<br>
8773
<form>
8874
<div class="flex space-between">
@@ -121,6 +107,7 @@ <h1>404 Not Found</h1>
121107
<script type="module" src="/components/channels-list.js"></script>
122108
<script type="module" src="/components/video-element.js"></script>
123109
<script type="module" src="/components/add-channel-form.js"></script>
110+
<script type="module" src="/components/video-quality-form.js"></script>
124111
<script type="module" src="/main.js"></script>
125112
<script type="module" src="/lib/router.js"></script>
126113
</body>

client/lib/router.js

-23
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,6 @@ const routes = {
6969
$showOriginalThumbnail.addEventListener('click', (event) => {
7070
store.toggle(store.showOriginalThumbnailKey)
7171
})
72-
73-
const $videoQuality = document.getElementById('video-quality')
74-
$videoQuality.addEventListener('change', (event) => {
75-
console.log('change video quality', event.target.value)
76-
window.fetch('/api/video-quality', {
77-
method: 'POST',
78-
headers: {
79-
'Content-Type': 'application/json'
80-
},
81-
body: event.target.value
82-
})
83-
})
84-
window.fetch('/api/video-quality', {
85-
method: 'GET'
86-
}).then(response => response.json())
87-
.then((videoQuality) => {
88-
console.log('video quality', videoQuality)
89-
$videoQuality.value = +videoQuality
90-
})
91-
.catch(error => console.error('Error:', error))
92-
93-
94-
9572

9673
const $reclaimDiskSpace = document.getElementById('reclaim-disk-space')
9774
const $diskUsage = document.getElementById('disk-usage')

lib/server.js

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export function createServer ({repo, connections = []}) {
7878
return fileHandler('client/components/video-element.js', 'application/javascript')(req, res)
7979
if (url.pathname === '/components/add-channel-form.js')
8080
return fileHandler('client/components/add-channel-form.js', 'application/javascript')(req, res)
81+
if (url.pathname === '/components/video-quality-form.js')
82+
return fileHandler('client/components/video-quality-form.js', 'application/javascript')(req, res)
8183
if (url.pathname === '/components/search-videos.js')
8284
return fileHandler('client/components/search-videos.js', 'application/javascript')(req, res)
8385
if (url.pathname === '/components/channels-list.js')

0 commit comments

Comments
 (0)