MusicTopus/views/pages/mon-compte/ma-collection.ejs

191 lines
7 KiB
Text
Raw Normal View History

2022-02-18 16:53:54 +01:00
<section class="layout-maxed ma-collection" id="app">
<div class="filters">
<div class="field">
<label for="artist">Artiste</label>
<select id="artist" v-model="artist" @change="changeFilter">
<option value="">Tous</option>
2022-02-18 23:57:51 +01:00
<%
2022-02-18 16:53:54 +01:00
for (let i = 0; i < page.artists.length; i += 1 ) {
__append(`<option value="${page.artists[i]}">${page.artists[i]}</option>`);
}
%>
</select>
</div>
<div class="field">
<label for="format">Format</label>
<select id="format" v-model="format" @change="changeFilter">
<option value="">Tous</option>
2022-02-18 23:57:51 +01:00
<%
2022-02-18 16:53:54 +01:00
for (let i = 0; i < page.formats.length; i += 1 ) {
__append(`<option value="${page.formats[i]}">${page.formats[i]}</option>`);
}
%>
</select>
</div>
<div class="field">
<label for="sortOrder">Trier par</label>
<select id="sortOrder" v-model="sortOrder" @change="changeSort">
<option value="artists_sort-asc">Artiste (A-Z)</option>
<option value="artists_sort-desc">Artiste (Z-A)</option>
<option value="year-asc">Année (A-Z)</option>
<option value="year-desc">Année (Z-A)</option>
<option value="country-asc">Pays (A-Z)</option>
<option value="country-desc">Pays (Z-A)</option>
<option value="formats.name-asc">Format (A-Z)</option>
<option value="formats.name-desc">Format (Z-A)</option>
</select>
</div>
</div>
<div class="list" v-if="!loading" v-for="item in items">
<span class="title">
{{ item.artists_sort}} - {{ item.title }}
<span class="icon" @click="showConfirmDelete(item._id)">
<i class="fa-solid fa-trash"></i>
</span>
</span>
2022-02-18 16:53:54 +01:00
<div class="grid grid-cols-2 md:grid-cols-4">
<div>
<img :src="item.thumb" :alt="item.title" style="max-width: 120px;"/>
</div>
<div class="md:col-span-3">
<span><strong>Année :</strong> {{ item.year }}</span>
<br />
<span><strong>Pays :</strong> {{ item.country }}</span>
<br />
2022-02-18 23:57:51 +01:00
<span class="items"><strong>Format :</strong> <span v-for="format in item.formats">{{ format.name }}</span></span>
2022-02-18 16:53:54 +01:00
<br />
2022-02-18 23:57:51 +01:00
<span class="items"><strong>Genre :</strong> <span v-for="genre in item.genres">{{ genre }}</span></span>
2022-02-18 16:53:54 +01:00
<br />
2022-02-18 23:57:51 +01:00
<span class="items"><strong>Style :</strong> <span v-for="style in item.styles">{{ style }}</span></span>
2022-02-18 16:53:54 +01:00
</div>
</div>
</div>
<nav class="pagination" role="navigation" aria-label="pagination">
<a class="pagination-previous" :class="{'is-disabled': page === 1}" @click="previous">Précédent</a>
<a class="pagination-next" :class="{'is-disabled': !total || (page*limit) >= total}" @click="next">Suivant</a>
2022-02-18 16:53:54 +01:00
<ul class="pagination-list">
<li v-for="p in Array.from({length: totalPages}, (v, i) => (i+1))">
<a class="pagination-link" :class="{'is-current': p === page}" @click="goTo(p)" aria-label="Goto page 1">{{ p }}</a>
</li>
</ul>
</nav>
<div class="modal" :class="{'is-visible': showModalDelete}">
<div class="modal-background"></div>
<div class="modal-card">
<header></header>
<section>
Êtes-vous sûr de vouloir supprimer cet album ?
</section>
<footer>
<button class="button is-primary" @click="deleteItem">Supprimer</button>
<button class="button" @click="toggleModal">Annuler</button>
</footer>
</div>
</div>
2022-02-18 16:53:54 +01:00
</section>
2022-02-15 16:45:14 +01:00
<script>
Vue.createApp({
data() {
return {
loading: false,
items: [],
total: 0,
page: 1,
totalPages: 1,
2022-02-18 16:53:54 +01:00
limit: 10,
2022-02-16 09:55:12 +01:00
artist: '',
2022-02-16 15:42:02 +01:00
format: '',
2022-02-18 16:53:54 +01:00
sortOrder: 'artists_sort-asc',
2022-02-16 09:55:12 +01:00
sort: 'artists_sort',
order: 'asc',
itemId: null,
showModalDelete: false,
2022-02-15 16:45:14 +01:00
}
},
created() {
this.fetch();
},
methods: {
fetch() {
this.loading = true;
2022-02-16 09:55:12 +01:00
2022-02-16 15:42:02 +01:00
let url = `/api/v1/albums?page=${this.page}&limit=${this.limit}&sort=${this.sort}&order=${this.order}`;
if ( this.artist ) {
url += `&artists_sort=${this.artist}`;
}
if ( this.format ) {
url += `&format=${this.format}`;
}
axios.get(url)
2022-02-15 16:45:14 +01:00
.then( response => {
this.items = response.data.rows;
this.total = response.data.count;
this.totalPages = parseInt(response.data.count / this.limit) + (response.data.count % this.limit > 0 ? 1 : 0);
})
.catch((err) => {
showToastr(err.response?.data?.message || "Impossible de charger votre collection");
})
.finally(() => {
this.loading = false;
});
},
next(event) {
event.preventDefault();
this.page += 1;
this.fetch();
},
previous(event) {
event.preventDefault();
this.page -= 1;
this.fetch();
},
goTo(page) {
this.page = page;
this.fetch();
2022-02-16 09:55:12 +01:00
},
2022-02-18 16:53:54 +01:00
changeSort() {
console.log('TEST:', this.sortOrder);
const [sort,order] = this.sortOrder.split('-');
2022-02-16 09:55:12 +01:00
this.sort = sort;
this.order = order;
this.page = 1;
this.fetch();
},
2022-02-16 15:42:02 +01:00
changeFilter() {
this.page = 1;
this.fetch();
},
toggleModal() {
this.showModalDelete = !this.showModalDelete;
},
showConfirmDelete(itemId) {
this.itemId = itemId;
this.toggleModal();
},
deleteItem() {
axios.delete(`/api/v1/albums/${this.itemId}`)
.then( () => {
this.fetch();
})
.catch((err) => {
showToastr(err.response?.data?.message || "Impossible de supprimer cet album");
})
.finally(() => {
this.toggleModal();
});
2022-02-16 15:42:02 +01:00
}
2022-02-15 16:45:14 +01:00
}
}).mount('#app')
</script>