wip: login data in store

This commit is contained in:
Roberto Tonino 2020-09-17 22:43:52 +02:00
parent 5205581719
commit d9a128b142
6 changed files with 195 additions and 134 deletions

File diff suppressed because one or more lines are too long

View File

@ -10,7 +10,6 @@ import i18n from '@/plugins/i18n'
import router from '@/router' import router from '@/router'
import store from '@/store' import store from '@/store'
import $ from 'jquery'
import { socket } from '@/utils/socket' import { socket } from '@/utils/socket'
import { toast } from '@/utils/toasts' import { toast } from '@/utils/toasts'
import { init as initTabs } from '@js/tabs.js' import { init as initTabs } from '@js/tabs.js'
@ -48,24 +47,24 @@ window.addEventListener('pywebviewready', initClient)
/* ===== Global shortcuts ===== */ /* ===== Global shortcuts ===== */
document.addEventListener('keyup', keyEvent => { document.addEventListener('keyup', keyEvent => {
if (keyEvent.key == "Backspace" && keyEvent.ctrlKey){ if (keyEvent.key == 'Backspace' && keyEvent.ctrlKey) {
let searchbar = document.querySelector('#searchbar') let searchbar = document.querySelector('#searchbar')
searchbar.value = "" searchbar.value = ''
searchbar.focus() searchbar.focus()
} }
}) })
document.addEventListener('paste', pasteEvent => { document.addEventListener('paste', pasteEvent => {
pasteText = pasteEvent.clipboardData.getData('Text') let pasteText = pasteEvent.clipboardData.getData('Text')
if (pasteEvent.target.localName != "input"){ if (pasteEvent.target.localName != 'input') {
if (isValidURL(pasteText)){ if (isValidURL(pasteText)) {
if (main_selected === 'analyzer_tab') { if (window.main_selected === 'analyzer_tab') {
EventBus.$emit('linkAnalyzerTab:reset') EventBus.$emit('linkAnalyzerTab:reset')
socket.emit('analyzeLink', pasteText) socket.emit('analyzeLink', pasteText)
} else { } else {
Downloads.sendAddToQueue(pasteText) Downloads.sendAddToQueue(pasteText)
} }
}else{ } else {
let searchbar = document.querySelector('#searchbar') let searchbar = document.querySelector('#searchbar')
searchbar.select() searchbar.select()
searchbar.setSelectionRange(0, 99999) searchbar.setSelectionRange(0, 99999)
@ -98,72 +97,36 @@ socket.on('init_autologin', function() {
}) })
socket.on('logged_in', function(data) { socket.on('logged_in', function(data) {
switch (data.status) { const { status, user } = data
console.log('on logged')
switch (status) {
case 1: case 1:
case 3: case 3:
// Login ok
toast(i18n.t('toasts.loggedIn'), 'done', true, 'login-toast') toast(i18n.t('toasts.loggedIn'), 'done', true, 'login-toast')
// Idea: set this whole object in the localStorage to read it in the future store.dispatch('login', data)
// Other idea would be using vuex
if (data.arl) {
localStorage.setItem('arl', data.arl)
// $('#login_input_arl').val(data.arl)
}
// ? What's this?
$('#open_login_prompt').hide()
if (data.user) {
$('#settings_username').text(data.user.name)
$('#settings_picture').attr(
'src',
`https://e-cdns-images.dzcdn.net/images/user/${data.user.picture}/125x125-000000-80-0-0.jpg`
)
$('#logged_in_info').removeClass('hide')
// document.getElementById('logged_in_info').classList.remove('hide')
}
// $('#home_not_logged_in').addClass('hide')
document.getElementById('home_not_logged_in').classList.add('hide')
break break
case 2: case 2:
// Already logged in
toast(i18n.t('toasts.alreadyLogged'), 'done', true, 'login-toast') toast(i18n.t('toasts.alreadyLogged'), 'done', true, 'login-toast')
if (data.user) { store.dispatch('setUser', user)
$('#settings_username').text(data.user.name)
$('#settings_picture').attr(
'src',
`https://e-cdns-images.dzcdn.net/images/user/${data.user.picture}/125x125-000000-80-0-0.jpg`
)
// $('#logged_in_info').show()
// document.getElementById('logged_in_info').classList.remove('hide')
$('#logged_in_info').removeClass('hide')
}
document.getElementById('home_not_logged_in').classList.add('hide')
break break
case 0: case 0:
// Login failed
toast(i18n.t('toasts.loginFailed'), 'close', true, 'login-toast') toast(i18n.t('toasts.loginFailed'), 'close', true, 'login-toast')
localStorage.removeItem('arl')
$('#login_input_arl').val('') store.dispatch('removeARL')
$('#open_login_prompt').show()
$('#logged_in_info').addClass('hide')
// document.getElementById('logged_in_info').classList.add('hide')
$('#settings_username').text('Not Logged')
$('#settings_picture').attr('src', `https://e-cdns-images.dzcdn.net/images/user/125x125-000000-80-0-0.jpg`)
document.getElementById('home_not_logged_in').classList.remove('hide')
break break
} }
}) })
socket.on('logged_out', function() { socket.on('logged_out', function() {
toast(i18n.t('toasts.loggedOut'), 'done', true, 'login-toast') toast(i18n.t('toasts.loggedOut'), 'done', true, 'login-toast')
localStorage.removeItem('arl')
$('#login_input_arl').val('') store.dispatch('logout')
$('#open_login_prompt').show()
document.getElementById('logged_in_info').classList.add('hide')
$('#settings_username').text('Not Logged')
$('#settings_picture').attr('src', `https://e-cdns-images.dzcdn.net/images/user/125x125-000000-80-0-0.jpg`)
document.getElementById('home_not_logged_in').classList.remove('hide')
}) })
socket.on('restoringQueue', function() { socket.on('restoringQueue', function() {
@ -179,11 +142,11 @@ socket.on('currentItemCancelled', function(uuid) {
}) })
socket.on('startAddingArtist', function(data) { socket.on('startAddingArtist', function(data) {
toast(i18n.t('toasts.startAddingArtist', {artist: data.name}), 'loading', false, 'artist_' + data.id) toast(i18n.t('toasts.startAddingArtist', { artist: data.name }), 'loading', false, 'artist_' + data.id)
}) })
socket.on('finishAddingArtist', function(data) { socket.on('finishAddingArtist', function(data) {
toast(i18n.t('toasts.finishAddingArtist', {artist: data.name}), 'done', true, 'artist_' + data.id) toast(i18n.t('toasts.finishAddingArtist', { artist: data.name }), 'done', true, 'artist_' + data.id)
}) })
socket.on('startConvertingSpotifyPlaylist', function(id) { socket.on('startConvertingSpotifyPlaylist', function(id) {
@ -204,7 +167,7 @@ socket.on('queueError', function(queueItem) {
}) })
socket.on('alreadyInQueue', function(data) { socket.on('alreadyInQueue', function(data) {
toast(i18n.t('toasts.alreadyInQueue', {item: data.title}), 'playlist_add_check') toast(i18n.t('toasts.alreadyInQueue', { item: data.title }), 'playlist_add_check')
}) })
socket.on('loginNeededToDownload', function(data) { socket.on('loginNeededToDownload', function(data) {

View File

@ -1,10 +1,15 @@
<template> <template>
<div id="home_tab" class="main_tabcontent" ref="root"> <div id="home_tab" class="main_tabcontent" ref="root">
<h2 class="page_heading">{{ $t('globals.welcome') }}</h2> <h2 class="page_heading">{{ $t('globals.welcome') }}</h2>
<section id="home_not_logged_in" class="home_section" ref="notLogged">
<section class="home_section" ref="notLogged" v-if="!isLoggedIn">
<p id="home_not_logged_text">{{ $t('home.needTologin') }}</p> <p id="home_not_logged_text">{{ $t('home.needTologin') }}</p>
<button type="button" name="button" @click="openSettings">{{ $t('home.openSettings') }}</button> <!-- <button type="button" name="button" @click="openSettings">{{ $t('home.openSettings') }}</button> -->
<router-link tag="button" name="button" :to="{ name: 'Settings' }">
{{ $t('home.openSettings') }}
</router-link>
</section> </section>
<section v-if="playlists.length" class="home_section"> <section v-if="playlists.length" class="home_section">
<h3 class="section_heading">{{ $t('home.sections.popularPlaylists') }}</h3> <h3 class="section_heading">{{ $t('home.sections.popularPlaylists') }}</h3>
<div class="release_grid"> <div class="release_grid">
@ -39,6 +44,7 @@
</div> </div>
</div> </div>
</section> </section>
<section v-if="albums.length" class="home_section"> <section v-if="albums.length" class="home_section">
<h3 class="section_heading">{{ $t('home.sections.popularAlbums') }}</h3> <h3 class="section_heading">{{ $t('home.sections.popularAlbums') }}</h3>
<div class="release_grid"> <div class="release_grid">
@ -84,7 +90,7 @@ export default {
} }
}, },
computed: { computed: {
...mapGetters(['getHomeData']), ...mapGetters(['getHomeData', 'isLoggedIn']),
needToWait() { needToWait() {
return this.getHomeData.albums.data.length === 0 && this.getHomeData.playlists.data.length === 0 return this.getHomeData.albums.data.length === 0 && this.getHomeData.playlists.data.length === 0
} }
@ -93,9 +99,6 @@ export default {
artistView: showView.bind(null, 'artist'), artistView: showView.bind(null, 'artist'),
albumView: showView.bind(null, 'album'), albumView: showView.bind(null, 'album'),
playlistView: showView.bind(null, 'playlist'), playlistView: showView.bind(null, 'playlist'),
openSettings() {
document.getElementById('main_settings_tablink').click()
},
addToQueue(e) { addToQueue(e) {
Downloads.sendAddToQueue(e.currentTarget.dataset.link) Downloads.sendAddToQueue(e.currentTarget.dataset.link)
}, },
@ -127,19 +130,11 @@ export default {
} }
}, },
mounted() { mounted() {
console.log('home mounted') // if (localStorage.getItem('arl')) {
// this.$refs.root.style.display = 'block' // this.$refs.notLogged.classList.add('hide')
// }
if (localStorage.getItem('arl')) {
this.$refs.notLogged.classList.add('hide')
}
this.checkIfWaitData(this.getHomeData) this.checkIfWaitData(this.getHomeData)
// socket.on('init_home', this.initHome)
},
beforeDestroy() {
console.log('home bef dest')
// this.$refs.root.style.display = 'none'
} }
} }
</script> </script>

View File

@ -2,14 +2,17 @@
<div id="settings_tab" class="main_tabcontent fixed_footer" ref="root"> <div id="settings_tab" class="main_tabcontent fixed_footer" ref="root">
<h2 class="page_heading">{{ $t('settings.title') }}</h2> <h2 class="page_heading">{{ $t('settings.title') }}</h2>
<div id="logged_in_info" ref="loggedInInfo"> <div id="logged_in_info" v-if="isLoggedIn" ref="loggedInInfo">
<img id="settings_picture" src="" alt="Profile Picture" ref="userpicture" class="circle" /> <img id="settings_picture" :src="pictureHref" alt="Profile Picture" ref="userpicture" class="circle" />
<i18n path="settings.login.loggedIn" tag="p"> <i18n path="settings.login.loggedIn" tag="p">
<strong place="username" id="settings_username" ref="username"></strong> <strong place="username" id="settings_username" ref="username">{{ user.name || 'Non loggato ahah' }}</strong>
</i18n> </i18n>
<button id="settings_btn_logout" @click="logout">{{ $t('settings.login.logout') }}</button> <button id="settings_btn_logout" @click="logout">{{ $t('settings.login.logout') }}</button>
<select v-if="accounts.length" id="family_account" v-model="accountNum" @change="changeAccount"> <select v-if="accounts.length" id="family_account" v-model="accountNum" @change="changeAccount">
<option v-for="(account, i) in accounts" :value="i.toString()">{{ account.BLOG_NAME }}</option> <option v-for="(account, i) in accounts" :key="account" :value="i.toString()">{{ account.BLOG_NAME }}</option>
</select> </select>
</div> </div>
@ -18,7 +21,14 @@
<i class="material-icons">person</i>{{ $t('settings.login.title') }} <i class="material-icons">person</i>{{ $t('settings.login.title') }}
</h3> </h3>
<div class="inline-flex"> <div class="inline-flex">
<input autocomplete="off" type="password" id="login_input_arl" ref="loginInput" placeholder="ARL" /> <input
autocomplete="off"
type="password"
:value="arl"
id="login_input_arl"
ref="loginInput"
placeholder="ARL"
/>
<button id="settings_btn_copyArl" class="only_icon" @click="copyARLtoClipboard"> <button id="settings_btn_copyArl" class="only_icon" @click="copyARLtoClipboard">
<i class="material-icons">assignment</i> <i class="material-icons">assignment</i>
</button> </button>
@ -600,6 +610,7 @@
</footer> </footer>
</div> </div>
</template> </template>
<style lang="scss"> <style lang="scss">
.locale-flag { .locale-flag {
width: 60px; width: 60px;
@ -627,45 +638,68 @@
</style> </style>
<script> <script>
import Vue from 'vue' import { mapActions, mapGetters } from 'vuex'
import { mapGetters } from 'vuex'
import { toast } from '@/utils/toasts' import { toast } from '@/utils/toasts'
import { socket } from '@/utils/socket' import { socket } from '@/utils/socket'
import EventBus from '@/utils/EventBus' import EventBus from '@/utils/EventBus'
import flags from '@/utils/flags' import flags from '@/utils/flags'
export default { export default {
name: 'the-settings-tab', data() {
data: () => ({ return {
flags, flags,
currentLocale: 'en', currentLocale: 'en',
locales: [], locales: [],
settings: { settings: {
tags: {} tags: {}
},
lastSettings: {},
spotifyFeatures: {},
lastCredentials: {},
// defaultSettings: {},
lastUser: '',
spotifyUser: '',
slimDownloads: false,
previewVolume: window.vol,
accountNum: 0,
accounts: []
}
},
computed: {
...mapGetters({
getSettings: 'getSettings',
getCredentials: 'getCredentials',
getDefaultSettings: 'getDefaultSettings',
arl: 'getARL',
user: 'getUser',
isLoggedIn: 'isLoggedIn'
}),
needToWait() {
return Object.keys(this.getSettings).length === 0
}, },
lastSettings: {}, changeSlimDownloads: {
spotifyFeatures: {}, get() {
lastCredentials: {}, return this.slimDownloads
// defaultSettings: {}, },
lastUser: '', set(wantSlimDownloads) {
spotifyUser: '', this.slimDownloads = wantSlimDownloads
slimDownloads: false, document.getElementById('download_list').classList.toggle('slim', wantSlimDownloads)
previewVolume: window.vol, localStorage.setItem('slimDownloads', wantSlimDownloads)
accountNum: 0, }
accounts: [] },
}), pictureHref() {
// Default image: https://e-cdns-images.dzcdn.net/images/user/125x125-000000-80-0-0.jpg
return `https://e-cdns-images.dzcdn.net/images/user/${this.user.picture}/125x125-000000-80-0-0.jpg`
}
},
mounted() { mounted() {
console.log('settings mounted')
this.locales = this.$i18n.availableLocales this.locales = this.$i18n.availableLocales
this.revertSettings() this.revertSettings()
this.revertCredentials() this.revertCredentials()
// EventBus.$on('settingsTab:revertSettings', this.revertSettings) // this.$refs.loggedInInfo.classList.add('hide')
// EventBus.$on('settingsTab:revertCredentials', this.revertCredentials)
this.$refs.loggedInInfo.classList.add('hide')
let storedLocale = localStorage.getItem('locale') let storedLocale = localStorage.getItem('locale')
@ -676,9 +710,9 @@ export default {
let storedArl = localStorage.getItem('arl') let storedArl = localStorage.getItem('arl')
if (storedArl) { // if (storedArl) {
this.$refs.loginInput.value = storedArl.trim() // this.$refs.loginInput.value = storedArl.trim()
} // }
let storedAccountNum = localStorage.getItem('accountNum') let storedAccountNum = localStorage.getItem('accountNum')
@ -712,23 +746,11 @@ export default {
socket.on('downloadFolderSelected', this.downloadFolderSelected) socket.on('downloadFolderSelected', this.downloadFolderSelected)
socket.on('applogin_arl', this.setArl) socket.on('applogin_arl', this.setArl)
}, },
computed: {
...mapGetters(['getSettings', 'getCredentials', 'getDefaultSettings']),
needToWait() {
return Object.keys(this.getSettings).length === 0
},
changeSlimDownloads: {
get() {
return this.slimDownloads
},
set(wantSlimDownloads) {
this.slimDownloads = wantSlimDownloads
document.getElementById('download_list').classList.toggle('slim', wantSlimDownloads)
localStorage.setItem('slimDownloads', wantSlimDownloads)
}
}
},
methods: { methods: {
...mapActions({
dispatchARL: 'setARL'
}),
waitSettings() { waitSettings() {
if (this.needToWait) { if (this.needToWait) {
// Checking if the saving of the settings is completed // Checking if the saving of the settings is completed
@ -802,7 +824,8 @@ export default {
}, },
login() { login() {
let arl = this.$refs.loginInput.value.trim() let arl = this.$refs.loginInput.value.trim()
if (arl != '' && arl != localStorage.getItem('arl')) {
if (arl !== '' && arl !== localStorage.getItem('arl')) {
socket.emit('login', arl, true, this.accountNum) socket.emit('login', arl, true, this.accountNum)
} }
}, },
@ -812,7 +835,10 @@ export default {
} }
}, },
setArl(arl) { setArl(arl) {
this.$refs.loginInput.value = arl console.log({ arl })
// this.$refs.loginInput.value = arl
this.dispatchARL(arl)
this.login() this.login()
}, },
changeAccount() { changeAccount() {

View File

@ -8,6 +8,7 @@ import spotifyCredentials from '@/store/modules/spotifyCredentials'
import charts from '@/store/modules/charts' import charts from '@/store/modules/charts'
import favorites from '@/store/modules/favorites' import favorites from '@/store/modules/favorites'
import about from '@/store/modules/about' import about from '@/store/modules/about'
import login from '@/store/modules/login'
// Load Vuex // Load Vuex
Vue.use(Vuex) Vue.use(Vuex)
@ -21,7 +22,8 @@ export default new Vuex.Store({
spotifyCredentials, spotifyCredentials,
charts, charts,
favorites, favorites,
about about,
login
}, },
strict: process.env.NODE_ENV !== 'production' strict: process.env.NODE_ENV !== 'production'
}) })

View File

@ -0,0 +1,75 @@
const getDefaultState = () => {
return {
arl: '',
status: null,
user: {
id: null,
name: '',
picture: ''
}
}
}
const state = getDefaultState()
const actions = {
login({ commit }, payload) {
const { arl, user, status } = payload
commit('SET_ARL', arl)
commit('SET_USER', user)
commit('SET_STATUS', status)
},
logout({ commit }) {
console.log('logout')
commit('RESET_LOGIN')
localStorage.removeItem('arl')
},
setARL({ commit }, payload) {
const { arl, saveOnLocalStorage } = payload
commit('SET_ARL', arl)
if (saveOnLocalStorage) {
localStorage.setItem('arl', arl)
}
},
removeARL({ commit }) {
commit('SET_ARL', '')
localStorage.removeItem('arl')
},
setUser({ commit }, payload) {
commit('SET_USER', payload)
}
}
const getters = {
getARL: state => state.arl,
getUser: state => state.user,
isLoggedIn: state => [1, 2, 3].indexOf(state.status) !== -1
}
const mutations = {
SET_ARL(state, payload) {
state.arl = payload
},
SET_STATUS(state, payload) {
state.status = payload
},
SET_USER(state, payload) {
state.user = payload
},
RESET_LOGIN(state) {
// Needed for reactivity
Object.assign(state, getDefaultState())
}
}
export default {
state,
getters,
actions,
mutations
}