2022-06-30 22:07:54 +00:00
|
|
|
<script setup lang="ts">
|
2022-07-04 22:52:53 +00:00
|
|
|
import type { BackendError, Playlist, APIErrorResponse } from '~/types'
|
2022-06-30 22:07:54 +00:00
|
|
|
|
2022-07-04 22:52:53 +00:00
|
|
|
import { filter, sortBy, flow } from 'lodash-es'
|
2022-07-01 11:13:07 +00:00
|
|
|
import axios from 'axios'
|
2022-06-30 22:07:54 +00:00
|
|
|
import { useGettext } from 'vue3-gettext'
|
|
|
|
|
import Modal from '~/components/semantic/Modal.vue'
|
|
|
|
|
import PlaylistForm from '~/components/playlists/Form.vue'
|
|
|
|
|
import useLogger from '~/composables/useLogger'
|
|
|
|
|
import { useStore } from '~/store'
|
|
|
|
|
import { ref, computed, watch } from 'vue'
|
|
|
|
|
import { useRouter } from 'vue-router'
|
|
|
|
|
|
|
|
|
|
const logger = useLogger()
|
|
|
|
|
const store = useStore()
|
|
|
|
|
|
|
|
|
|
const showDuplicateTrackAddConfirmation = ref(false)
|
|
|
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
router.beforeEach(() => {
|
|
|
|
|
store.commit('playlists/showModal', false)
|
|
|
|
|
showDuplicateTrackAddConfirmation.value = false
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const playlists = computed(() => store.state.playlists.playlists)
|
|
|
|
|
const track = computed(() => store.state.playlists.modalTrack)
|
|
|
|
|
|
|
|
|
|
const { $pgettext } = useGettext()
|
|
|
|
|
const labels = computed(() => ({
|
|
|
|
|
addToPlaylist: $pgettext('Popup/Playlist/Table.Button.Tooltip/Verb', 'Add to this playlist'),
|
|
|
|
|
filterPlaylistField: $pgettext('Popup/Playlist/Form/Placeholder', 'Enter playlist name')
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
const playlistNameFilter = ref('')
|
|
|
|
|
|
|
|
|
|
const sortedPlaylists = computed(() => flow(
|
|
|
|
|
filter((playlist: Playlist) => playlist.name.match(new RegExp(playlistNameFilter.value, 'i')) !== null),
|
|
|
|
|
sortBy((playlist: Playlist) => { return playlist.modification_date })
|
|
|
|
|
)(playlists.value).reverse())
|
|
|
|
|
|
|
|
|
|
const formKey = ref(new Date().toString())
|
|
|
|
|
watch(() => store.state.playlists.showModal, () => {
|
|
|
|
|
formKey.value = new Date().toString()
|
|
|
|
|
showDuplicateTrackAddConfirmation.value = false
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const lastSelectedPlaylist = ref(-1)
|
2022-07-01 11:13:07 +00:00
|
|
|
const errors = ref([] as string[])
|
2022-06-30 22:07:54 +00:00
|
|
|
const duplicateTrackAddInfo = ref({} as { playlist_name?: string })
|
|
|
|
|
|
|
|
|
|
const addToPlaylist = async (playlistId: number, allowDuplicates: boolean) => {
|
|
|
|
|
lastSelectedPlaylist.value = playlistId
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await axios.post(`playlists/${playlistId}/add`, {
|
|
|
|
|
tracks: [track.value?.id].filter(i => i),
|
|
|
|
|
allow_duplicates: allowDuplicates
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
logger.info('Successfully added track to playlist')
|
|
|
|
|
store.state.playlists.showModal = false
|
|
|
|
|
store.dispatch('playlists/fetchOwn')
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error as BackendError) {
|
2022-07-01 11:13:07 +00:00
|
|
|
const { backendErrors, rawPayload = {} } = error as BackendError
|
2022-06-30 22:07:54 +00:00
|
|
|
|
2022-07-01 11:13:07 +00:00
|
|
|
// TODO (wvffle): Test if it works
|
|
|
|
|
// if (backendErrors.length === 1 && backendErrors[0].code === 'tracks_already_exist_in_playlist') {
|
|
|
|
|
if (backendErrors.length === 1 && backendErrors[0] === 'Tracks already exist in playlist') {
|
|
|
|
|
duplicateTrackAddInfo.value = ((rawPayload.playlist as APIErrorResponse).non_field_errors as APIErrorResponse)[0] as object
|
2022-06-30 22:07:54 +00:00
|
|
|
showDuplicateTrackAddConfirmation.value = true
|
|
|
|
|
} else {
|
|
|
|
|
errors.value = backendErrors
|
|
|
|
|
showDuplicateTrackAddConfirmation.value = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
2018-03-19 16:39:03 +00:00
|
|
|
<template>
|
2021-12-06 10:35:20 +00:00
|
|
|
<modal
|
2022-05-01 11:45:07 +00:00
|
|
|
v-model:show="$store.state.playlists.showModal"
|
2021-12-06 10:35:20 +00:00
|
|
|
>
|
2020-07-03 12:20:47 +00:00
|
|
|
<h4 class="header">
|
2019-12-07 16:06:24 +00:00
|
|
|
<template v-if="track">
|
|
|
|
|
<h2 class="ui header">
|
2021-12-06 10:35:20 +00:00
|
|
|
<translate translate-context="Popup/Playlist/Title/Verb">
|
|
|
|
|
Add to playlist
|
|
|
|
|
</translate>
|
2019-12-07 16:06:24 +00:00
|
|
|
<div
|
2022-07-04 22:52:53 +00:00
|
|
|
v-translate="{artist: track.artist?.name, title: track.title}"
|
2019-12-07 16:06:24 +00:00
|
|
|
class="ui sub header"
|
2019-03-20 10:36:16 +00:00
|
|
|
translate-context="Popup/Playlist/Paragraph"
|
2021-12-06 10:35:20 +00:00
|
|
|
:translate-params="{artist: track.artist.name, title: track.title}"
|
|
|
|
|
>
|
2018-06-30 13:28:47 +00:00
|
|
|
"%{ title }", by %{ artist }
|
2019-12-07 16:06:24 +00:00
|
|
|
</div>
|
|
|
|
|
</h2>
|
|
|
|
|
</template>
|
2021-12-06 10:35:20 +00:00
|
|
|
<translate
|
|
|
|
|
v-else
|
|
|
|
|
translate-context="Popup/Playlist/Title/Verb"
|
|
|
|
|
>
|
|
|
|
|
Manage playlists
|
|
|
|
|
</translate>
|
2020-07-03 12:20:47 +00:00
|
|
|
</h4>
|
2019-12-07 16:06:24 +00:00
|
|
|
<div class="scrolling content">
|
2021-12-06 10:35:20 +00:00
|
|
|
<playlist-form :key="formKey" />
|
|
|
|
|
<div class="ui divider" />
|
2019-12-07 16:06:24 +00:00
|
|
|
<div v-if="playlists.length > 0">
|
2021-12-06 10:35:20 +00:00
|
|
|
<div
|
|
|
|
|
v-if="showDuplicateTrackAddConfirmation"
|
|
|
|
|
role="alert"
|
|
|
|
|
class="ui warning message"
|
|
|
|
|
>
|
|
|
|
|
<p
|
2022-06-30 22:07:54 +00:00
|
|
|
v-translate="{track: track?.title, playlist: duplicateTrackAddInfo.playlist_name}"
|
2021-12-06 10:35:20 +00:00
|
|
|
translate-context="Popup/Playlist/Paragraph"
|
2022-06-30 22:07:54 +00:00
|
|
|
:translate-params="{track: track?.title, playlist: duplicateTrackAddInfo.playlist_name}"
|
2021-12-06 10:35:20 +00:00
|
|
|
>
|
|
|
|
|
<strong>%{ track }</strong> is already in <strong>%{ playlist }</strong>.
|
|
|
|
|
</p>
|
2019-04-24 09:31:46 +00:00
|
|
|
<button
|
2021-12-06 10:35:20 +00:00
|
|
|
class="ui small basic cancel button"
|
2022-06-30 22:07:54 +00:00
|
|
|
@click="showDuplicateTrackAddConfirmation = false"
|
2021-12-06 10:35:20 +00:00
|
|
|
>
|
|
|
|
|
<translate translate-context="*/*/Button.Label/Verb">
|
|
|
|
|
Cancel
|
|
|
|
|
</translate>
|
2019-04-24 09:31:46 +00:00
|
|
|
</button>
|
|
|
|
|
<button
|
2020-05-15 12:12:36 +00:00
|
|
|
class="ui small success button"
|
2021-12-06 10:35:20 +00:00
|
|
|
@click="addToPlaylist(lastSelectedPlaylist, true)"
|
|
|
|
|
>
|
|
|
|
|
<translate translate-context="*/Playlist/Button.Label/Verb">
|
|
|
|
|
Add anyways
|
|
|
|
|
</translate>
|
|
|
|
|
</button>
|
2019-04-24 09:31:46 +00:00
|
|
|
</div>
|
2021-12-06 10:35:20 +00:00
|
|
|
<div
|
|
|
|
|
v-if="errors.length > 0"
|
|
|
|
|
role="alert"
|
|
|
|
|
class="ui negative message"
|
|
|
|
|
>
|
|
|
|
|
<h4 class="header">
|
|
|
|
|
<translate translate-context="Popup/Playlist/Error message.Title">
|
|
|
|
|
The track can't be added to a playlist
|
|
|
|
|
</translate>
|
|
|
|
|
</h4>
|
2018-03-20 22:41:15 +00:00
|
|
|
<ul class="list">
|
2021-12-06 10:35:20 +00:00
|
|
|
<li
|
|
|
|
|
v-for="(error, key) in errors"
|
|
|
|
|
:key="key"
|
|
|
|
|
>
|
|
|
|
|
{{ error }}
|
|
|
|
|
</li>
|
2018-03-20 22:41:15 +00:00
|
|
|
</ul>
|
2018-03-19 16:39:03 +00:00
|
|
|
</div>
|
2021-12-06 10:35:20 +00:00
|
|
|
<h4 class="ui header">
|
|
|
|
|
<translate translate-context="Popup/Playlist/Title">
|
|
|
|
|
Available playlists
|
|
|
|
|
</translate>
|
|
|
|
|
</h4>
|
2019-12-07 16:06:24 +00:00
|
|
|
<div class="ui form">
|
|
|
|
|
<div class="fields">
|
|
|
|
|
<div class="field">
|
|
|
|
|
<label for="playlist-name-filter"><translate translate-context="Popup/Playlist/Label">Filter</translate></label>
|
2021-12-06 10:35:20 +00:00
|
|
|
<input
|
|
|
|
|
id="playlist-name-filter"
|
|
|
|
|
v-model="playlistNameFilter"
|
|
|
|
|
type="text"
|
|
|
|
|
class="inline"
|
|
|
|
|
:placeholder="labels.filterPlaylistField"
|
|
|
|
|
>
|
2019-12-07 16:06:24 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2021-12-06 10:35:20 +00:00
|
|
|
<table
|
|
|
|
|
v-if="sortedPlaylists.length > 0"
|
|
|
|
|
class="ui unstackable very basic table"
|
|
|
|
|
>
|
2018-03-20 22:41:15 +00:00
|
|
|
<thead>
|
|
|
|
|
<tr>
|
2020-08-01 09:11:51 +00:00
|
|
|
<th><span class="visually-hidden"><translate translate-context="*/*/*/Verb">Edit</translate></span></th>
|
2021-12-06 10:35:20 +00:00
|
|
|
<th>
|
|
|
|
|
<translate translate-context="*/*/*/Noun">
|
|
|
|
|
Name
|
|
|
|
|
</translate>
|
|
|
|
|
</th>
|
|
|
|
|
<th class="sorted descending">
|
|
|
|
|
<translate translate-context="Popup/Playlist/Table.Label/Short">
|
|
|
|
|
Last modification
|
|
|
|
|
</translate>
|
|
|
|
|
</th>
|
|
|
|
|
<th>
|
|
|
|
|
<translate translate-context="*/*/*">
|
|
|
|
|
Tracks
|
|
|
|
|
</translate>
|
|
|
|
|
</th>
|
2018-03-20 22:41:15 +00:00
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody>
|
2021-12-06 10:35:20 +00:00
|
|
|
<tr
|
|
|
|
|
v-for="(playlist, key) in sortedPlaylists"
|
|
|
|
|
:key="key"
|
|
|
|
|
>
|
2018-03-20 22:41:15 +00:00
|
|
|
<td>
|
|
|
|
|
<router-link
|
|
|
|
|
class="ui icon basic small button"
|
2021-12-06 10:35:20 +00:00
|
|
|
:to="{name: 'library.playlists.detail', params: {id: playlist.id }, query: {mode: 'edit'}}"
|
|
|
|
|
>
|
|
|
|
|
<i class="ui pencil icon" />
|
|
|
|
|
<span class="visually-hidden"><translate translate-context="*/*/*/Verb">Edit</translate></span>
|
|
|
|
|
</router-link>
|
2018-03-20 22:41:15 +00:00
|
|
|
</td>
|
2020-08-01 09:11:51 +00:00
|
|
|
<td>
|
2021-12-06 10:35:20 +00:00
|
|
|
<router-link
|
|
|
|
|
:to="{name: 'library.playlists.detail', params: {id: playlist.id }}"
|
2022-05-01 13:30:42 +00:00
|
|
|
@click="$store.state.playlists.showModal = false"
|
2021-12-06 10:35:20 +00:00
|
|
|
>
|
|
|
|
|
{{ playlist.name }}
|
|
|
|
|
</router-link>
|
|
|
|
|
</td>
|
|
|
|
|
<td><human-date :date="playlist.modification_date" /></td>
|
2018-03-20 22:41:15 +00:00
|
|
|
<td>{{ playlist.tracks_count }}</td>
|
|
|
|
|
<td>
|
2020-08-11 12:07:06 +00:00
|
|
|
<button
|
2018-03-20 22:41:15 +00:00
|
|
|
v-if="track"
|
2020-05-15 12:12:36 +00:00
|
|
|
class="ui success icon basic small right floated button"
|
2018-07-01 19:50:50 +00:00
|
|
|
:title="labels.addToPlaylist"
|
2021-12-06 10:35:20 +00:00
|
|
|
@click.prevent="addToPlaylist(playlist.id, false)"
|
|
|
|
|
>
|
|
|
|
|
<i class="plus icon" /> <translate translate-context="Popup/Playlist/Table.Button.Label/Verb">
|
|
|
|
|
Add track
|
|
|
|
|
</translate>
|
2020-08-11 12:07:06 +00:00
|
|
|
</button>
|
2018-03-20 22:41:15 +00:00
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
2019-12-07 16:06:24 +00:00
|
|
|
<template v-else>
|
2020-05-15 12:12:36 +00:00
|
|
|
<div class="ui small placeholder segment component-placeholder">
|
2020-07-03 12:20:47 +00:00
|
|
|
<h4 class="ui header">
|
2021-12-06 10:35:20 +00:00
|
|
|
<translate translate-context="Popup/Playlist/EmptyState">
|
|
|
|
|
No results matching your filter
|
2019-10-17 12:15:33 +00:00
|
|
|
</translate>
|
2021-12-06 10:35:20 +00:00
|
|
|
</h4>
|
2019-10-17 12:15:33 +00:00
|
|
|
</div>
|
|
|
|
|
</template>
|
2018-03-19 16:39:03 +00:00
|
|
|
</div>
|
2021-12-06 10:35:20 +00:00
|
|
|
<template v-else>
|
|
|
|
|
<div class="ui placeholder segment">
|
|
|
|
|
<div class="ui icon header">
|
|
|
|
|
<i class="list icon" />
|
|
|
|
|
<translate translate-context="Content/Home/Placeholder">
|
|
|
|
|
No playlists have been created yet
|
|
|
|
|
</translate>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
2018-03-20 22:41:15 +00:00
|
|
|
</div>
|
|
|
|
|
<div class="actions">
|
2021-12-06 10:35:20 +00:00
|
|
|
<button class="ui basic cancel button">
|
|
|
|
|
<translate translate-context="*/*/Button.Label/Verb">
|
|
|
|
|
Cancel
|
|
|
|
|
</translate>
|
|
|
|
|
</button>
|
2018-03-20 22:41:15 +00:00
|
|
|
</div>
|
2018-03-19 16:39:03 +00:00
|
|
|
</modal>
|
|
|
|
|
</template>
|