fix: favorites behaviour when editing spotify credentials

This commit is contained in:
Roberto Tonino 2021-08-11 15:23:41 +02:00
parent 10d8d7cfa6
commit 58e7f80fb6
6 changed files with 70 additions and 23 deletions

View File

@ -25,6 +25,7 @@ import { fetchData, postToServer } from '@/utils/api'
import { toast } from '@/utils/toasts' import { toast } from '@/utils/toasts'
import { isValidURL } from '@/utils/utils' import { isValidURL } from '@/utils/utils'
import { sendAddToQueue } from '@/utils/downloads' import { sendAddToQueue } from '@/utils/downloads'
import { SPOTIFY_STATUS } from '@/constants'
/* ===== App initialization ===== */ /* ===== App initialization ===== */
async function startApp() { async function startApp() {
@ -36,9 +37,14 @@ async function startApp() {
}).$mount('#app') }).$mount('#app')
const connectResponse = await (await fetch('connect')).json() const connectResponse = await (await fetch('connect')).json()
if (!connectResponse.deezerAvailable) document.getElementById('deezer_not_available').classList.remove('hide') const spotifyStatus = connectResponse.spotifyEnabled ? SPOTIFY_STATUS.ENABLED : SPOTIFY_STATUS.DISABLED
if (!connectResponse.deezerAvailable) {
document.getElementById('deezer_not_available').classList.remove('hide')
}
store.dispatch('setAppInfo', connectResponse.update).catch(console.error) store.dispatch('setAppInfo', connectResponse.update).catch(console.error)
store.dispatch('setSpotifyStatus', spotifyStatus).catch(console.error)
let arl = localStorage.getItem('arl') let arl = localStorage.getItem('arl')
const accessToken = localStorage.getItem('accessToken') const accessToken = localStorage.getItem('accessToken')

View File

@ -198,7 +198,7 @@ export default defineComponent({
refreshFavorites refreshFavorites
} = useFavorites() } = useFavorites()
refreshFavorites({ isInitial: true }) refreshFavorites({ isInitial: true }).catch(console.error)
watch(isRefreshingFavorites, (newVal, oldVal) => { watch(isRefreshingFavorites, (newVal, oldVal) => {
// If oldVal is true and newOne is false, it means that a refreshing has just terminated // If oldVal is true and newOne is false, it means that a refreshing has just terminated

View File

@ -723,7 +723,7 @@
</template> </template>
<script> <script>
import { mapActions, mapGetters, mapMutations } from 'vuex' import { mapActions, mapGetters } from 'vuex'
import { debounce } from 'lodash-es' import { debounce } from 'lodash-es'
import TemplateVariablesList from '@components/settings/TemplateVariablesList.vue' import TemplateVariablesList from '@components/settings/TemplateVariablesList.vue'
@ -858,10 +858,9 @@ export default {
setSlimDownloads: 'setSlimDownloads', setSlimDownloads: 'setSlimDownloads',
setSlimSidebar: 'setSlimSidebar', setSlimSidebar: 'setSlimSidebar',
dispatchLogout: 'logout', dispatchLogout: 'logout',
dispatchLogin: 'login' dispatchLogin: 'login',
}), setSpotifyUserId: 'setSpotifyUserId',
...mapMutations({ refreshSpotifyStatus: 'refreshSpotifyStatus'
setSpotifyUserId: 'SET_SPOTIFY_USER_ID'
}), }),
onTemplateVariableClick(templateName) { onTemplateVariableClick(templateName) {
copyToClipboard(templateName) copyToClipboard(templateName)
@ -911,6 +910,8 @@ export default {
spotifySettings: this.lastCredentials, spotifySettings: this.lastCredentials,
spotifyUser: changed ? this.lastUser : false spotifyUser: changed ? this.lastUser : false
}) })
// this.refreshSpotifyStatus()
}, },
selectDownloadFolder() { selectDownloadFolder() {
window.api.send('selectDownloadFolder', this.settings.downloadLocation) window.api.send('selectDownloadFolder', this.settings.downloadLocation)
@ -1018,6 +1019,8 @@ export default {
this.loadCredentials(newCredentials) this.loadCredentials(newCredentials)
toast(this.$t('settings.toasts.update'), 'settings') toast(this.$t('settings.toasts.update'), 'settings')
this.refreshSpotifyStatus()
}, },
resetToDefault() { resetToDefault() {
const wantsToReset = confirm(this.$t('settings.resetMessage')) const wantsToReset = confirm(this.$t('settings.resetMessage'))

4
src/constants.js Normal file
View File

@ -0,0 +1,4 @@
export const SPOTIFY_STATUS = {
DISABLED: 'DISABLED',
ENABLED: 'ENABLED'
}

View File

@ -1,3 +1,6 @@
import { SPOTIFY_STATUS } from '@/constants'
import { fetchData } from '@/utils/api'
const getDefaultState = () => ({ const getDefaultState = () => ({
arl: localStorage.getItem('arl') || '', arl: localStorage.getItem('arl') || '',
accessToken: localStorage.getItem('accessToken') || '', accessToken: localStorage.getItem('accessToken') || '',
@ -12,6 +15,10 @@ const getDefaultState = () => ({
name: null, name: null,
picture: null picture: null
}, },
// This does not always represent the truth because the status update on the server is async
// and at the moment there's no way to notice the status change. Therefore a fetch of the status
// is needed everytime we need to use it
spotifyStatus: SPOTIFY_STATUS.DISABLED,
clientMode: false clientMode: false
}) })
@ -72,6 +79,25 @@ const actions = {
}, },
setClientMode({ commit }, payload) { setClientMode({ commit }, payload) {
commit('SET_CLIENT_MODE', payload) commit('SET_CLIENT_MODE', payload)
},
setSpotifyStatus({ commit }, newSpotifyStatus) {
commit('SET_SPOTIFY_STATUS', newSpotifyStatus)
},
setSpotifyUserId({ commit }, newSpotifyUserId) {
commit('SET_SPOTIFY_USER_ID', newSpotifyUserId)
},
/**
* Returning a Promise so that who calls this action is sure that
* the fetching is complete after the statement
*
* @example
* await store.dispatch('refreshSpotifyStatus')
* // From here the status is refreshed
*/
refreshSpotifyStatus({ commit }) {
return fetchData('spotifyStatus').then(response => {
commit('SET_SPOTIFY_STATUS', response.spotifyEnabled ? SPOTIFY_STATUS.ENABLED : SPOTIFY_STATUS.DISABLED)
})
} }
} }
@ -83,7 +109,7 @@ const getters = {
getClientMode: state => state.clientMode, getClientMode: state => state.clientMode,
isLoggedIn: state => !!state.arl, isLoggedIn: state => !!state.arl,
isLoggedWithSpotify: state => !!state.spotifyUser.id isLoggedWithSpotify: state => !!state.spotifyUser.id && state.spotifyStatus === SPOTIFY_STATUS.ENABLED
} }
const mutations = { const mutations = {
@ -108,8 +134,10 @@ const mutations = {
Object.assign(state, getDefaultState()) Object.assign(state, getDefaultState())
state.clientMode = clientMode state.clientMode = clientMode
}, },
SET_SPOTIFY_STATUS(state, newSpotifyStatus) {
state.spotifyStatus = newSpotifyStatus
},
SET_SPOTIFY_USER_ID(state, newSpotifyUserId) { SET_SPOTIFY_USER_ID(state, newSpotifyUserId) {
console.log('setting spotify user', { newSpotifyUserId })
state.spotifyUser = { state.spotifyUser = {
...state.spotifyUser, ...state.spotifyUser,
id: newSpotifyUserId id: newSpotifyUserId

View File

@ -1,13 +1,15 @@
import { ref } from '@vue/composition-api' import { ref, computed, watchEffect } from '@vue/composition-api'
import store from '@/store' import store from '@/store'
import { fetchData } from '@/utils/api' import { fetchData } from '@/utils/api'
import { SPOTIFY_STATUS } from '@/constants'
const favoriteArtists = ref([]) const favoriteArtists = ref([])
const favoriteAlbums = ref([]) const favoriteAlbums = ref([])
const favoriteSpotifyPlaylists = ref([]) const favoriteSpotifyPlaylists = ref([])
const favoritePlaylists = ref([]) const favoritePlaylists = ref([])
const favoriteTracks = ref([]) const favoriteTracks = ref([])
const isLoggedWithSpotify = computed(() => store.getters.isLoggedWithSpotify)
const isRefreshingFavorites = ref(false) const isRefreshingFavorites = ref(false)
@ -22,26 +24,30 @@ const setAllFavorites = data => {
favoriteTracks.value = tracks favoriteTracks.value = tracks
} }
const refreshFavorites = ({ isInitial = false }) => { const setSpotifyPlaylists = response => {
if (response.error === 'spotifyNotEnabled') {
favoriteSpotifyPlaylists.value = []
store.dispatch('setSpotifyStatus', SPOTIFY_STATUS.DISABLED).catch(console.error)
return
}
favoriteSpotifyPlaylists.value = response
}
const refreshFavorites = async ({ isInitial = false }) => {
if (!isInitial) { if (!isInitial) {
isRefreshingFavorites.value = true isRefreshingFavorites.value = true
} }
await store.dispatch('refreshSpotifyStatus')
fetchData('getUserFavorites').then(setAllFavorites).catch(console.error) fetchData('getUserFavorites').then(setAllFavorites).catch(console.error)
if (store.getters.isLoggedWithSpotify) { if (isLoggedWithSpotify.value) {
fetchData('getUserSpotifyPlaylists', { const spotifyUser = store.getters.getSpotifyUser.id
spotifyUser: store.getters.getSpotifyUser.id
})
.then(spotifyPlaylists => {
if (spotifyPlaylists.error === 'spotifyNotEnabled') {
favoriteSpotifyPlaylists.value = []
return
}
favoriteSpotifyPlaylists.value = spotifyPlaylists fetchData('getUserSpotifyPlaylists', { spotifyUser }).then(setSpotifyPlaylists).catch(console.error)
})
.catch(console.error)
} else { } else {
favoriteSpotifyPlaylists.value = [] favoriteSpotifyPlaylists.value = []
} }