Piped/src/components/PlaylistAddModal.vue

93 lines
2.8 KiB
Vue
Raw Normal View History

2022-04-07 02:33:25 +00:00
<template>
2023-08-22 07:20:40 +00:00
<ModalComponent @close="$emit('close')">
<span v-t="'actions.select_playlist'" class="inline-block w-max text-2xl" />
<select v-model="selectedPlaylist" class="select mt-3 w-full">
2023-07-27 11:46:05 +00:00
<option v-for="playlist in playlists" :key="playlist.id" :value="playlist.id" v-text="playlist.name" />
2022-08-28 17:40:35 +00:00
</select>
<div class="mt-3 w-full flex justify-between">
2023-07-27 11:46:05 +00:00
<button ref="addButton" v-t="'actions.create_playlist'" class="btn" @click="onCreatePlaylist" />
2022-08-28 17:40:35 +00:00
<button
ref="addButton"
v-t="'actions.add_to_playlist'"
2023-07-27 11:46:05 +00:00
class="btn"
@click="handleClick(selectedPlaylist)"
2022-08-28 17:40:35 +00:00
/>
</div>
</ModalComponent>
2022-04-07 02:33:25 +00:00
</template>
<script>
2022-08-28 17:40:35 +00:00
import ModalComponent from "./ModalComponent.vue";
2022-04-07 02:33:25 +00:00
export default {
2022-08-28 17:40:35 +00:00
components: {
ModalComponent,
},
2022-04-07 02:33:25 +00:00
props: {
videoInfo: {
type: Object,
required: true,
},
2022-04-07 02:33:25 +00:00
videoId: {
type: String,
2022-04-07 02:33:25 +00:00
required: true,
},
},
2023-07-27 11:46:05 +00:00
emits: ["close"],
2022-04-07 02:33:25 +00:00
data() {
return {
playlists: [],
selectedPlaylist: null,
processing: false,
2022-04-07 02:33:25 +00:00
};
},
mounted() {
this.fetchPlaylists();
this.selectedPlaylist = this.getPreferenceString("selectedPlaylist" + this.hashCode(this.authApiUrl()));
window.addEventListener("keydown", this.handleKeyDown);
window.blur();
},
unmounted() {
window.removeEventListener("keydown", this.handleKeyDown);
2022-04-07 02:33:25 +00:00
},
methods: {
handleKeyDown(event) {
2022-08-28 17:40:35 +00:00
if (event.code === "Enter") {
this.handleClick(this.selectedPlaylist);
2022-08-28 17:40:35 +00:00
event.preventDefault();
}
},
2022-04-07 02:33:25 +00:00
handleClick(playlistId) {
if (!playlistId) {
alert(this.$t("actions.please_select_playlist"));
return;
}
if (this.processing) return;
2022-04-07 02:33:25 +00:00
this.$refs.addButton.disabled = true;
this.processing = true;
2022-04-07 02:33:25 +00:00
this.addVideosToPlaylist(playlistId, [this.videoId], [this.videoInfo]).then(json => {
this.setPreference("selectedPlaylist" + this.hashCode(this.authApiUrl()), playlistId);
2022-04-07 02:33:25 +00:00
this.$emit("close");
if (json.error) alert(json.error);
});
},
async fetchPlaylists() {
this.getPlaylists().then(json => {
2022-04-07 02:33:25 +00:00
this.playlists = json;
});
},
onCreatePlaylist() {
const name = prompt(this.$t("actions.create_playlist"));
if (!name) return;
this.createPlaylist(name).then(json => {
if (json.error) alert(json.error);
else this.fetchPlaylists();
});
},
2022-04-07 02:33:25 +00:00
},
};
</script>