-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathPlaylists.svelte
161 lines (151 loc) · 5.77 KB
/
Playlists.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<script>
import { onDestroy, onMount, getContext } from 'svelte';
import { _ } from 'svelte-i18n';
import { PageHeader } from '@keenmate/svelte-adminlte';
import { dayjs } from 'svelte-time';
import duration from 'dayjs/esm/plugin/duration';
dayjs.extend(duration);
import { locale } from '../locale/i18n';
import { setCustomPageTitle, customPageTitleUsed } from '../stores/page-title';
import { isAuthenticated } from '../stores/authentication';
import { fetchPlaylists, deletePlaylist } from '../providers/api';
import { successNotification, errorNotification } from '../providers/notification-provider';
import { isDay } from '../stores/terrariumpi';
import { DataTablesLanguageUrl } from '../constants/urls';
import Card from '../user-controls/Card.svelte';
const { editPlaylist } = getContext('modals');
const loadData = async () => {
loading = true;
if (dataTable) {
dataTable.destroy();
}
await fetchPlaylists(false, (data) => (playlists = data));
dataTable = jQuery('#playlists').DataTable({
language: {
url: `${DataTablesLanguageUrl}/${$locale}.json`,
},
columnDefs: [
{
targets: 'no-sort',
orderable: false,
},
],
});
loading = false;
};
const { confirmModal } = getContext('confirm');
const deletePlaylistAction = (playlist) => {
confirmModal(
$_('audio.playlists.delete.confirm.message', {
default: "Are you sure to delete the playlist ''{name}'' with {length} numbers?",
values: { name: playlist.name, length: playlist.length },
}),
async () => {
try {
await deletePlaylist(playlist.id);
successNotification(
$_('audio.playlists.delete.ok.message', {
default: "The playlist ''{name}'' is deleted.",
values: { name: playlist.name },
}),
$_('notification.delete.ok.title'),
);
loadData();
} catch (e) {
errorNotification(
$_('audio.playlists.delete.error.message', {
default: "The playlist ''{name}'' could not be deleted!\nError: {error}",
values: { name: playlist.name, error: e.message },
}),
$_('notification.delete.error.title'),
);
}
},
);
};
let playlists = [];
let loading = true;
let dataTable;
onMount(() => {
setCustomPageTitle($_('audio.playlists.title', { default: 'Playlists' }));
loadData();
});
onDestroy(() => {
customPageTitleUsed.set(false);
});
</script>
<PageHeader>
{$_('audio.playlists.title', { default: 'Playlists' })}
</PageHeader>
<div class="container-fluid">
<div class="row">
<div class="col-12">
<Card {loading} noTools={true}>
<table id="playlists" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>{$_('table.header.name', { default: 'Name' })}</th>
<th>{$_('table.header.duration', { default: 'Duration' })}</th>
<th>{$_('table.header.number_files', { default: '# Files' })}</th>
<th>{$_('table.header.volume', { default: 'Audio volume' })}</th>
<th>{$_('table.header.shuffle', { default: 'Shuffle' })}</th>
<th>{$_('table.header.repeat', { default: 'Repeat' })}</th>
{#if $isAuthenticated}
<th class="no-sort">{$_('table.header.actions', { default: 'Actions' })}</th>
{/if}
</tr>
</thead>
<tbody>
{#each playlists as playlist}
<tr>
<td>{playlist.name}</td>
<td data-order={playlist.duration}>{dayjs.duration(playlist.duration * 1000).humanize()}</td>
<td>{playlist.length}</td>
<td>{playlist.volume}</td>
<td data-order={playlist.shuffle ? 1 : 0}
><i class="fas" class:fa-check={playlist.shuffle} class:fa-times={!playlist.shuffle}> </i></td
>
<td data-order={playlist.repeat ? 1 : 0}
><i class="fas" class:fa-check={playlist.repeat} class:fa-times={!playlist.repeat}> </i></td
>
{#if $isAuthenticated}
<td class="d-flex justify-content-around">
<button
class="btn btn-sm"
class:btn-light={$isDay}
class:btn-dark={!$isDay}
on:click={() => editPlaylist(playlist)}
>
<i class="fas fa-wrench"></i>
</button>
<button
class="btn btn-sm"
class:btn-light={$isDay}
class:btn-dark={!$isDay}
on:click={() => deletePlaylistAction(playlist)}
>
<i class="fas fa-trash-alt text-danger"></i>
</button>
</td>
{/if}
</tr>
{/each}
</tbody>
<tfoot>
<tr>
<th>{$_('table.header.name', { default: 'Name' })}</th>
<th>{$_('table.header.duration', { default: 'Duration' })}</th>
<th>{$_('table.header.number_files', { default: '# Files' })}</th>
<th>{$_('table.header.volume', { default: 'Audio volume' })}</th>
<th>{$_('table.header.shuffle', { default: 'Shuffle' })}</th>
<th>{$_('table.header.repeat', { default: 'Repeat' })}</th>
{#if $isAuthenticated}
<th class="no-sort">{$_('table.header.actions', { default: 'Actions' })}</th>
{/if}
</tr>
</tfoot>
</table>
</Card>
</div>
</div>
</div>