gamebrary/src/App.vue

308 lines
7.7 KiB
Vue
Raw Normal View History

2018-10-19 05:15:28 +00:00
<template>
2019-11-08 20:34:06 +00:00
<div
id="app"
2020-08-25 05:19:30 +00:00
:class="['mvh-100', theme]"
2019-11-08 20:34:06 +00:00
:style="style"
:dir="dir"
>
2020-08-11 04:16:43 +00:00
<page-header />
2019-11-22 19:08:04 +00:00
<router-view v-if="user" />
2019-11-22 19:07:32 +00:00
<authorizing v-else />
2019-11-08 20:34:06 +00:00
</div>
2018-10-19 05:15:28 +00:00
</template>
<script>
2020-08-11 04:16:43 +00:00
import PageHeader from '@/components/PageHeader';
2019-11-22 19:07:32 +00:00
import Authorizing from '@/pages/Authorizing';
2018-11-05 02:28:29 +00:00
import firebase from 'firebase/app';
2019-10-09 16:30:07 +00:00
import { mapState } from 'vuex';
2018-11-05 02:28:29 +00:00
import 'firebase/auth';
import 'firebase/firestore';
2019-04-09 04:17:26 +00:00
import 'firebase/storage';
2020-08-15 00:00:04 +00:00
// TODO: store in env vars
2018-11-05 02:28:29 +00:00
firebase.initializeApp({
2019-11-08 19:56:03 +00:00
apiKey: 'AIzaSyA6MsmnLtqT4b11r-j15wwreRypO3AodcA',
authDomain: 'gamebrary.com',
databaseURL: 'https://gamebrary-8c736.firebaseio.com',
projectId: 'gamebrary-8c736',
storageBucket: 'gamebrary-8c736.appspot.com',
messagingSenderId: '324529217902',
2018-11-05 02:28:29 +00:00
});
2019-04-09 04:17:26 +00:00
const storage = firebase.storage().ref();
const db = firebase.firestore();
2018-10-19 05:15:28 +00:00
export default {
2019-11-08 19:56:03 +00:00
name: 'App',
2018-10-19 05:15:28 +00:00
2019-11-08 19:56:03 +00:00
components: {
2020-08-11 04:16:43 +00:00
PageHeader,
2019-11-22 19:07:32 +00:00
Authorizing,
2019-11-08 19:56:03 +00:00
},
2020-08-11 04:16:43 +00:00
data() {
return {
debugUserId: null,
};
},
2019-11-08 19:56:03 +00:00
computed: {
...mapState(['user', 'platform', 'wallpaperUrl', 'settings']),
2018-11-21 02:43:10 +00:00
2020-08-11 04:16:43 +00:00
userId() {
return this.debugUserId || this.user.uid;
},
2019-11-08 19:56:03 +00:00
dir() {
return this.settings && this.settings.language === 'ar'
? 'rtl'
: 'ltr';
},
2019-11-08 19:56:03 +00:00
style() {
return {
'background-image':
2020-08-14 23:58:55 +00:00
this.$route.name === 'board' &&
this.wallpaperUrl
? `url('${this.wallpaperUrl}')`
: null,
};
2019-11-08 19:56:03 +00:00
},
2019-04-09 04:17:26 +00:00
2019-11-08 19:56:03 +00:00
customWallpaper() {
// eslint-disable-next-line
2019-11-08 20:34:06 +00:00
return this.settings && this.settings.wallpapers && this.platform && this.settings.wallpapers[this.platform.code]
2019-11-08 19:56:03 +00:00
? this.settings.wallpapers[this.platform.code].url
: '';
},
2019-10-09 16:30:07 +00:00
2019-11-08 19:56:03 +00:00
theme() {
const hasPlatform = this.platform && this.platform.code;
const hasTheme = hasPlatform
2019-11-08 20:34:06 +00:00
&& this.settings
&& this.settings[this.platform.code]
&& this.settings[this.platform.code].theme;
2019-10-09 18:55:34 +00:00
2020-08-14 23:58:55 +00:00
const isBoard = this.$route.name === 'board';
2019-11-08 19:56:03 +00:00
const hasPlatformTheme = hasPlatform && hasTheme;
2020-08-14 23:58:55 +00:00
return isBoard && hasPlatformTheme
2019-11-08 19:56:03 +00:00
? `theme-${this.settings[this.platform.code].theme}`
: 'theme-default';
},
},
watch: {
customWallpaper(value) {
if (value) {
if (this.platform) {
this.loadWallpaper();
}
} else {
2020-08-18 22:01:16 +00:00
this.$store.commit('SET_WALLPAPER_URL_LEGACY', '');
2019-11-08 19:56:03 +00:00
}
},
},
mounted() {
this.init();
},
methods: {
init() {
if (this.user) {
this.syncData();
return;
}
if (this.customWallpaper) {
this.loadWallpaper();
}
firebase.auth().getRedirectResult().then(({ additionalUserInfo, user }) => {
if (additionalUserInfo && additionalUserInfo.isNewUser) {
this.$store.dispatch('SEND_WELCOME_EMAIL', additionalUserInfo);
}
if (user) {
return this.initUser(user);
}
return this.handleAuthRedirect();
});
},
handleAuthRedirect() {
const authProvider = this.$route.params.authProvider || 'google';
const firebaseAuthProvider = authProvider === 'twitter'
? new firebase.auth.TwitterAuthProvider()
: new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithRedirect(firebaseAuthProvider)
.catch((message) => {
2020-08-13 06:55:56 +00:00
this.$bvToast.toast(message, { title: 'Error', variant: 'danger' });
2019-11-08 19:56:03 +00:00
});
},
loadWallpaper() {
const wallpaperRef = this.customWallpaper;
2020-08-18 22:01:16 +00:00
this.$store.commit('SET_WALLPAPER_URL_LEGACY', '');
2019-11-08 19:56:03 +00:00
storage.child(wallpaperRef).getDownloadURL().then((url) => {
2020-08-18 22:01:16 +00:00
this.$store.commit('SET_WALLPAPER_URL_LEGACY', url);
2019-11-08 19:56:03 +00:00
});
},
syncData() {
// TODO: track progresses as well
2019-12-03 18:26:35 +00:00
// TODO: move to actions
2020-08-11 04:16:43 +00:00
db.collection('lists').doc(this.userId)
2019-11-08 19:56:03 +00:00
.onSnapshot((doc) => {
if (doc.exists) {
const gameLists = doc.data();
2020-08-18 22:01:16 +00:00
this.$store.commit('SET_GAME_LISTS_LEGACY', gameLists);
2019-11-08 19:56:03 +00:00
}
});
2019-12-03 18:26:35 +00:00
// TODO: move to actions
2020-08-11 04:16:43 +00:00
db.collection('settings').doc(this.userId)
2019-11-08 19:56:03 +00:00
.onSnapshot((doc) => {
if (doc.exists) {
const settings = doc.data();
this.$store.commit('SET_SETTINGS', settings);
}
});
2019-12-03 18:26:35 +00:00
// TODO: move to actions
2020-08-11 04:16:43 +00:00
db.collection('tags').doc(this.userId)
2019-11-08 19:56:03 +00:00
.onSnapshot((doc) => {
if (doc.exists) {
const tags = doc.data();
this.$store.commit('SET_TAGS', tags);
}
});
2019-12-03 18:26:35 +00:00
// TODO: move to actions
2020-08-11 04:16:43 +00:00
db.collection('notes').doc(this.userId)
2019-11-08 19:56:03 +00:00
.onSnapshot((doc) => {
if (doc.exists) {
const notes = doc.data();
this.$store.commit('SET_NOTES', notes);
}
});
// TODO: move to actions
2020-08-11 04:16:43 +00:00
db.collection('progresses').doc(this.userId)
.onSnapshot((doc) => {
if (doc.exists) {
const progresses = doc.data();
this.$store.commit('SET_PROGRESSES', progresses);
}
});
2019-11-08 19:56:03 +00:00
},
initUser(user) {
this.$store.commit('SET_USER', user);
this.loadSettings();
this.loadTags();
this.loadLists();
this.syncData();
},
loadSettings() {
2019-12-03 18:26:35 +00:00
// TODO: move to actions
2020-08-11 04:16:43 +00:00
const docRef = db.collection('settings').doc(this.userId);
2019-10-09 18:55:34 +00:00
2019-11-08 19:56:03 +00:00
docRef.get().then((doc) => {
const hasData = doc && doc.exists;
2019-10-09 16:30:07 +00:00
2019-11-08 19:56:03 +00:00
return hasData
? this.$store.commit('SET_SETTINGS', doc.data())
: this.initSettings();
}).catch(() => {
2020-08-13 06:55:56 +00:00
this.$bvToast.toast('Authentication error', { title: 'Error', variant: 'danger' });
2019-11-08 19:56:03 +00:00
this.$router.push({ name: 'sessionExpired' });
});
2019-04-09 04:17:26 +00:00
},
2019-11-08 19:56:03 +00:00
loadLists() {
2019-12-03 18:26:35 +00:00
// TODO: move to actions
2020-08-11 04:16:43 +00:00
db.collection('lists').doc(this.userId).get()
2019-11-08 19:56:03 +00:00
.then((doc) => {
if (doc.exists) {
const data = doc.data();
2020-08-18 22:01:16 +00:00
this.$store.commit('SET_GAME_LISTS_LEGACY', data);
2019-11-08 19:56:03 +00:00
} else {
this.initList();
}
})
.catch(() => {
2020-08-13 06:55:56 +00:00
this.$bvToast.toast('Authentication error', { title: 'Error', variant: 'danger' });
2019-11-08 19:56:03 +00:00
this.$router.push({ name: 'sessionExpired' });
});
},
2019-11-08 19:56:03 +00:00
loadTags() {
2019-12-03 18:26:35 +00:00
// TODO: move to actions
2020-08-11 04:16:43 +00:00
db.collection('tags').doc(this.userId).get()
2019-11-08 19:56:03 +00:00
.then((doc) => {
if (doc.exists) {
const data = doc.data();
this.$store.commit('SET_TAGS', data);
}
})
.catch(() => {
2020-08-13 06:55:56 +00:00
this.$bvToast.toast('Authentication error', { title: 'Error', variant: 'danger' });
2019-11-08 19:56:03 +00:00
this.$router.push({ name: 'sessionExpired' });
});
},
2019-11-08 19:56:03 +00:00
initList() {
2019-12-03 18:26:35 +00:00
// TODO: move to actions
2020-08-11 04:16:43 +00:00
db.collection('lists').doc(this.userId).set({}, { merge: true })
2019-11-08 19:56:03 +00:00
.then(() => {
this.loadLists();
})
.catch(() => {
2020-08-13 06:55:56 +00:00
this.$bvToast.toast('Authentication error', { title: 'Error', variant: 'danger' });
2019-11-08 19:56:03 +00:00
this.$router.push({ name: 'sessionExpired' });
});
},
2019-11-08 19:56:03 +00:00
initSettings() {
2019-12-03 18:26:35 +00:00
// TODO: move to actions
2020-08-11 04:16:43 +00:00
db.collection('settings').doc(this.userId).set({}, { merge: true })
2019-11-08 19:56:03 +00:00
.then(() => {
this.loadSettings();
})
.catch(() => {
2020-08-13 06:55:56 +00:00
this.$bvToast.toast('Authentication error', { title: 'Error', variant: 'danger' });
2019-11-08 19:56:03 +00:00
this.$router.push({ name: 'sessionExpired' });
});
},
2019-11-08 19:56:03 +00:00
},
2018-10-19 05:15:28 +00:00
};
</script>
<style lang="scss" rel="stylesheet/scss">
2019-11-08 20:34:06 +00:00
@import url(https://fonts.googleapis.com/css?family=Fira+Sans:700|Roboto:400,400italic,700);
2020-07-22 20:44:48 +00:00
// @import "~styles/styles";
2018-10-19 05:15:28 +00:00
</style>
2019-02-15 22:58:32 +00:00
<style lang="scss" rel="stylesheet/scss" scoped>
2019-11-08 20:34:06 +00:00
#app {
2020-08-25 05:19:30 +00:00
display: flex;
flex-direction: column;
2020-08-11 04:16:43 +00:00
background-color: #ccc;
2020-08-25 06:34:39 +00:00
min-height: 100vh;
2019-11-08 20:34:06 +00:00
}
2019-02-15 22:58:32 +00:00
</style>