koel/resources/assets/js/stores/favorite.js

123 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-11-26 03:25:35 +00:00
import { each, map, difference, union } from 'lodash'
import NProgress from 'nprogress'
2015-12-13 04:42:28 +00:00
2016-11-26 03:25:35 +00:00
import { http } from '../services'
2016-12-19 05:37:30 +00:00
import { alerts, pluralize } from '../utils'
2015-12-13 04:42:28 +00:00
2016-06-25 10:15:57 +00:00
export const favoriteStore = {
2016-06-25 16:05:24 +00:00
state: {
songs: [],
length: 0,
2016-11-26 03:25:35 +00:00
fmtLength: ''
2016-06-25 16:05:24 +00:00
},
2016-03-11 09:55:04 +00:00
2016-06-25 16:05:24 +00:00
/**
* All songs favorite'd by the current user.
*
* @return {Array.<Object>}
*/
2016-11-26 03:25:35 +00:00
get all () {
return this.state.songs
2016-06-25 16:05:24 +00:00
},
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
/**
* Set all favorite'd songs.
*
* @param {Array.<Object>} value
*/
2016-11-26 03:25:35 +00:00
set all (value) {
this.state.songs = value
2016-06-25 16:05:24 +00:00
},
2015-12-30 04:14:47 +00:00
2016-06-25 16:05:24 +00:00
/**
* Toggle like/unlike a song.
* A request to the server will be made.
*
* @param {Object} song
*/
2016-11-26 03:25:35 +00:00
toggleOne (song) {
2016-06-25 16:05:24 +00:00
// Don't wait for the HTTP response to update the status, just toggle right away.
// This may cause a minor problem if the request fails somehow, but do we care?
2016-11-26 03:25:35 +00:00
song.liked = !song.liked
song.liked ? this.add(song) : this.remove(song)
2015-12-13 04:42:28 +00:00
NProgress.start()
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
2017-01-12 16:50:00 +00:00
http.post('interaction/like', { song: song.id }, ({ data }) => {
2016-12-01 10:54:28 +00:00
// We don't really need to notify just for one song.
2017-01-12 16:50:00 +00:00
resolve(data)
2016-12-20 15:44:47 +00:00
}, error => reject(error))
2016-11-26 03:25:35 +00:00
})
2016-06-25 16:05:24 +00:00
},
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
/**
* Add a song/songs into the store.
*
* @param {Array.<Object>|Object} songs
*/
2016-11-26 03:25:35 +00:00
add (songs) {
this.all = union(this.all, [].concat(songs))
2016-06-25 16:05:24 +00:00
},
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
/**
* Remove a song/songs from the store.
*
* @param {Array.<Object>|Object} songs
*/
2016-11-26 03:25:35 +00:00
remove (songs) {
this.all = difference(this.all, [].concat(songs))
2016-06-25 16:05:24 +00:00
},
2016-04-05 07:38:10 +00:00
2016-06-25 16:05:24 +00:00
/**
* Remove all favorites.
*/
2016-11-26 03:25:35 +00:00
clear () {
this.all = []
2016-06-25 16:05:24 +00:00
},
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
/**
* Like a bunch of songs.
*
* @param {Array.<Object>} songs
*/
2016-11-26 03:25:35 +00:00
like (songs) {
2016-06-25 16:05:24 +00:00
// Don't wait for the HTTP response to update the status, just set them to Liked right away.
// This may cause a minor problem if the request fails somehow, but do we care?
2016-11-26 03:25:35 +00:00
each(songs, song => {
song.liked = true
})
this.add(songs)
2015-12-13 04:42:28 +00:00
NProgress.start()
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
2017-01-12 16:50:00 +00:00
http.post('interaction/batch/like', { songs: map(songs, 'id') }, ({ data }) => {
2016-12-19 05:37:30 +00:00
alerts.success(`Added ${pluralize(songs.length, 'song')} into Favorites.`)
2017-01-12 16:50:00 +00:00
resolve(data)
2016-12-20 15:44:47 +00:00
}, error => reject(error))
2016-11-26 03:25:35 +00:00
})
2016-06-25 16:05:24 +00:00
},
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
/**
* Unlike a bunch of songs.
*
* @param {Array.<Object>} songs
*/
2016-11-26 03:25:35 +00:00
unlike (songs) {
each(songs, song => {
song.liked = false
})
this.remove(songs)
2015-12-13 04:42:28 +00:00
NProgress.start()
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
2017-01-12 16:50:00 +00:00
http.post('interaction/batch/unlike', { songs: map(songs, 'id') }, ({ data }) => {
2016-12-19 05:37:30 +00:00
alerts.success(`Removed ${pluralize(songs.length, 'song')} from Favorites.`)
2017-01-12 16:50:00 +00:00
resolve(data)
2016-12-20 15:44:47 +00:00
}, error => reject(error))
2016-11-26 03:25:35 +00:00
})
}
}