2016-06-25 16:05:24 +00:00
|
|
|
import { each, map, difference, union } from 'lodash';
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-06-25 10:15:57 +00:00
|
|
|
import { http } from '../services';
|
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,
|
|
|
|
fmtLength: '',
|
|
|
|
},
|
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>}
|
|
|
|
*/
|
|
|
|
get all() {
|
|
|
|
return this.state.songs;
|
|
|
|
},
|
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
|
|
|
|
*/
|
|
|
|
set all(value) {
|
|
|
|
this.state.songs = value;
|
|
|
|
},
|
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-06-27 06:11: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?
|
|
|
|
song.liked = !song.liked;
|
|
|
|
song.liked ? this.add(song) : this.remove(song);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-06-27 06:11:35 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
http.post('interaction/like', { song: song.id }, r => resolve(r), r => reject(r));
|
|
|
|
});
|
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
|
|
|
|
*/
|
|
|
|
add(songs) {
|
|
|
|
this.all = union(this.all, [].concat(songs));
|
|
|
|
},
|
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
|
|
|
|
*/
|
|
|
|
remove(songs) {
|
|
|
|
this.all = difference(this.all, [].concat(songs));
|
|
|
|
},
|
2016-04-05 07:38:10 +00:00
|
|
|
|
2016-06-25 16:05:24 +00:00
|
|
|
/**
|
|
|
|
* Remove all favorites.
|
|
|
|
*/
|
|
|
|
clear() {
|
|
|
|
this.all = [];
|
|
|
|
},
|
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-06-27 06:11: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?
|
|
|
|
each(songs, song => song.liked = true);
|
|
|
|
this.add(songs);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-06-27 06:11:35 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
http.post('interaction/batch/like', { songs: map(songs, 'id') }, r => resolve(r), r => reject(r));
|
|
|
|
});
|
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-06-27 06:11:35 +00:00
|
|
|
unlike(songs) {
|
2016-06-25 16:05:24 +00:00
|
|
|
each(songs, song => song.liked = false);
|
|
|
|
this.remove(songs);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-06-27 06:11:35 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
http.post('interaction/batch/unlike', { songs: map(songs, 'id') }, r => resolve(r), r => reject(r));
|
|
|
|
});
|
2016-06-25 16:05:24 +00:00
|
|
|
},
|
2015-12-13 04:42:28 +00:00
|
|
|
};
|