diff --git a/client/pages/library/_library/narrators.vue b/client/pages/library/_library/narrators.vue index 9e58bf14..80dafddb 100644 --- a/client/pages/library/_library/narrators.vue +++ b/client/pages/library/_library/narrators.vue @@ -4,8 +4,8 @@
- - + + @@ -63,7 +63,9 @@ export default { loading: true, narrators: [], selectedNarrator: null, - newNarratorName: null + newNarratorName: null, + sortBy: 'name', + sortOrder: 'asc' } }, computed: { @@ -143,14 +145,38 @@ export default { }) }, async init() { + await this.fetchNarrators() + }, + async fetchNarrators() { + this.loading = true this.narrators = await this.$axios - .$get(`/api/libraries/${this.currentLibraryId}/narrators`) + .$get(`/api/libraries/${this.currentLibraryId}/narrators`, {params: {sortOrder: this.sortOrder, sortBy: this.sortBy}}) .then((response) => response.narrators) .catch((error) => { console.error('Failed to load narrators', error) return [] }) this.loading = false + }, + async sortNarratorsBy(attribute) { + if (this.sortBy == attribute) { + if (this.sortOrder == 'asc') { + this.sortOrder = 'desc'; + } else { + this.sortOrder = 'asc'; + } + } else { + this.sortBy = attribute; + this.sortOrder = 'asc'; + } + await this.fetchNarrators(); + }, + sortingArrow(attribute) { + if (this.sortBy == attribute) { + return this.sortOrder == 'asc'? '▲' : '▼'; + } else { + return ''; + } } }, mounted() { diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index 73b3d5c6..392d04b4 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -1140,8 +1140,14 @@ class LibraryController { }) } + // sorting + const key = req.query.sortBy === 'numBooks' ? 'numBooks' : 'name'; + const order = req.query.sortOrder === 'desc' ? 'desc' : 'asc'; + const narratorArr = Object.values(narrators); + const sorted = naturalSort(narratorArr)[order]((n) => n[key]) + res.json({ - narrators: naturalSort(Object.values(narrators)).asc((n) => n.name) + narrators: sorted }) }
{{ $strings.LabelName }}{{ $strings.LabelBooks }}{{ $strings.LabelName }} {{ sortingArrow('name') }}{{ $strings.LabelBooks }} {{ sortingArrow('numBooks') }}