List.vue 4.68 KB
Newer Older
1
<template>
Bat's avatar
Bat committed
2
  <div class="main pusher" v-title="'Your Favorites'">
3
4
5
6
7
8
    <div class="ui vertical center aligned stripe segment">
      <div :class="['ui', {'active': isLoading}, 'inverted', 'dimmer']">
        <div class="ui text loader">Loading your favorites...</div>
      </div>
      <h2 v-if="results" class="ui center aligned icon header">
        <i class="circular inverted heart pink icon"></i>
9
        {{ $store.state.favorites.count }} favorites
10
11
12
13
      </h2>
      <radio-button type="favorites"></radio-button>
    </div>
    <div class="ui vertical stripe segment">
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
      <div :class="['ui', {'loading': isLoading}, 'form']">
        <div class="fields">
          <div class="field">
            <label>Ordering</label>
            <select class="ui dropdown" v-model="ordering">
              <option v-for="option in orderingOptions" :value="option[0]">
                {{ option[1] }}
              </option>
            </select>
          </div>
          <div class="field">
            <label>Ordering direction</label>
            <select class="ui dropdown" v-model="orderingDirection">
              <option value="">Ascending</option>
              <option value="-">Descending</option>
            </select>
          </div>
          <div class="field">
            <label>Results per page</label>
            <select class="ui dropdown" v-model="paginateBy">
              <option :value="parseInt(12)">12</option>
              <option :value="parseInt(25)">25</option>
              <option :value="parseInt(50)">50</option>
            </select>
          </div>
        </div>
      </div>

42
      <track-table v-if="results" :tracks="results.results"></track-table>
Eliot Berriot's avatar
Eliot Berriot committed
43
44
      <div class="ui center aligned basic segment">
        <pagination
Eliot Berriot's avatar
Eliot Berriot committed
45
          v-if="results && results.count > 0"
Eliot Berriot's avatar
Eliot Berriot committed
46
47
          @page-changed="selectPage"
          :current="page"
48
          :paginate-by="paginateBy"
Eliot Berriot's avatar
Eliot Berriot committed
49
50
51
          :total="results.count"
          ></pagination>
      </div>
52
53
54
55
56
    </div>
  </div>
</template>

<script>
Eliot Berriot's avatar
Eliot Berriot committed
57
import axios from 'axios'
58
import $ from 'jquery'
59
60
61
import logger from '@/logging'
import TrackTable from '@/components/audio/track/Table'
import RadioButton from '@/components/radios/Button'
Eliot Berriot's avatar
Eliot Berriot committed
62
import Pagination from '@/components/Pagination'
63
64
import OrderingMixin from '@/components/mixins/Ordering'
import PaginationMixin from '@/components/mixins/Pagination'
Eliot Berriot's avatar
Eliot Berriot committed
65
const FAVORITES_URL = 'tracks/'
66
67

export default {
68
  mixins: [OrderingMixin, PaginationMixin],
69
70
  components: {
    TrackTable,
Eliot Berriot's avatar
Eliot Berriot committed
71
72
    RadioButton,
    Pagination
73
74
  },
  data () {
75
    let defaultOrdering = this.getOrderingFromString(this.defaultOrdering || 'artist__name')
76
77
78
79
80
    return {
      results: null,
      isLoading: false,
      nextLink: null,
      previousLink: null,
81
82
83
84
85
86
87
88
89
      page: parseInt(this.defaultPage),
      paginateBy: parseInt(this.defaultPaginateBy || 25),
      orderingDirection: defaultOrdering.direction,
      ordering: defaultOrdering.field,
      orderingOptions: [
        ['title', 'Track name'],
        ['album__title', 'Album name'],
        ['artist__name', 'Artist name']
      ]
90
91
92
93
94
    }
  },
  created () {
    this.fetchFavorites(FAVORITES_URL)
  },
95
96
97
  mounted () {
    $('.ui.dropdown').dropdown()
  },
98
  methods: {
99
100
101
102
103
104
105
106
107
    updateQueryString: function () {
      this.$router.replace({
        query: {
          page: this.page,
          paginateBy: this.paginateBy,
          ordering: this.getOrderingAsString()
        }
      })
    },
108
109
110
111
    fetchFavorites (url) {
      var self = this
      this.isLoading = true
      let params = {
Eliot Berriot's avatar
Eliot Berriot committed
112
        favorites: 'true',
113
        page: this.page,
114
115
        page_size: this.paginateBy,
        ordering: this.getOrderingAsString()
116
117
      }
      logger.default.time('Loading user favorites')
Eliot Berriot's avatar
Eliot Berriot committed
118
      axios.get(url, {params: params}).then((response) => {
119
120
121
122
        self.results = response.data
        self.nextLink = response.data.next
        self.previousLink = response.data.previous
        self.results.results.forEach((track) => {
123
          self.$store.commit('favorites/track', {id: track.id, value: true})
124
125
126
127
        })
        logger.default.timeEnd('Loading user favorites')
        self.isLoading = false
      })
Eliot Berriot's avatar
Eliot Berriot committed
128
129
130
131
132
133
134
    },
    selectPage: function (page) {
      this.page = page
    }
  },
  watch: {
    page: function () {
135
136
137
138
139
140
141
142
143
144
145
146
147
      this.updateQueryString()
      this.fetchFavorites(FAVORITES_URL)
    },
    paginateBy: function () {
      this.updateQueryString()
      this.fetchFavorites(FAVORITES_URL)
    },
    orderingDirection: function () {
      this.updateQueryString()
      this.fetchFavorites(FAVORITES_URL)
    },
    ordering: function () {
      this.updateQueryString()
Eliot Berriot's avatar
Eliot Berriot committed
148
      this.fetchFavorites(FAVORITES_URL)
149
150
151
152
153
154
155
156
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>