Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

feat: manage channels form in settings

Christian Fei2499e3d97110
diff --git a/client/components/add-channel-form.js b/client/components/add-channel-form.js
index 7f4c798..c8e4583 100644
--- a/client/components/add-channel-form.js
+++ b/client/components/add-channel-form.js
@@ -47,9 +47,10 @@ class AddChannelForm extends HTMLElement {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: channelName })
})
- .then(() => {
+ .then(res => res.text())
+ .then((text) => {
form.reset()
- status.innerText = `Successfully added ${channelName}`
+ status.innerText = text // `Successfully added ${channelName}`
})
.catch(error => {
console.error('Error adding channel:', error)
diff --git a/client/components/manage-channels-form.js b/client/components/manage-channels-form.js
new file mode 100644
index 0000000..f1ff8d5
--- /dev/null
+++ b/client/components/manage-channels-form.js
@@ -0,0 +1,64 @@
+class ManageChannelsForm extends HTMLElement {
+ constructor () {
+ super()
+ }
+ connectedCallback () {
+ this.render()
+ this.fetchData()
+ }
+ disconnectedCallback () {
+ this.unregisterEvents()
+ }
+ fetchData() {
+ fetch('/api/channels')
+ .then(res => res.json())
+ .then(channels => {
+ const $container = this.querySelector('#manage-channels')
+ $container.innerHTML = /*html*/`
+ ${channels.map(channel =>
+ /*html*/`<div style="margin-bottom: 10px" tabindex=0 data-channel="${channel.name}" class="channel">❌ ${channel.name}</div>`).join('')}
+ `
+ })
+ .then(this.registerEvents.bind(this))
+ }
+ registerEvents () {
+ this.querySelectorAll('[data-channel]')
+ .forEach($channel => window.utils.addClickListener($channel, this.removeChannel.bind(this)))
+ }
+ unregisterEvents () {
+ this.querySelectorAll('[data-channel]')
+ .forEach($channel => window.utils.removeClickListener($channel, this.removeChannel.bind(this)))
+ }
+ render () {
+ this.innerHTML = /*html*/`
+ <details>
+ <summary>Manage channels</summary>
+ <br>
+ <div class="flex space-between" style="flex-flow: wrap;" id="manage-channels"></div>
+ </details>
+ `
+ }
+
+ removeChannel (event) {
+ event.preventDefault()
+
+ const $channel = event.target
+ const channelName = $channel.dataset['channel']
+ if (!channelName) return alert('empty channel name')
+ if (!confirm(`Do you want to delete the channel ${channelName}?`)) return
+
+ fetch('/api/channels', {
+ method: 'DELETE',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ name: channelName })
+ })
+ .then(() => {
+ $channel.remove()
+ })
+ .catch(error => {
+ console.error('Error deleting channel:', error)
+ status.innerText = `There was an error deleting the channel ${channelName}, please check your application logs`
+ })
+ }
+}
+customElements.define('manage-channels-form', ManageChannelsForm)
diff --git a/client/index.html b/client/index.html
index e8ead55..69877ab 100644
--- a/client/index.html
+++ b/client/index.html
@@ -74,6 +74,8 @@
<input type="checkbox" id="show-original-thumbnail"/>
<div><small>Show the creators chosen thumbnail, or a neutral one based on the actual video frames</small></div>
</form>
+ <br>
+ <manage-channels-form></manage-channels-form>
</div>
</template>
@@ -93,6 +95,7 @@
<script type="module" src="/components/channels-list.js"></script>
<script type="module" src="/components/video-element.js"></script>
<script type="module" src="/components/add-channel-form.js"></script>
+ <script type="module" src="/components/manage-channels-form.js"></script>
<script type="module" src="/components/video-quality-form.js"></script>
<script type="module" src="/components/manage-disk-space-form.js"></script>
<script type="module" src="/main.js"></script>
diff --git a/lib/repository.js b/lib/repository.js
index b94b0fb..2b78c1a 100644
--- a/lib/repository.js
+++ b/lib/repository.js
@@ -53,7 +53,12 @@ export default class Repository {
}
addChannel (name) {
this.channels.push({name})
- console.log('adding channel', name)
+ console.log('added channel', name)
+ this.saveChannels()
+ }
+ deleteChannel (name) {
+ this.channels.splice(this.channels.findIndex(c => c.name === name), 1)
+ console.log('removed channel', name)
this.saveChannels()
}
saveChannels() {
diff --git a/lib/server.js b/lib/server.js
index 3d04610..2ea9353 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -39,6 +39,8 @@ export function createServer ({repo, connections = []}) {
/* api */
if (url.pathname === '/api/channels' && req.method === 'POST')
return addChannelHandler(req, res, repo, connections)
+ if (url.pathname === '/api/channels' && req.method === 'DELETE')
+ return deleteChannelHandler(req, res, repo, connections)
if (url.pathname === '/api/channels' && req.method === 'GET')
return getChannelHandler(req, res, repo, connections)
if (url.pathname === '/api/download-video' && req.method === 'POST')
@@ -81,6 +83,8 @@ export function createServer ({repo, connections = []}) {
return fileHandler('client/components/video-element.js', 'application/javascript')(req, res)
if (url.pathname === '/components/add-channel-form.js')
return fileHandler('client/components/add-channel-form.js', 'application/javascript')(req, res)
+ if (url.pathname === '/components/manage-channels-form.js')
+ return fileHandler('client/components/manage-channels-form.js', 'application/javascript')(req, res)
if (url.pathname === '/components/video-quality-form.js')
return fileHandler('client/components/video-quality-form.js', 'application/javascript')(req, res)
if (url.pathname === '/components/manage-disk-space-form.js')
@@ -129,7 +133,7 @@ async function addChannelHandler(req, res, repo, connections = []) {
name = name.startsWith('@') ? name.substring(1) : name
if (repo.channelExists(name)) {
- res.writeHead(409, { 'Content-Type': 'text/plain' })
+ res.writeHead(409)
return res.end('Channel already added')
}
@@ -137,14 +141,29 @@ async function addChannelHandler(req, res, repo, connections = []) {
if (Array.isArray(videos)) {
repo.addChannel(name)
broadcastSSE(JSON.stringify({type: 'new-videos', name, videos}), connections)
- res.writeHead(201, { 'Content-Type': 'text/plain' })
+ res.writeHead(201)
return res.end('Channel added')
}
- res.writeHead(404, { 'Content-Type': 'text/plain' })
+ res.writeHead(404)
return res.end('Channel not found')
}
+async function deleteChannelHandler(req, res, repo, connections = []) {
+ const body = await getBody(req)
+ let { name } = JSON.parse(body)
+ name = name.trim()
+
+ if (!repo.channelExists(name)) {
+ res.writeHead(409, { 'Content-Type': 'text/plain' })
+ return res.end('Channel does not exist')
+ }
+
+ repo.deleteChannel(name)
+ res.writeHead(200, { 'Content-Type': 'text/plain' })
+ return res.end('Channel deleted')
+}
+
async function getChannelHandler(req, res, repo, connections = []) {
const channels = repo.getChannels()
res.writeHead(200, { 'Content-Type': 'application/json' })