2016-03-31 07:44:36 +00:00
|
|
|
import {
|
|
|
|
each,
|
2016-03-31 08:58:46 +00:00
|
|
|
map,
|
2016-03-31 07:44:36 +00:00
|
|
|
difference,
|
|
|
|
union
|
|
|
|
} from 'lodash';
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
import http from '../services/http';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
state: {
|
|
|
|
songs: [],
|
2016-01-14 08:02:59 +00:00
|
|
|
length: 0,
|
|
|
|
fmtLength: '',
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
2016-03-11 09:55:04 +00:00
|
|
|
|
2016-01-17 14:26:24 +00:00
|
|
|
/**
|
2016-03-20 13:42:33 +00:00
|
|
|
* All songs favorite'd by the current user.
|
2016-03-11 09:55:04 +00:00
|
|
|
*
|
2016-01-17 14:26:24 +00:00
|
|
|
* @return {Array.<Object>}
|
|
|
|
*/
|
2016-03-20 13:42:33 +00:00
|
|
|
get all() {
|
2015-12-13 04:42:28 +00:00
|
|
|
return this.state.songs;
|
|
|
|
},
|
|
|
|
|
2016-04-05 07:38:10 +00:00
|
|
|
/**
|
|
|
|
* Set all favorite'd songs.
|
|
|
|
*
|
|
|
|
* @param {Array.<Object>} value
|
|
|
|
*/
|
|
|
|
set all(value) {
|
|
|
|
this.state.songs = value;
|
2015-12-30 04:14:47 +00:00
|
|
|
},
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
2016-03-11 09:55:04 +00:00
|
|
|
* Toggle like/unlike a song.
|
2015-12-13 04:42:28 +00:00
|
|
|
* A request to the server will be made.
|
|
|
|
*
|
2016-01-07 09:03:38 +00:00
|
|
|
* @param {Object} song
|
|
|
|
* @param {?Function} cb
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
toggleOne(song, cb = null) {
|
2015-12-24 09:59:04 +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?
|
|
|
|
song.liked = !song.liked;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-05-05 10:38:54 +00:00
|
|
|
song.liked ? this.add(song) : this.remove(song);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-05-03 04:08:24 +00:00
|
|
|
http.post('interaction/like', { song: song.id }, () => cb && cb());
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-04-05 07:38:10 +00:00
|
|
|
* Add a song/songs into the store.
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2016-04-05 07:38:10 +00:00
|
|
|
* @param {Array.<Object>|Object} songs
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2016-04-05 07:38:10 +00:00
|
|
|
add(songs) {
|
|
|
|
this.all = union(this.all, [].concat(songs));
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-04-05 07:38:10 +00:00
|
|
|
* Remove a song/songs from the store.
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2016-04-05 07:38:10 +00:00
|
|
|
* @param {Array.<Object>|Object} songs
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2016-04-05 07:38:10 +00:00
|
|
|
remove(songs) {
|
|
|
|
this.all = difference(this.all, [].concat(songs));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all favorites.
|
|
|
|
*/
|
|
|
|
clear() {
|
|
|
|
this.all = [];
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like a bunch of songs.
|
2016-03-11 09:55:04 +00:00
|
|
|
*
|
2016-01-17 14:26:24 +00:00
|
|
|
* @param {Array.<Object>} songs
|
|
|
|
* @param {?Function} cb
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
like(songs, cb = null) {
|
2015-12-24 09:59:04 +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-03-31 07:44:36 +00:00
|
|
|
each(songs, song => song.liked = true);
|
2016-04-05 07:38:10 +00:00
|
|
|
this.add(songs);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-05-03 04:08:24 +00:00
|
|
|
http.post('interaction/batch/like', { songs: map(songs, 'id') }, () => cb && cb());
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlike a bunch of songs.
|
2016-03-11 09:55:04 +00:00
|
|
|
*
|
2016-01-17 14:26:24 +00:00
|
|
|
* @param {Array.<Object>} songs
|
|
|
|
* @param {?Function} cb
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
unlike(songs, cb = null) {
|
2016-03-31 07:44:36 +00:00
|
|
|
each(songs, song => song.liked = false);
|
2016-04-05 07:38:10 +00:00
|
|
|
this.remove(songs);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-05-03 04:08:24 +00:00
|
|
|
http.post('interaction/batch/unlike', { songs: map(songs, 'id') }, () => cb && cb());
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
};
|