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;
|
|
|
|
},
|
|
|
|
|
2015-12-30 04:14:47 +00:00
|
|
|
clear() {
|
|
|
|
this.state.songs = [];
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
2015-12-24 09:59:04 +00:00
|
|
|
if (song.liked) {
|
|
|
|
this.add(song);
|
|
|
|
} else {
|
|
|
|
this.remove(song);
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-01-26 06:25:31 +00:00
|
|
|
http.post('interaction/like', { song: song.id }, () => {
|
2015-12-13 04:42:28 +00:00
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a song into the store.
|
|
|
|
*
|
2016-01-07 09:03:38 +00:00
|
|
|
* @param {Object} song
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
add(song) {
|
|
|
|
this.state.songs.push(song);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a song from the store.
|
|
|
|
*
|
2016-01-07 09:03:38 +00:00
|
|
|
* @param {Object} song
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
remove(song) {
|
2016-03-31 07:44:36 +00:00
|
|
|
this.state.songs = difference(this.state.songs, [song]);
|
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);
|
|
|
|
this.state.songs = union(this.state.songs, songs);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-03-31 08:58:46 +00:00
|
|
|
http.post('interaction/batch/like', { songs: map(songs, 'id') }, () => {
|
2015-12-13 04:42:28 +00:00
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2015-12-24 09:59:04 +00:00
|
|
|
// Don't wait for the HTTP response to update the status, just set them to Unliked 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 = false);
|
|
|
|
this.state.songs = difference(this.state.songs, songs);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-03-31 08:58:46 +00:00
|
|
|
http.post('interaction/batch/unlike', { songs: map(songs, 'id') }, () => {
|
2015-12-13 04:42:28 +00:00
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|