From 1a3e1e7f1cd40b1417e234647899beb77bc8a984 Mon Sep 17 00:00:00 2001 From: An Phan Date: Fri, 13 Jan 2017 00:50:00 +0800 Subject: [PATCH] Refactor, mostly using destructuring --- resources/assets/js/stores/album.js | 4 +- resources/assets/js/stores/artist.js | 10 ++-- resources/assets/js/stores/favorite.js | 12 ++--- resources/assets/js/stores/playlist.js | 14 +++-- resources/assets/js/stores/preference.js | 2 +- resources/assets/js/stores/queue.js | 8 +-- resources/assets/js/stores/setting.js | 4 +- resources/assets/js/stores/shared.js | 66 ++++++++++-------------- resources/assets/js/stores/song.js | 21 +++----- resources/assets/js/stores/user.js | 21 ++++---- 10 files changed, 67 insertions(+), 95 deletions(-) diff --git a/resources/assets/js/stores/album.js b/resources/assets/js/stores/album.js index 99006ec3..5523103e 100644 --- a/resources/assets/js/stores/album.js +++ b/resources/assets/js/stores/album.js @@ -23,9 +23,7 @@ export const albumStore = { this.all = reduce(artists, (albums, artist) => { // While we're doing so, for each album, we get its length // and keep a back reference to the artist too. - each(artist.albums, album => { - this.setupAlbum(album, artist) - }) + each(artist.albums, album => this.setupAlbum(album, artist)) return albums.concat(artist.albums) }, []) diff --git a/resources/assets/js/stores/artist.js b/resources/assets/js/stores/artist.js index b7602b35..583ccaea 100644 --- a/resources/assets/js/stores/artist.js +++ b/resources/assets/js/stores/artist.js @@ -25,9 +25,7 @@ export const artistStore = { this.all = artists // Traverse through artists array to get the cover and number of songs for each. - each(this.all, artist => { - this.setupArtist(artist) - }) + each(this.all, artist => this.setupArtist(artist)) albumStore.init(this.all) }, @@ -95,7 +93,7 @@ export const artistStore = { */ add (artists) { artists = [].concat(artists) - each(artists, a => this.setupArtist(a)) + each(artists, artist => this.setupArtist(artist)) this.all = union(this.all, artists) }, @@ -110,9 +108,7 @@ export const artistStore = { this.all = difference(this.all, artists) // Remember to clear the cache - each(artists, artist => { - delete this.cache[artist.id] - }) + each(artists, artist => delete this.cache[artist.id]) }, /** diff --git a/resources/assets/js/stores/favorite.js b/resources/assets/js/stores/favorite.js index 862581e0..c59818a7 100644 --- a/resources/assets/js/stores/favorite.js +++ b/resources/assets/js/stores/favorite.js @@ -44,9 +44,9 @@ export const favoriteStore = { NProgress.start() return new Promise((resolve, reject) => { - http.post('interaction/like', { song: song.id }, response => { + http.post('interaction/like', { song: song.id }, ({ data }) => { // We don't really need to notify just for one song. - resolve(response.data) + resolve(data) }, error => reject(error)) }) }, @@ -92,9 +92,9 @@ export const favoriteStore = { NProgress.start() return new Promise((resolve, reject) => { - http.post('interaction/batch/like', { songs: map(songs, 'id') }, response => { + http.post('interaction/batch/like', { songs: map(songs, 'id') }, ({ data }) => { alerts.success(`Added ${pluralize(songs.length, 'song')} into Favorites.`) - resolve(response.data) + resolve(data) }, error => reject(error)) }) }, @@ -113,9 +113,9 @@ export const favoriteStore = { NProgress.start() return new Promise((resolve, reject) => { - http.post('interaction/batch/unlike', { songs: map(songs, 'id') }, response => { + http.post('interaction/batch/unlike', { songs: map(songs, 'id') }, ({ data }) => { alerts.success(`Removed ${pluralize(songs.length, 'song')} from Favorites.`) - resolve(response.data) + resolve(data) }, error => reject(error)) }) } diff --git a/resources/assets/js/stores/playlist.js b/resources/assets/js/stores/playlist.js index c0a23d97..da8eacaa 100644 --- a/resources/assets/js/stores/playlist.js +++ b/resources/assets/js/stores/playlist.js @@ -101,8 +101,7 @@ export const playlistStore = { NProgress.start() return new Promise((resolve, reject) => { - http.post('playlist', { name, songs }, response => { - const playlist = response.data + http.post('playlist', { name, songs }, ({ data: playlist }) => { playlist.songs = songs this.objectifySongs(playlist) this.add(playlist) @@ -121,10 +120,10 @@ export const playlistStore = { NProgress.start() return new Promise((resolve, reject) => { - http.delete(`playlist/${playlist.id}`, {}, response => { + http.delete(`playlist/${playlist.id}`, {}, ({ data }) => { this.remove(playlist) alerts.success(`Deleted playlist "${playlist.name}".`) - resolve(response.data) + resolve(data) }, error => reject(error)) }) }, @@ -182,7 +181,12 @@ export const playlistStore = { NProgress.start() return new Promise((resolve, reject) => { - http.put(`playlist/${playlist.id}`, { name: playlist.name }, () => resolve(playlist), error => reject(error)) + http.put( + `playlist/${playlist.id}`, + { name: playlist.name }, + () => resolve(playlist), + error => reject(error) + ) }) } } diff --git a/resources/assets/js/stores/preference.js b/resources/assets/js/stores/preference.js index 0df8d6e6..273c5e2a 100644 --- a/resources/assets/js/stores/preference.js +++ b/resources/assets/js/stores/preference.js @@ -43,7 +43,7 @@ export const preferenceStore = { each(Object.keys(this.state), key => { Object.defineProperty(this, key, { get: () => this.state[key], - set: (value) => { + set: value => { this.state[key] = value this.save() }, diff --git a/resources/assets/js/stores/queue.js b/resources/assets/js/stores/queue.js index 81e725d9..acc0bf2d 100644 --- a/resources/assets/js/stores/queue.js +++ b/resources/assets/js/stores/queue.js @@ -168,9 +168,9 @@ export const queueStore = { return first(this.all) } - const idx = map(this.all, 'id').indexOf(this.current.id) + 1 + const index = map(this.all, 'id').indexOf(this.current.id) + 1 - return idx >= this.all.length ? null : this.all[idx] + return index >= this.all.length ? null : this.all[index] }, /** @@ -183,9 +183,9 @@ export const queueStore = { return last(this.all) } - const idx = map(this.all, 'id').indexOf(this.current.id) - 1 + const index = map(this.all, 'id').indexOf(this.current.id) - 1 - return idx < 0 ? null : this.all[idx] + return index < 0 ? null : this.all[index] }, /** diff --git a/resources/assets/js/stores/setting.js b/resources/assets/js/stores/setting.js index a7462191..224a0a63 100644 --- a/resources/assets/js/stores/setting.js +++ b/resources/assets/js/stores/setting.js @@ -19,9 +19,9 @@ export const settingStore = { update () { return new Promise((resolve, reject) => { - http.post('settings', this.all, response => { + http.post('settings', this.all, ({ data }) => { alerts.success('Settings saved.') - resolve(response.data) + resolve(data) }, error => reject(error)) }) } diff --git a/resources/assets/js/stores/shared.js b/resources/assets/js/stores/shared.js index 030e1578..680cf976 100644 --- a/resources/assets/js/stores/shared.js +++ b/resources/assets/js/stores/shared.js @@ -1,37 +1,39 @@ -import { assign } from 'lodash' +import { assign, clone } from 'lodash' import isMobile from 'ismobilejs' import { http } from '../services' import { userStore, preferenceStore, artistStore, songStore, playlistStore, queueStore, settingStore } from '.' +const emptyState = { + songs: [], + albums: [], + artists: [], + favorites: [], + queued: [], + interactions: [], + users: [], + settings: [], + currentUser: null, + playlists: [], + useLastfm: false, + useYouTube: false, + useiTunes: true, + allowDownload: false, + currentVersion: '', + latestVersion: '', + cdnUrl: '', + originalMediaPath: '' +} + export const sharedStore = { - state: { - songs: [], - albums: [], - artists: [], - favorites: [], - queued: [], - interactions: [], - users: [], - settings: [], - currentUser: null, - playlists: [], - useLastfm: false, - useYouTube: false, - useiTunes: true, - allowDownload: false, - currentVersion: '', - latestVersion: '', - cdnUrl: '', - originalMediaPath: '' - }, + state: clone(emptyState), init () { this.reset() return new Promise((resolve, reject) => { - http.get('data', response => { - assign(this.state, response.data) + http.get('data', ({ data }) => { + assign(this.state, data) // Don't allow downloading on mobile devices this.state.allowDownload = this.state.allowDownload && !isMobile.any @@ -60,22 +62,6 @@ export const sharedStore = { }, reset () { - this.state.songs = [] - this.state.albums = [] - this.state.artists = [] - this.state.favorites = [] - this.state.queued = [] - this.state.interactions = [] - this.state.users = [] - this.state.settings = [] - this.state.currentUser = null - this.state.playlists = [] - this.state.useLastfm = false - this.state.useYouTube = false - this.state.useiTunes = true - this.state.allowDownload = false - this.state.currentVersion = '' - this.state.latestVersion = '' - this.state.cdnUrl = '' + this.state = clone(emptyState) } } diff --git a/resources/assets/js/stores/song.js b/resources/assets/js/stores/song.js index 44bb1ea3..b6f76dd1 100644 --- a/resources/assets/js/stores/song.js +++ b/resources/assets/js/stores/song.js @@ -180,13 +180,13 @@ export const songStore = { return new Promise((resolve, reject) => { const oldCount = song.playCount - http.post('interaction/play', { song: song.id }, response => { + http.post('interaction/play', { song: song.id }, ({ data }) => { // Use the data from the server to make sure we don't miss a play from another device. - song.playCount = response.data.play_count + song.playCount = data.play_count song.album.playCount += song.playCount - oldCount song.artist.playCount += song.playCount - oldCount - resolve(response.data) + resolve(data) }, error => reject(error)) }) }, @@ -215,8 +215,8 @@ export const songStore = { */ scrobble (song) { return new Promise((resolve, reject) => { - http.post(`${song.id}/scrobble/${song.playStartTime}`, {}, response => { - resolve(response.data) + http.post(`${song.id}/scrobble/${song.playStartTime}`, {}, ({ data }) => { + resolve(data) }, error => reject(error)) }) }, @@ -232,8 +232,7 @@ export const songStore = { http.put('songs', { data, songs: map(songs, 'id') - }, response => { - const songs = response.data + }, ({ songs }) => { each(songs, song => this.syncUpdatedSong(song)) alerts.success(`Updated ${pluralize(songs.length, 'song')}.`) resolve(songs) @@ -396,13 +395,5 @@ export const songStore = { */ getRecentlyAdded (n = 10) { return take(orderBy(this.all, 'created_at', 'desc'), n) - }, - - /** - * Called when the application is torn down. - * Reset stuff. - */ - teardown () { - this.state.recentlyPlayed = [] } } diff --git a/resources/assets/js/stores/user.js b/resources/assets/js/stores/user.js index a9f690cc..4ddfe37a 100644 --- a/resources/assets/js/stores/user.js +++ b/resources/assets/js/stores/user.js @@ -105,8 +105,8 @@ export const userStore = { NProgress.start() return new Promise((resolve, reject) => { - http.post('me', { email, password }, response => { - resolve(response.data) + http.post('me', { email, password }, ({ data }) => { + resolve(data) }, error => reject(error)) }) }, @@ -116,8 +116,8 @@ export const userStore = { */ logout () { return new Promise((resolve, reject) => { - http.delete('me', {}, response => { - resolve(response.data) + http.delete('me', {}, ({ data }) => { + resolve(data) }, error => reject(error)) }) }, @@ -155,8 +155,7 @@ export const userStore = { NProgress.start() return new Promise((resolve, reject) => { - http.post('user', { name, email, password }, response => { - const user = response.data + http.post('user', { name, email, password }, ({ data: user }) => { this.setAvatar(user) this.all.unshift(user) alerts.success(`New user "${name}" created.`) @@ -178,10 +177,8 @@ export const userStore = { return new Promise((resolve, reject) => { http.put(`user/${user.id}`, { name, email, password }, () => { - this.setAvatar(user) - user.name = name - user.email = email - user.password = '' + this.setAvatar(user); + [user.name, user.email, user.password] = [name, email, ''] alerts.success('User profile updated.') resolve(user) }, error => reject(error)) @@ -197,7 +194,7 @@ export const userStore = { NProgress.start() return new Promise((resolve, reject) => { - http.delete(`user/${user.id}`, {}, response => { + http.delete(`user/${user.id}`, {}, ({ data }) => { this.all = without(this.all, user) alerts.success(`User "${user.name}" deleted.`) @@ -223,7 +220,7 @@ export const userStore = { /** * Brian May enters the stage. */ - resolve(response.data) + resolve(data) }, error => reject(error)) }) }